Writing the First Java Servlet
In this tutorial, we will guide you to develop the first Java Servlet and explain Servlet API in details.
Servlet is a pure Java solution to deal with request/response model. A good JSP developer uses servlet for processing business logic. Understanding Servlet is critical to create a maintainable and scalable Java web application. In this tutorial you will learn how to write a simplest Servlet. The goal is understanding the API of the Servlet itself and how to apply it for the complex situation. Here is the source code for the first Java Servlet.
package com.jsptutorial;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class HelloWorldServlet extends HttpServlet {
/**
*
* Processes requests for both HTTP GET & POST methods.
* @param request servlet request
* @param response servlet response
* @throws ServletException if a servlet-specific error occurs
* @throws IOException if an I/O error occurs
*/
protected void processRequest(HttpServletRequest request,
HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html;charset=UTF-8");
PrintWriter out = response.getWriter();
try {
out.println("<html>");
out.println("<head>");
out.println("<title>Servlet HelloWorld</title>");
out.println("</head>");
out.println("<body>");
out.println("<h1>Hello World!</h1>");
out.println("</body>");
out.println("</html>");
} finally {
out.close();
}
}
/**
* Handles the HTTP GET method.
* @param request servlet request
* @param response servlet response
* @throws ServletException if a servlet-specific error occurs
* @throws IOException if an I/O error occurs
*/
@Override
protected void doGet(HttpServletRequest request,
HttpServletResponse response)
throws ServletException, IOException {
processRequest(request, response);
}
/**
* Handles the HTTP POST method.
* @param request servlet request
* @param response servlet response
* @throws ServletException if a servlet-specific error occurs
* @throws IOException if an I/O error occurs
*/
@Override
protected void doPost(HttpServletRequest request,
HttpServletResponse response)
throws ServletException, IOException {
processRequest(request, response);
}
/**
* Returns a short description of the servlet.
* @return a String containing servlet description
*/
@Override
public String getServletInfo() {
return "Hello World Servlet";
}
}
First you notice that we import a bunch of classes for use in our Servlet. Next we create a new class call HelloWorldServlet which extends the HttpServlet class. By doing so we states that our servlet is to deal with HTTP request / response. Inside the HelloWorldServlet class, there is a method called doGet which is used to handle GET HTTP method. There is also another method which is doPost is used to handle POST HTTP method. Those methods call another method call processRequest. in the processRequest method we print out the HTML page we want to response to the web browser.
In the most common IDE such as Eclipse and NetBean, when you create a new Servlet, its automatically create a mapping between Servlet name and URL for you. Here is the deployment descriptor file created by NetBean IDE.
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5"
xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
<servlet>
<servlet-name>HelloWorldServlet</servlet-name>
<servlet-class>com.jsptutorial.HelloWorldServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>HelloWorldServlet</servlet-name>
<url-pattern>/HelloWorld</url-pattern>
</servlet-mapping>
<session-config>
<session-timeout>
30
</session-timeout>
</session-config>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>
As you can see in the servlet mapping section we have servlet name and URL which is mapped to our servlet name. If you call the servlet from the browser as follows:http://localhost:8080/JSPTutorial/HelloWorld The servlet engines will load, initialize and execute the corresponding servlet in the deployment descriptor file which is HelloWorldServlet. Here is the output result:
