A first servlet

I originally wrote this post in 2010, while I was refreshing my Java EE knowledge reading Head First Servlet and JSP. I worked with Netbeans 6.9, referring to Java 6 and Tomcat 6. It should not be difficult to adapt it to newer technologies.

I was reading the part where the first servlet is created. I didn't write by hand all the required stuff, I used instead the NetBeans support, asking to create a new Web Application project, and using the generated structure.

I have created a project going through File > New Project ... > Java Web > Web Application.

I have right-clicked on the "Source Packages" folder icon and then I have selected the New > Servlet option, putting in the package ch01 my new servlet named SrvHello. I have left all the other option as suggested by default.

This was enough to have a SrvHello.java file almost complete. The SrvlHello class derives from HttpServlet, overrides the public method getServletInfo(). You usually want to modify the default implementation for this method to return a more sensible servlet's description, and the protected methods doGet() and doPost() to refer to a common processRequest() method.

I uncommented the standard code provided by the processRequest() implementation adding a couple of lines, to match the book suggestion to show how it is easy to display the current date-time from the server to the client. Here is the result of my editing:
protected void processRequest(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
    response.setContentType("text/html;charset=UTF-8");
    PrintWriter out = response.getWriter();
    java.util.Date today = new java.util.Date(); // 1
    try {
        out.println("<html>");
        out.println("<head>");
        out.println("<title>Servlet Hello</title>");  
        out.println("</head>");
        out.println("<body>");
        out.println("<h1>Servlet SrvlHello at " + request.getContextPath () + "</h1>");
        out.println(today); // 2
        out.println("</body>");
        out.println("</html>");
    } finally { 
        out.close();
    }
}
1. get the current date-time from system
2. put it in the servlet response

That was very easy, indeed.

Another good thing is that I had not to do anything on the deployment descriptor (usually known with the friendly name of "DD") that we find in the "Web Pages" section of our project, under the folder "WEB-INF", with name web.xml. I didn't change by hand a single character of it, but it is a good idea to have a thorough look at it, since it rules the deployment of my web application:
<?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>SrvlHello</servlet-name>
        <servlet-class>ch01.SrvlHello</servlet-class>
    </servlet>
    <servlet-mapping>
        <servlet-name>SrvlHello</servlet-name>
        <url-pattern>/SrvlHello</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>
We can see how the assigned URL pattern for the servlet is "/SrvlHello", and that we have a pregenerated welcome file named index.jsp; I'm going to add a link in it to our servlet, just one line under the existing title:
<h1>Hello World!</h1>
<a href="./SrvlHello">A simple servlet</a>
The URL servlet, as defined in the DD, is a relative path, that's way I put a dot in front of its name.

And that's it. I now can build and run my project, getting access to the pages generated by Tomcat for my Web Application.

The HTML source generated for the servler would be something like this:
<html>
    <head>
        <title>Servlet Hello</title>
    </head>
    <body>
        <h1>Servlet SrvlHello at /HFSJ</h1>
        Wed Oct 12 23:55:41 CEST 2010
    </body>
</html>
Someone could argue that such a simple result should not require this effort. And he is right.

Actually, in this case, instead of generating an HTML page from Java, we could have work in the other way round, spicing up a bit our HTLM adding some Java. That is the sense of JSP. To get the required result we could add to a jsp page this tag:
<%= new java.util.Date() %>
And, in this case, this looks like the more natural solution.

No comments:

Post a Comment