JSP/Servlet | Servlet | Creating a Simple Servlet
Now let’s actually create a very basic servlet. Below are sample servlet code and an example of registering it in the web.xml file.
MyGaeAppServlet.java
package com.devkuma.mygaeapp;
import java.io.*;
import javax.servlet.http.*;
@SuppressWarnings("serial")
public class MyGaeAppServlet extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException {
response.setContentType("text/html");
request.setCharacterEncoding("utf8");
response.setCharacterEncoding("utf8");
PrintWriter out = response.getWriter();
out.println("<html><head></head><body>");
out.println("<h1>Hello, world</h1><p>this is sample servlet.</p>");
out.println("</body></html>");
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws IOException {
// not use.
}
}
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>MyGaeApp</servlet-name>
<servlet-class>com.devkuma.mygaeapp.MyGaeAppServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>MyGaeApp</servlet-name>
<url-pattern>/mygaeapp</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
</welcome-file-list>
</web-app>
When you create a GAE project, a source code file for the MyGaeAppServlet class should be generated by default. It is in the project’s src folder. Use this as it is.
If you want to create a new servlet, select the [File]-[New]-[Class] menu and create a new class source code file in the project’s src folder.
Here, simple output sample code is written in the doGet method of the MyGaeAppServlet class. doPost exists only as a method and has no content. When it is complete, deploy it to GAE and access the following address.
http://{application name}.appspot.com/mygaeapp
It only displays simple text, but if it displays correctly, that is enough. It is very simple, but with this you have learned the basics from writing servlet code and settings to deploying it.
Basic servlet processing to remember
This doGet consists only of processing that is almost always used in servlets. These should be remembered first as basic servlet processing. Let’s summarize them in order.
Setting the content type
response.setContentType(type);
When displaying some result on the client side, you need to set what type of content it is. Otherwise, the browser cannot know whether it can display that content. The setContentType method of HttpServletResponse performs this. Specify text representing the content type as the argument. First, remember these two.
- For HTML: “text/html”
- For plain text: “text/plain”
Encoding settings
request.setCharacterEncoding(encodingName);
response.setCharacterEncoding(encodingName);
When exchanging information with a client, the basic form is text. If the encoding of that text is not known, characters may become garbled. These two statements configure that setting.
setCharacterEncoding sets the encoding specified as its argument. It is provided by both HttpServletRequest and HttpServletResponse. HttpServletRequest is for text encoding sent from a browser and similar clients, and HttpServletResponse is for encoding when sending text to the client. In GAE, UTF-8 is the default, so setting both to “utf8” is fine.
Getting a PrintWriter
PrintWriter out = response.getWriter();
Output to the client, meaning sending something to the browser, uses a class named PrintWriter. You can obtain an instance by calling the getWriter method of HttpServletResponse. Call methods on this PrintWriter to output content.
Output to the client
out.println(outputValue);
The method used to output values with PrintWriter is println. It sends the value specified as its argument to the client. It is overloaded, so you can specify many kinds of values as the argument.
Here, HTML source code is written. If you want to send back an HTML page, write HTML tags in println like this.
Now you know how to create a simple servlet. The basics covered here are essential knowledge for servlet development. Next, we will create more concrete servlets, so be sure to remember this much.