创建 Maven 项目
在 Eclipse 上,创建一个Maven项目
单击下一步按钮以选择项目的工作区位置
单击下一步按钮为项目选择Archetype
单击下一步按钮并输入项目信息:
- 组 ID:LearnJSPServletWithRealApps
- 工件 ID:LearnJSPServletWithRealApps
- 包:com.demo
点击Finish按钮完成创建 Maven 项目
配置 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>
配置 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>
重定向页面
在src\main\webapp文件夹中创建名为index.jsp的新 JSP 文件。该文件将重定向到AccountServlet,如下所示:
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<jsp:forward page="account"></jsp:forward>
创建 Servlet
创建名为com.demo.servlets的新包。在这个包中,创建一个名为AccountServlet的新Servlet,如下所示:
package com.demo.servlets;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
@WebServlet("/account")
public class AccountServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
public AccountServlet() {
super();
}
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
HttpSession session = request.getSession();
String action = request.getParameter("action");
if (action == null) {
request.getRequestDispatcher("account/index.jsp").forward(request, response);
} else {
if (action.equalsIgnoreCase("logout")) {
session.removeAttribute("username");
response.sendRedirect("account");
}
}
}
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
HttpSession session = request.getSession();
String username = request.getParameter("username").trim();
String password = request.getParameter("password").trim();
if (username.equalsIgnoreCase("acc1") && password.equalsIgnoreCase("123")) {
session.setAttribute("username", username);
request.getRequestDispatcher("account/welcome.jsp").forward(request, response);
} else {
request.setAttribute("error", "Invalid Account");
request.getRequestDispatcher("account/index.jsp").forward(request, response);
}
}
}
创建 JSP 页面
在src\main\webapp文件夹中创建名为account的新文件夹。在此文件夹中,创建新的JSP文件,如下所示:
索引 JSP
在src\main\webapp\account文件夹中,创建名为index.jsp的新JSP文件,如下所示:
<%@ 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>Login Page</h3>
${error }
<form method="post" action="${pageContext.request.contextPath }/account">
<table border="0" cellpadding="2" cellspacing="2">
<tr>
<td>Username</td>
<td><input type="text" name="username"></td>
</tr>
<tr>
<td>Password</td>
<td><input type="password" name="password"></td>
</tr>
<tr>
<td> </td>
<td><input type="submit" value="Login"></td>
</tr>
</table>
</form>
</body>
</html>
欢迎 JSP
在src\main\webapp\account文件夹中,创建名为welcome.jsp的新JSP文件,如下所示:
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1" isELIgnored="false"%>
<html>
<head>
<title>Welcome Page</title>
</head>
<body>
<h3>Welcome Page</h3>
Welcome ${sessionScope.username }
<br>
<a href="${pageContext.request.contextPath }/account?action=logout">Logout</a>
</body>
</html>
Maven项目的结构
运行应用程序
选择项目,右键单击并选择Run As/Run on Server菜单
使用以下 url访问DemoServlet :
- http://localhost:8081/LearnJSPServletWithRealApps
- http://localhost:8081/LearnJSPServletWithRealApps/account
输出
使用无效帐户进行测试是用户名:abc和密码:123
输出
使用有效帐户进行测试是username: acc1和password: 123。
输出
单击欢迎页面中的注销链接以删除会话并再次打开登录页面
输出