Tuesday, 24 April 2018

HttpServlet class

HttpServlet class,Hierarchy of Http Servlet,Methods of HttpServlet class,service(ServletRequest req,ServletResponse res),service(HttpServletRequest req, HttpServletResponse res),doGet(HttpServletRequest req, HttpServletResponse res),doPost(HttpServletRequest req, HttpServletResponse res),doHead(HttpServletRequest req, HttpServletResponse res),doOptions(HttpServletRequest req, HttpServletResponse res),doPut(HttpServletRequest req, HttpServletResponse res),doTrace(HttpServletRequest req, HttpServletResponse res),doDelete(HttpServletRequest req, HttpServletResponse res),getLastModified(HttpServletRequest req),Example of HttpServlet class,

The HttpServlet class extends the GenericServlet class and implements Serializable interface. It provides http specific methods such as doGet, doPost, doHead, doTrace etc.

Hierarchy of Http Servlet

java.lang.Object
          |_extended by javax.servlet.GenericServlet
           |_extended by javax.servlet.http.HttpServlet


Methods of HttpServlet class

1. service(ServletRequest req,ServletResponse res):Dispatches the requests to the protected service method. It converts the request and response object into http type before dispatching request.
Syntax: public void service(ServletRequest req, ServletResponse res) throws ServletException, IOException

2. service(HttpServletRequest req, HttpServletResponse res):Receives HTTP requests from the public service method and dispatches the request to the doXXX methods defined in this class.
Syntax: protected void service(HttpServletRequest req, HttpServletResponse res) throws ServletException,IOException

3. doGet(HttpServletRequest req, HttpServletResponse res): This method is called by web container for handling GET requests.
Syntax: protected void doGet(HttpServletRequest req, HttpServletResponse res) throws ServletException,IOException

4. doPost(HttpServletRequest req, HttpServletResponse res): This method is called by web container for handling POST requests.
Syntax: protected void doPost(HttpServletRequest req, HttpServletResponse res) throws ServletException,IOException

5. doHead(HttpServletRequest req, HttpServletResponse res): This method is called by web container for handling HEAD requests.
Syntax: protected void doHead(HttpServletRequest req, HttpServletResponse res) throws ServletException,IOException

6. doOptions(HttpServletRequest req, HttpServletResponse res): This method is called by web container for handling OPTIONS requests.
Syntax: protected void doOptions(HttpServletRequest req, HttpServletResponse res) throws ServletException,IOException

7. doPut(HttpServletRequest req, HttpServletResponse res): This method is called by web container for handling PUT requests.
Syntax: protected void doPut(HttpServletRequest req, HttpServletResponse res) throws ServletException,IOException

8. doTrace(HttpServletRequest req, HttpServletResponse res): This method is called by web container for handling TRACE requests.
Syntax: protected void doTrace(HttpServletRequest req, HttpServletResponse res) throws ServletException,IOException

9. doDelete(HttpServletRequest req, HttpServletResponse res): This method is called by web container for handling DELETE requests. 
Syntax: protected void doDelete(HttpServletRequest req, HttpServletResponse res) throws ServletException,IOException

10. getLastModified(HttpServletRequest req): It returns the time the HttpServletRequest  object was last modified since midnight January 1, 1970 GMT. It returns a negative number if time is unknown.
Syntax: protected long getLastModified(HttpServletRequest req)

Example of HttpServlet class

Create a dynamic web project  and then write the code HttpServlet class.
index.html
index<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>HttpServlet Demo</title>
</head>
<body>
<a href="welcome">Click to call Servlet</a>
</body>
</html>

ExampleHttpServlet.java
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
// Creating Http Servlet by Extending HttpServlet class
public class ExampleHttpServlet extends HttpServlet 
{    
    private String mymsg;
    public void init() throws ServletException 
    {      
       mymsg = "Http Servlet Demo";   
    }
    public void doGet(HttpServletRequest request, 
        HttpServletResponse response) throws ServletException, 
        IOException 
    {            
        // Setting up the content type of web page      
        response.setContentType("text/html");
        // Writing the message on the web page      
        PrintWriter out = response.getWriter();      
        out.println("<h1>" + mymsg + "</h1>");      
        out.println("<p>" + "Hello Guys!" + "</p>");   
    }
    public void destroy() 
    {      
       // Leaving empty. Use this if you want to perform  
       //something at the end of Servlet life cycle.   
    }
}

Web.xml

<web-app>
<display-name>Dashzin Servlet</display-name>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
</welcome-file-list>
 
<servlet>
<servlet-name>MyHttpServlet</servlet-name>
<servlet-class>ExampleHttpServlet</servlet-class>
</servlet>
 
<servlet-mapping>
<servlet-name>MyHttpServlet</servlet-name>
<url-pattern>/welcome</url-pattern>
</servlet-mapping>
 
</web-app>

Output

for output and more clarity on the above example just copy and past in your eclipse and run it and check your output. After that try to write the other example so that you will get more confident on HttpServlet Class.









DashZin

Author & Editor

Has laoreet percipitur ad. Vide interesset in mei, no his legimus verterem. Et nostrum imperdiet appellantur usu, mnesarchum referrentur id vim.

0 comments:

Post a Comment