doPost() is the method in the servlet that is used to service the Http requests submitted using the Post option.
HTML forms can be submitted in two ways using the Get method and the Post method. You can specify the method while declaring the form itself. So you will have two methods in the servlet doGet() and doPost() each for one of them
There are 3 main types of requests that get processed by a Servlet. They are: • Get • Post • Put Each of them have a corresponding doXXX() method in the Servlet class which would be: • doGet • doPost • doPut You must override either of these methods based on the type of requests that would be processed by your servlet
The doGet() method is the method inside a servlet that gets called every time a request from a jsp page is submitted. The control first reaches the doGet() method of the servlet and then the servlet decides what functionality to invoke based on the submit request. The get method called when the type of page submission is "GET" There is another way of submitting requests from a jsp page is "POST" and when that happens it calls the doPost() method inside the servlet.
The difference between a GET and a POST is the way data is transferred to a servlet. With a GET, the URL will show each name/value pair on the query string in the URL. For example, if you had a form with a field named 'foo,' and when submitted had a value of 'bar,' the URL might be something like this: http://www.example.com/servlet?foo=bar With a POST, this information is not visible in the URL. Instead, it is transferred in the HTTP headers. As far as the actual servlet is concerned, there is not a great deal of difference when it comes to getting the parameters. Whether you use a GET or a POST, you still use request.getParameter("foo"); to get the value. The method used in the Servlet for processing either a GET or a POST is different too. If you use a GET, the method that is called is doGet(HttpServletRequest, HttpServletResponse). The doGet method is also called if there is no GET or POST data. If you use a POST, the method called is doPost(HttpServletRequest, HttpServletResponse).
A Servlet Mapping is a directive in the web.xml that tells the Servlet Container which class to use when a particular Servlet is called. A Servlet is a class within your Java Web Application. Let's say you have a servlet called MyServlet in the com.example.servlet package. You would need to have a Servlet Mapping pointing the path "/MyServlet" to the "com.example.servlet.MyServlet" class. Without the servlet mapping, you would not be able to invoke your servlet because the Servlet container would not know where it is. JSPs are different - they do not need mappings like this. JSPs exist within the WebRoot of the application, so they are always available. Servlets exist in the WEB-INF\Classes directory once your application is deployed.
Yes you can but it is not required. A Servlet is nothing but another .java file and all rules that are applicable to standard Java classes are applicable to them. Note: Even if you write a constructor in a servlet, it will not get executed.
ServletRequest provides the functionalities to the classes to access the HTTP requests. The servlet container creates an HttpServletRequest object and an HttpServletResponse object, and passes them as arguments to the servlet's service methods such as doGet, doPost, etc.
There are 3 main types of requests that get processed by a Servlet. They are: • Get • Post • Put Each of them have a corresponding doXXX() method in the Servlet class which would be: • doGet • doPost • doPut You must override either of these methods based on the type of requests that would be processed by your servlet
The doGet() method is the method inside a servlet that gets called every time a request from a jsp page is submitted. The control first reaches the doGet() method of the servlet and then the servlet decides what functionality to invoke based on the submit request. The get method called when the type of page submission is "GET" There is another way of submitting requests from a jsp page is "POST" and when that happens it calls the doPost() method inside the servlet.
The Http RequestWhen a user hits a URL with a servlet at the other end, the Servlet Container creates an HttpServletRequest object. It passes this object as an argument to the servlet's service methods (doPut(), doGet(), and doPost()). There is a lot of information in this object, including the login details of the user making this request and the name of the HTTP method with which this request was made.
The doXXX MethodsThere are 3 main types of requests that get processed by a Servlet. They are:• Get• Post• PutEach of them have a corresponding doXXX() method in the Servlet class which would be:• doGet• doPost• doPutThese methods are called by the service method in your Servlet.Let us now take a look at a sample Servlet that has these 3 doXXX methods.import java.io.*;import javax.servlet.*;import javax.servlet.http.*;/*** A servlet that has the 3 doXXX Methods** @author Anand*/public class OurSecondServlet extends HttpServlet{// doGet()public void doGet(HttpServletRequest request,HttpServletResponse response)throws IOException, ServletException{requestType("GET", response);}// doPost()public void doPost(HttpServletRequest request,HttpServletResponse response)throws IOException, ServletException{requestType("POST", response);}// doPut()public void doPut(HttpServletRequest request,HttpServletResponse response)throws IOException, ServletException{requestType("PUT", response);}public void requestType(String requestType,HttpServletResponse response)throws IOException, ServletException{response.setContentType("text/HTML");PrintWriter out = response.getWriter();out.println("< HTML >");out.println("< head >");out.println("< title >Our Second Servlet" +"< / title >");out.println("< / head >");out.println("< body >");out.println("< h1 >Servlet Request< / h1 >");out.println("The Submitted Request type is : " + requestType);out.println("< / body >");out.println("< / HTML >");}}
A Servlets skeleton would look like below: /* * servlet name * * servlet description * All other stuff that is part of a Standard Class comment section */ //package declarations //import statements public class ServletName extends HttpServlet { // Instance Variables /** * Code to Initialize the Servlet */ public void init() throws ServletException { // Servlet Initialization Code goes here } /** * The Service Method * Gets invoked for every request submitted to the Servlet * This method is optional. We mostly use doGet, doPost Methods */ protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { // Code for the Service Method goes here } /** * Process a GET request * */ protected void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException { // Code for the doGet() method goes here } /** * Process a POST request * */ protected void doPost(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException { // Code for the doPost() method goes here } /** * Process a PUT request * */ protected void doPut(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { //Code for the doPut() method goes here } /** * You can have any number of methods for your processing * here. There is no limit as to the number of methods or * any restrictions on what you can name them. * Since this is all java code, you need to keep them * syntactically correct as per Java Coding Standards. */ /** * Clean-Up */ public void destroy() { // clean up activities before the Servlet is put to death } }
JSPs and Servlets can be used interchangeably for the most part. In fact, when a JSP is compiled, it is actually turned into a servlet! That being said, there are some guidelines for using servlets over JSPs or vice versa. Here are a couple of examples, but by no means a complete list: Typically, you would use a JSP to display dynamic data, or a form. A Servlet is usually what you would want to use to process a post or a get, especially considering the method that is called when you call a servlet (either doGet or doPost). For more information on this, here is a good article on Servlet and JSP Best Practices: http:/java.sun.com/developer/technicalArticles/javaserverpages/servlets_jsp
Http servlet and Generic servlet
Servlet SkeletonIf I ask you, what are the components of a Java class, you'll happily tell me that, there are first package statements and then imports and then the class declaration. Within the class brackets, we have constructors, instance variables, methods etc. That was easy, wasn't it?The same way, every Servlet has a certain set of components that are mandatory for its well-being. (I just got carried away a bit) Or I must say, for its proper functioning.A Servlets skeleton would look like below:/** servlet name** servlet description* All other stuff that is part of a Standard Class comment section*///package declarations//import statementspublic class ServletName extends HttpServlet {// Instance Variables/*** Code to Initialize the Servlet*/public void init() throws ServletException{// Servlet Initialization Code goes here}/*** The Service Method* Gets invoked for every request submitted to the Servlet* This method is optional. We mostly use doGet, doPost Methods*/protected void service(HttpServletRequest req,HttpServletResponse resp)throws ServletException, IOException{// Code for the Service Method goes here}/*** Process a GET request**/protected void doGet(HttpServletRequest request,HttpServletResponse response)throws IOException, ServletException{// Code for the doGet() method goes here}/*** Process a POST request**/protected void doPost(HttpServletRequest request,HttpServletResponse response)throws IOException, ServletException{// Code for the doPost() method goes here}/*** Process a PUT request**/protected void doPut(HttpServletRequest req,HttpServletResponse resp)throws ServletException, IOException{//Code for the doPut() method goes here}/*** You can have any number of methods for your processing* here. There is no limit as to the number of methods or* any restrictions on what you can name them.* Since this is all java code, you need to keep them* syntactically correct as per Java Coding Standards.*//*** Clean-Up*/public void destroy(){// clean up activities before the Servlet is put to death}}The above is what a Servlets skeleton would look like
Servlet Chaining means the output of one servlet act as a input to another servlet. Servlet Aliasing allows us to invoke more than one servlet in sequence when the URL is opened with a common servlet alias. The output from first Servlet is sent as input to other Servlet and so on. The Output from the last Servlet is sent back to the browser. The entire process is called Servlet Chaining.
Comparison Between Get and Post methods:GETPOSTQuery string or form data is simply appended to the URL as name-value pairs.Form name-value pairs are sent in the body of the request, not in the URL itself.Query length is limited (~1KB).Query length is unlimited.Users can see data in address bar.Data hidden from users.Not Safe - A person standing over your shoulder can view your userid/pwd if submitted via GetSafe - No one will be able to view what data is getting submittedinvokes doGet()invokes doPost()Supports ASCII.Supports ASCII + Binary.Easy to bookmarkHard to bookmark.
No. Javascript code can be present inside a JSP but not inside a servlet. A Servlet is a pure java class.