Servlets - SPLessons
SPLessons 5 Steps, 3 Clicks
5 Steps - 3 Clicks

Http Servlets

Http Servlets

shape Description

Http Servlets, The HttpServlet class peruses the HTTP request, and figures out whether the request is a HTTP GET, POST, PUT, DELETE, HEAD and so on and calls one the relating method.When the Servlet is deployed in the server the Servlet container creates life cycle of the Servlet. The central abstraction in the Servlet API is the Servlet interface, all the Servlets have to implement this interface either directly or by extending a class such as GenericServlet, HttpServlet. The HttpServlet class extends the GenericServlet class and implements Serialize interface. Http servlet class provides methods such as doGet, doPost. For more detailed overview on GenericServlet Click Here

shape Methods of HttpServlet Class

Following are the regular using servlet methods.

shape Example

Following is an example which describes how HttpServlet Class will use GenericServlet. First.java [java]package servletinterface; import java.io.*; import javax.servlet.*; public class First implements Servlet{ ServletConfig config=null; public void init(ServletConfig config){ this.config=config; System.out.println("servlet is initialized"); } public void service(ServletRequest req,ServletResponse res) throws IOException,ServletException{ res.setContentType("text/html"); PrintWriter out=res.getWriter(); out.print(""); out.print("<b>Hey man, welcome to SPLesons.</b>"); out.print(""); } public void destroy(){System.out.println("servlet is destroyed");} public ServletConfig getServletConfig(){return config;} public String getServletInfo(){return "copyright 2007-1010";} }[/java] Where First is the class and that is implementing Servlet, where init() method has been used once all over the code, Class java.io.PrintWriter. Print formatted representations of objects to a text-output stream, implements all of the print methods found in PrintStream. out.println() will be used which means to print the data in browser. web.xml [java] <web-app> <servlet> <servlet-name>s1</servlet-name> <servlet-class>servletinterface.First</servlet-class> </servlet> <servlet-mapping> <servlet-name>s1</servlet-name> <url-pattern>/hello</url-pattern> </servlet-mapping> </web-app>[/java] Following is the sample structure of the web.xml file. index.html [java] <center><a href="./hello">Invoke Generic Servlet</a></center> [/java] Output When compile the program following is the output will be displayed. When click on the link following message will be opened.

GenericServlet vs HttpServlet

shape Description

The following are the some important differences between GenericServlet vs HttpServlet.
  • GenericServlet gives abstract service(ServletRequest, ServletResponse) technique to actualize which gets called by container at whatever point it gets request for handling, On the other hand HttpServlet supersedes servicestrategy and gives callback on doGet and doPost at whatever point it gets HTTP request for from GET or POST strategy. It additionally gives a few other technique in light of different HTTP methods for sending request.
  • Generic servlet gives a easy approach to expand Servlet, its enough to supersede benefit technique to actualize GenericServlet. Aside from expanding Servlet interface, it additionally executes ServletConfig interface and gives approach to acknowledge introduction parameter go to Servlet from web.xml e.g. by utilizing getInitParamter().
  • Click Here To More About GenericServlet

    Summary

    shape Key Points

    • Servlet creates a thread in which thread takes only HTTP Request and Response is processed.
    • GET request will be handled by doGet() method.
    • doGet() and doPost() will be invoked by web container.