Servlets & Java Server Pages
last modified January 27, 2024
A servlet is a Java Web component that generates dynamic content. Servlets are managed by containers like Jetty or Tomcat; they are classes used to build web applications based on request-response programming model. Java Server Pages (JSP) technology allows us to easily create web content that has both static and dynamic components. JSPs are compiled into servlets by a JSP compiler.
This chapter will show how to set up servlets and Java Server Pages in Jetty.
Processing a POST request
An HTTP POST request sends data from a client to a server. An HTML form sends data to server using the POST method.
In our example a servlet processes the data sent by the client. It retrieves the values and sends it back to the client.
tree
.
├── build.xml
└── src
├── com
│ └── zetcode
│ └── MyServlet.java
└── web
├── index.html
└── WEB-INF
└── web.xml
5 directories, 4 files
This is how our project directory looks like.
<!DOCTYPE html> <html> <body> <form id="contact" method="post" action="process.do"> <label for="name">Name:</label> <input type="text" name="name"> <br> <label for="age">Age:</label> <input type="text" name="age"> <input type="submit" value="Submit"> </form> </body> </html>
The index.html file contains an HTML form. It has
two input tags to get data from the user. The values can be retrieved
later from the request parameters whose names match the input's name
attributes. The action attribute of the form tag
provides a URL pattern that is mapped to a specific servlet to
process.
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee
http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
version="3.1">
</web-app>
The web.xml is empty. The mapping of the URL to
the servlet is created using an annotation. Actually, the file
is not needed in our example. However, since not all features
can be replaced by annotations, the web.xml file
is included in larger projects.
package com.zetcode;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.annotation.WebServlet;
@WebServlet(urlPatterns = "/process.do")
public class MyServlet extends HttpServlet {
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/plain");
response.setStatus(HttpServletResponse.SC_OK);
response.getWriter().format("Name: %s%n", request.getParameter("name"));
response.getWriter().format("Age: %s%n", request.getParameter("age"));
}
}
This is the code of our processing servlet.
@WebServlet(urlPatterns = "/process.do")
The WebServlet annotation maps com.zetcode.MyServlet
to the /process.do URL pattern.
response.getWriter().format("Name: %s%n", request.getParameter("name"));
We retrieve the name value from the request object using the getParameter
method. The name parameter corresponds to the name attribute of the input tag.
<?xml version="1.0" encoding="UTF-8"?>
<project name="ProcessForm" default="compile">
<property name="name" value="myform"/>
<property environment="env"/>
<property name="src.dir" value="src"/>
<property name="web.dir" value="${src.dir}/web"/>
<property name="build.dir" location="${web.dir}/WEB-INF/classes"/>
<property name="jetty.lib.dir" location="${env.JETTY_HOME}/lib"/>
<property name="dist.dir" location="dist"/>
<property name="deploy.path" location="${env.JETTY_BASE}/webapps"/>
<path id="compile.classpath">
<fileset dir="${jetty.lib.dir}"/>
</path>
<target name="init">
<mkdir dir="${build.dir}"/>
<mkdir dir="${dist.dir}"/>
</target>
<target name="compile" depends="init">
<javac srcdir="${src.dir}" destdir="${build.dir}"
includeantruntime="false">
<classpath refid="compile.classpath"/>
</javac>
<echo>Compilation completed</echo>
</target>
<target name="archive" depends="compile">
<war destfile="${dist.dir}/${name}.war"
webxml="${web.dir}/WEB-INF/web.xml">
<fileset dir="${web.dir}"/>
</war>
<echo>Archive created</echo>
</target>
<target name="clean" depends="init">
<delete dir="${build.dir}"/>
<delete dir="${dist.dir}"/>
<echo>Cleaning completed</echo>
</target>
<target name="deploy" depends="archive">
<copy file="${dist.dir}/${name}.war" overwrite="true"
todir="${deploy.path}"/>
<echo>Archive deployed</echo>
</target>
</project>
This is the Ant build file.
$ curl --data "name=Robert&age=33" localhost:8080/myform/process.do Name: Robert Age: 33
We do a POST request with the curl tool and the servlet responds
with these two lines.
Custom 404 error page
This example sets up a custom JSP page for displaying an HTTP 404 error.
The 404 or Not Found error message is a HTTP standard response code indicating
that the client was able to communicate with a given server, but the server could
not find what was requested.
$ tree
.
├── build.xml
└── src
├── com
│ └── zetcode
│ └── MyServlet.java
└── web
├── error404.jsp
└── WEB-INF
└── web.xml
5 directories, 4 files
These are the contents of the project directory.
<%@ page language="java" isErrorPage="true" contentType="text/html;
charset=UTF-8" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<title>Error page</title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
</head>
<body>
<button onclick="history.back()">Back to Previous Page</button>
<h1>404 Page Not Found.</h1>
<p><b>Error code:</b> ${pageContext.errorData.statusCode}</p>
<p><b>Request URI:</b>
${pageContext.request.scheme}://${header.host}${pageContext.errorData.requestURI}
</p>
</body>
</html>
This JSP page will be displayed if Jetty cannot find a requested page.
package com.zetcode;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class MyServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/plain");
response.setStatus(HttpServletResponse.SC_OK);
response.getWriter().println("MyServlet called");
}
}
A simple servlet is created. The servlet sends a plain message back to the client.
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee
http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
version="3.1">
<display-name>MyServlet</display-name>
<servlet id="jsp">
<servlet-name>jsp</servlet-name>
<servlet-class>org.apache.jasper.servlet.JspServlet</servlet-class>
<init-param>
<param-name>fork</param-name>
<param-value>false</param-value>
</init-param>
<init-param>
<param-name>keepgenerated</param-name>
<param-value>true</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet>
<servlet-name>MyServlet</servlet-name>
<servlet-class>com.zetcode.MyServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>MyServlet</servlet-name>
<url-pattern>/myservlet.do</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>jsp</servlet-name>
<url-pattern>*.jsp</url-pattern>
</servlet-mapping>
<error-page>
<error-code>404</error-code>
<location>/error404.jsp</location>
</error-page>
</web-app>
The web.xml file registers a JspServlet which
handles JSP pages, registers our MyServlet, and maps
the error404.jsp file to the 404 error.
<?xml version="1.0" encoding="UTF-8"?>
<project name="CustomErrorPage" default="compile">
<property name="name" value="customerror"/>
<property environment="env"/>
<property name="src.dir" value="src"/>
<property name="web.dir" value="${src.dir}/web"/>
<property name="build.dir" location="${web.dir}/WEB-INF/classes"/>
<property name="lib.dir" location="${env.JETTY_HOME}/lib"/>
<property name="dist.dir" location="dist"/>
<property name="deploy.path" location="${env.JETTY_BASE}/webapps"/>
<path id="compile.classpath">
<fileset dir="${lib.dir}"/>
</path>
<target name="init">
<mkdir dir="${build.dir}"/>
<mkdir dir="${dist.dir}"/>
</target>
<target name="compile" depends="init">
<javac srcdir="${src.dir}" destdir="${build.dir}"
includeantruntime="false">
<classpath refid="compile.classpath"/>
</javac>
<echo>Compilation completed</echo>
</target>
<target name="archive" depends="compile">
<war destfile="${dist.dir}/${name}.war"
webxml="${web.dir}/WEB-INF/web.xml">
<fileset dir="${web.dir}"/>
</war>
<echo>Archive created</echo>
</target>
<target name="clean" depends="init">
<delete dir="${build.dir}"/>
<delete dir="${dist.dir}"/>
<echo>Cleaning completed</echo>
</target>
<target name="deploy" depends="archive">
<copy file="${dist.dir}/${name}.war" overwrite="true"
todir="${deploy.path}"/>
<echo>Archive deployed</echo>
</target>
</project>
This is the Ant build file.
$ java -jar $JETTY_HOME/start.jar --add-to-start=http,deploy,jsp,jstl,annotations
These modules must be enabled in the Jetty base.
$ java -jar $JETTY_HOME/start.jar $ curl localhost:8080/customerror/myservlet.do MyServlet called
We start Jetty and make a valid request.
$ curl localhost:8080/customerror/servlet.do
<!DOCTYPE html>
<html>
<head>
<title>Error page</title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
</head>
<body>
<button onclick="history.back()">Back to Previous Page</button>
<h1>404 Page Not Found.</h1>
<p><b>Error code:</b> 404</p>
<p><b>Request URI:</b> http://localhost:8080/customerror/servlet.do</p>
</body>
</html>
Trying to reach a non-existing resource causes Jetty to send our
custom 404 error page.
In this chapter of the Jetty tutorial, we have worked with Java servlets and JSPs.
Author
List all Java tutorials.