Create Maven Project
On the Eclipse, create a Maven project
Click Next button to select Workspace Location for project
Click Next button to select Archetype for project
Click Next button and enter Project Information:
- Group Id: LearnJSPServletWithRealApps
- Artifact Id: LearnJSPServletWithRealApps
- Package: com.demo
Click Finish button to finish create Maven project
Configure pom.xml
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>LearnJSPServletWithRealApps</groupId>
<artifactId>LearnJSPServletWithRealApps</artifactId>
<packaging>war</packaging>
<version>0.0.1-SNAPSHOT</version>
<name>Learn JSP-Servlet with Real Apps</name>
<url>http://maven.apache.org</url>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>javax.servlet.jsp.jstl</groupId>
<artifactId>javax.servlet.jsp.jstl-api</artifactId>
<version>1.2.1</version>
</dependency>
<dependency>
<groupId>taglibs</groupId>
<artifactId>standard</artifactId>
<version>1.1.2</version>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>3.1.0</version>
<scope>provided</scope>
</dependency>
</dependencies>
<build>
<finalName>LearnJSPServletWithRealApps</finalName>
</build>
</project>
Configure web.xml
<!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>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>
Redirect Page
Create new JSP file named index.jsp in src\main\webapp folder.
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1" isELIgnored="false"%>
<html>
<head>
<title>Demo Page</title>
</head>
<body>
<h3>Query String</h3>
<a href="${pageContext.request.contextPath }/demo?action=demo1&id=123">Single Parameter</a> |
<a href="${pageContext.request.contextPath }/demo?action=demo2&id=123&username=acc1">Multiple Parameters</a> |
<a href="${pageContext.request.contextPath }/demo?action=demo3&id=123&username=acc1">Get All Multiple Parameters</a>
</body>
</html>
Create Servlet
Create new package named com.demo.servlets. In this package, create new Servlet named DemoServlet as below:
package com.demo.servlets;
import java.io.IOException;
import java.util.Enumeration;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
@WebServlet("/demo")
public class DemoServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
public DemoServlet() {
super();
}
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
String action = request.getParameter("action");
if (action.equalsIgnoreCase("demo1")) {
doGet_Demo1(request, response);
} else if (action.equalsIgnoreCase("demo2")) {
doGet_Demo2(request, response);
} else if (action.equalsIgnoreCase("demo3")) {
doGet_Demo3(request, response);
}
}
protected void doGet_Demo1(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
int id = Integer.parseInt(request.getParameter("id"));
request.setAttribute("id", id);
request.getRequestDispatcher("demo/result1.jsp").forward(request, response);
}
protected void doGet_Demo2(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
int id = Integer.parseInt(request.getParameter("id"));
String username = request.getParameter("username");
request.setAttribute("id", id);
request.setAttribute("username", username);
request.getRequestDispatcher("demo/result2.jsp").forward(request, response);
}
protected void doGet_Demo3(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
Enumeration<String> parameterNames = request.getParameterNames();
while (parameterNames.hasMoreElements()) {
String paramName = parameterNames.nextElement();
request.setAttribute(paramName, request.getParameter(paramName));
}
request.getRequestDispatcher("demo/result3.jsp").forward(request, response);
}
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
}
}
Create JSP Pages
Create new folder named demo in src\main\webapp folder. In this folder, create new JSP files as below:
Result 1 Page
In src\main\webapp\demo folder, create new JSP file named result1.jsp as below:
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1" isELIgnored="false"%>
<h3>Single Parameter</h3>
Id: ${id }
Result 2 Page
In src\main\webapp\demo folder, create new JSP file named result2.jsp as below:
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1" isELIgnored="false"%>
<h3>Multiple Parameters</h3>
Id: ${id }
<br>
Username: ${username }
Result 3 Page
In src\main\webapp\demo folder, create new JSP file named result3.jsp as below:
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1" isELIgnored="false"%>
<h3>Get All Multiple Parameters</h3>
Action: ${action }
<br>
Id: ${id }
<br>
Username: ${username }
Structure of Maven Project
Run Application
Select project, right click and select Run As/Run on Server menu
Access DemoServlet with following urls as below:
- http://localhost:8081/LearnJSPServletWithRealApps
- http://localhost:8081/LearnJSPServletWithRealApps/demo
Output
Click Single Parameter link:
Output
Click Multiple Parameters link:
Output
Click Get All Multiple Parameters link:
Output