×
DocsConsole

DocsConsole

Different Ways To Create Servlet

By | | Updated : 2019-04-11 | Viewed : 224 times

Ways of Servlet Implementation

    There are 3 ways of servlet implementations are there.

  • By Using Servlet

  • By Using GenericServlet

  • By Using HttpServlet

By Using Servlet:

This is first type of approach to implement the Servlet. When we want to implement the Servlet application, we can use the javax.servlet.Servlet interface. This is basic interface for all Servlet. Each Servlet should implement javax.servlet.Servlet interface directly or indirectly. In this implementation, we have one challenge is here. We have to implement the all methods of javax.servlet.Servlet interface.

Please see the below given implementation for javax.servlet.Servlet interface.

HelloWorldServlet .java
public class HelloWorldServlet implements Servlet {

	@Override
	public void init(ServletConfig config) throws ServletException {

	}

	@Override
	public ServletConfig getServletConfig() {
		return null;
	}

	@Override
	public void service(ServletRequest req, ServletResponse res) throws ServletException, IOException {

	}

	@Override
	public String getServletInfo() {
		return null;
	}

	@Override
	public void destroy() {

	}

}

After instantiation of Servlet, ServletConfig object should be created. ServletRequest and ServletResponse should be initialized for wrapping the request and response objects. It is very difficult to instantiate all the above given three class objects. As Servlet Container will take care about Servlet life cycle, Container will call and create objects for these classes. So, in the Servlet API we have one more option called GenericeServlet. It is subclass of Servlet. Let’s try example with GenericServlet.

By Using GenericServlet

Here it is simplified the implementation approach. Here Developer no need to implement all the methods. Service method implementation is enough for providing the response. Please see the below given example.

HelloWorldServlet.java
public class HelloWorldServlet extends GenericServlet {


	@Override
	public void service(ServletRequest req, ServletResponse res) throws ServletException, IOException {

		res.setContentType("text/html");
		PrintWriter out = res.getWriter();
		out.print("<html><body><div style="padding-left: 50px; padding-top: 50px;">");
		out.print("<b style="color: #5337c4;">HelloWorldServlet Example</b>");
		out.print("</div></body></html>");

	}
}

Here we have implemented the service method. It will be executed with irrespective of request type. Here Request type means Get, post, put and so on. Due this problem, we cannot invoke particular methods with respect to the request types.

By Using HttpServlet

Here we can implement method with respect to the request type. Please observe the below given example.

HelloWorldServlet.java
public class HelloWorldServlet extends HttpServlet {

	protected void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		System.out.println("Hello World");
	}

	protected void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
	}
}

Here we have implemented doGet() method, which will be executed with respect to Get request type. When we fire the Get type of request from the client then doGet() method will be executed. Please find examples zip given below.

Leave A Reply