Servlet interface provides common behavior to all the servlets.
Servlet interface needs to be implemented for creating any servlet (either directly or indirectly). It provides 3 life cycle methods that are used to initialize the servlet, to service the requests, and to destroy the servlet and 2 non-life cycle methods.
There are 5 methods in Servlet interface. The init, service and destroy are the life cycle methods of servlet. These are invoked by the web container.
Following is the sample source code structure of a servlet example to write Hello World:
// Import required java libraries import java.io.*; import javax.servlet.*; import javax.servlet.http.*; // Extend HttpServlet class public class HelloWorld extends HttpServlet { private String message; public void init() throws ServletException { // Do required initialization message = "Hello World"; } public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // Set response content type response.setContentType("text/html"); // Actual logic goes here. PrintWriter out = response.getWriter(); out.println("<h1>" + message + "</h1>"); } public void destroy() { // do nothing. } }
Let us put above code if HelloWorld.java file and put this file in D:Servlet (Windows) or /usr/Servlet (Unix) then you would need to add these directories as well in CLASSPATH.
Assuming your environment is setup properly, go in Servlet directory and compile HelloWorld.java as follows:
If the servlet depends on any other libraries, you have to include those JAR files on your CLASSPATH as well. I have included only servlet-api.jar JAR file because I’m not using any other library in Hello World program.
If everything goes fine, above compilation would produce HelloWorld.class file in the same directory. Next section would explain how a compiled servlet would be deployed in production.
By default, a servlet application is located at the path C:Program Files (x86)Apache Software FoundationTomcat 7.0webappsROOT and the class file would reside in C:Program Files (x86)Apache Software FoundationTomcat 7.0webappsROOTWEB-INFclasses.
If you have a fully qualified class name of com.dineshonjava.MyServlet, then this servlet class must be located in C:Program Files (x86)Apache Software FoundationTomcat 7.0webappsROOTWEB-INF/classes/com/dineshonjava/MyServlet.class.
For now, let us copy HelloWorld.class into /webapps/ROOT/WEB-INF/classes and create following entries in web.xml file located in C:Program Files (x86)Apache Software FoundationTomcat 7.0webappsROOT/webapps/ROOT/WEB-INF/
<servlet> <servlet-name>HelloWorld</servlet-name> <servlet-class>HelloWorld</servlet-class> </servlet> <servlet-mapping> <servlet-name>HelloWorld</servlet-name> <url-pattern>/HelloWorld</url-pattern> </servlet-mapping>
Now let us start tomcat server using C:Program Files (x86)Apache Software FoundationTomcat 7.0binstartup.bat (on windows) or /bin/startup.sh (on Linux/Solaris etc.) and finally type http://localhost:8080/HelloWorld in browser’s address box. If everything goes fine, you would get following result:
Strategy Design Patterns We can easily create a strategy design pattern using lambda. To implement…
Decorator Pattern A decorator pattern allows a user to add new functionality to an existing…
Delegating pattern In software engineering, the delegation pattern is an object-oriented design pattern that allows…
Technology has emerged a lot in the last decade, and now we have artificial intelligence;…
Managing a database is becoming increasingly complex now due to the vast amount of data…
Overview In this article, we will explore Spring Scheduler how we could use it by…