A simple example of a Servlet that displays Japanese

A Servlet that prints the following: servelet_jun25.png

Folder structure Place the following folders under / var / lib / tomcat9 / webapps.

$ tree /var/lib/tomcat9/webapps/hello
/var/lib/tomcat9/webapps/hello
└── WEB-INF
    ├── classes
    │   ├── HelloWorld.class
    │   ├── HelloWorld.java
    │   └── Makefile
    ├── lib
    └── web.xml

HelloWorld.java


import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;

public class HelloWorld extends HttpServlet {
public void doGet(HttpServletRequest request,
HttpServletResponse response)
throws IOException, ServletException
{
	response.setContentType("text/html; charset=UTF-8");
	PrintWriter out = response.getWriter();

	out.println("<!DOCTYPE html>");
	out.println("<html lang=\"ja\">");

	out.println("<head>");
	out.println("<meta http-equiv=\"CONTENT-TYPE\" content=\"text/html; charset=utf-8\" />");
	out.println("</head>");

	out.println("<body>");
	out.println("Hello World!<p />");
	out.println("Good Evening! PM 20:15<p />");
	out.println("tonight<p />");
	out.println("</body>");
	out.println("</html>");
	out.close();
}
}

Makefile


HelloWorld.class: HelloWorld.java
	javac -classpath /usr/share/tomcat9/lib/servlet-api.jar HelloWorld.java
clean:
	rm -f *.class

web.xml


<?xml version="1.0" encoding="ISO-8859-1"?>
<!DOCTYPE web-app
PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
"http://java.sun.com/dtd/web-app_2_3.dtd">

<web-app>
<servlet>
<servlet-name>hello</servlet-name>
<servlet-class>HelloWorld</servlet-class>
</servlet>

<servlet-mapping>
<servlet-name>
hello
</servlet-name>
<url-pattern>
/servlet/hello
</url-pattern>
</servlet-mapping>

</web-app>

compile

Make

Restart tomcat

sudo systemctl restart tomcat9

Recommended Posts

A simple example of a Servlet that displays Japanese
A simple example of an MVC model
[Rails] Volume that displays favorites and a list of favorites
A sample Servlet that downloads Excel.
A memo that containerizes the simple chat application of Node.js + socket.io
A simple sample of ArBiMap (two-way map)
I made a viewer app that displays a PDF
A collection of simple questions for Java beginners
A collection of RSpecs that I used frequently
A description of Interface that is often mistaken
[Ruby] I want to make a program that displays today's day of the week!
I made a Docker image of SDAPS for Japanese
Create a docker image that runs a simple Java app
Implementation example of simple LISP processing system (Java version)
Let's make a robot! "A simple demo of Java AWT Robot"
Builder pattern that forces a set of required properties
Implementation example of simple LISP processing system (Ruby version)