JSP/Servlet | Servlet | Basic Servlet Code
Now let’s look at servlet basics. A servlet is basically a Java class. This may sound obvious, but it simply means that being able to write Java classes is enough. There is nothing especially mysterious about it.
Let’s see how a servlet class is written. The basic code is as follows.
import javax.servlet.http.*;
@SuppressWarnings("serial")
public class ClassName extends HttpServlet {
public void doGet(HttpServletRequest request,
HttpServletResponse response)
throws IOException {
...write GET processing here...
}
public void doPost(HttpServletRequest request,
HttpServletResponse response)
throws IOException {
...write POST processing here...
}
}
This is the basic structure of servlet code. Let’s summarize the points.
-
The class extends HttpServlet.
The base of a servlet is the HttpServlet class provided in the javax.servlet.http package. A servlet is created by extending this class. -
The basics are the doGet and doPost methods.
The almost essential methods provided in the class are doGet and doPost. They are executed when accessed with the HTTP methods GET and POST respectively. One or both of these methods will usually be prepared. Since either method can throw IOException, addthrows IOException. -
HttpServletRequest manages request information.
Two important objects are passed to these methods as arguments. One is HttpServletRequest. This object manages request information, that is, information from when the client accesses the servlet. Various information about the request can be obtained by calling methods on this object. -
HttpServletResponse manages the response.
The other object is HttpServletResponse. This object manages response information, the information returned from the servlet to the client. Output to the client is also written by obtaining a PrintWriter from this HttpServletResponse.
Servlet 3.0 annotations
In addition, when using Servlet 3.0, you can specify the public address of the servlet with an annotation. Write it at the class declaration area, where @SuppressWarnings is located.
@WebServlet(publicAddress)
It looks like this. For example, if you publish it at an address such as http://OO/sample, write @WebServlet("/sample"). With this, the servlet is automatically published at the specified address.
In 2.5, annotations cannot be used. You must specify a separate configuration file. This will be explained next.
Registering servlet information in web.xml
If you are using Servlet 3.0, servlet creation is complete with this. After that, you only need to deploy it. However, in environments that do not yet support 3.0, such as GAE, you must prepare a configuration file describing servlet information in addition to the servlet source code itself.
That file is web.xml. It is generally prepared in the WEB-INF folder of the web application, a special folder that cannot be accessed from outside. In a GAE project, it is usually generated automatically.
This web.xml contains various configuration information for the web application. For a servlet, information such as the servlet name, the class to use, and the public address must be written here.
The basic form of servlet-related information is summarized below.
Basic form of web.xml
<?xml version="1.0" encoding="utf-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" version="2.5">
<servlet>
<servlet-name>servlet name</servlet-name>
<servlet-class>servlet class</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>servlet name</servlet-name>
<url-pattern>public address</url-pattern>
</servlet-mapping>
...other tags continue...
</web-app>
In web.xml, write configuration tags inside the root tag <web-app>. Servlet information is provided with the following two tags.
<servlet> tag
This is for registering the servlet. Inside it, the following two tags are prepared. Write the servlet class with the package included, not just the class name.
- <servlet-name> registers the servlet name.
- <servlet-class> describes the servlet class to use.
<servlet-mapping> tag
This is for configuring servlet URL mapping, or which address it is published at. The following two tags are prepared inside it. With this, the servlet can be published at the specified address.
- <servlet-name> specifies the servlet name.
- <url-pattern> describes the public address.
The servlet class created by these tags is published at the specified address. As explained several times, this is needed for versions before Servlet 3.0. In 3.0 and later, it is not needed if you specify the address with annotations.
Incidentally, web.xml also has a tag named <welcome-file-list>. This specifies which file inside a directory is displayed when the URL path, or file name, is not specified. Even if it is absent, it does not otherwise affect the web application.