需求:(1)使用Session进行用户名和密码的验证。
(2)登录成功跳转到带有用户信息的成功页面1。
(3)登录失败提示用户名或密码哪个错误。
(4)登录成功的页面中提示有超链接 ,跳转到成功页面2。
(5)如果未登录成功手动跳转成功页面1或2,则自动跳转
到登录的页面,并提示登录。
业务逻辑图
login.jsp
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort()
+ path + "/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>My JSP 'index.jsp' starting page</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<style type="text/css">
* {
padding: 0px;
margin: 0px;
text-align: center;
margin-top: 20px;
}
</style>
<body>
<form action="<%=basePath%>LoginServlet" method="post">
用户名:<input type="text" name="username"><br> 密码:<input
type="text" name="password"><br> <input type="submit"
value="提交">
</form>
<%
int index = 0;
String err_info = (String) request.getAttribute("err_info");
if (err_info == null) {
err_info = "";
}
Enumeration enumeration = session.getAttributeNames();
while (enumeration.hasMoreElements()) {
String name = enumeration.nextElement().toString();
String value = (String) session.getAttribute(name);
if (value.equals("wang") || value.equals("121")) {
index++;
}
}
if (index == 2) {
request.getRequestDispatcher("/succ1.jsp").forward(request, response);
}
%>
<%=err_info%>
</body>
</html>
succ1.jsp
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort()
+ path + "/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>page2</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
</head>
<body>
<h1>这里是登录的成功的页面1</h1>
<%
//判断是是否有 用户登录后保存的session
String username = (String) session.getAttribute("username");
if (username == null) {
//如果没有 提供 错误的信息“未登录”,并重定向到login.jsp
request.setAttribute("err_info", "请登录!");
request.getRequestDispatcher("/login.jsp").forward(request, response);
}
%>
你的名字是:<%=username%>
<br>
<a href="<%=basePath%>succ2.jsp">点击跳转到succ2.jsp</a>
</body>
</html>
succ2.jsp
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort()
+ path + "/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>My JSP 'b.jsp' starting page</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
</head>
<body>
<%
//判断是是否有 用户登录后保存的session
String username = (String) session.getAttribute("username");
if (username == null) {
//如果没有 提供 错误的信息“未登录”,并重定向到login.jsp
request.setAttribute("err_info", "请登录!");
request.getRequestDispatcher("/login.jsp").forward(request, response);
}
%>
<h1>这里是登录成功的页面2</h1>
</body>
</html>
LoginServlet.java
package com.wang.servlet;
import java.io.IOException;
import java.io.PrintWriter;
import javax.jms.Session;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
public class LoginServlet extends HttpServlet {
public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
final String Myname = "wang";
final String Mypassword = "121";
String name = request.getParameter("username");
String password = request.getParameter("password");
if (name.equals(Myname) && password.equals(Mypassword)) {
// 保存session并,转发到成功的页面
HttpSession session = request.getSession();
session.setAttribute("username", "wang");// 保存session
session.setAttribute("password", "121");
// 重定向
request.getRequestDispatcher("/succ1.jsp").forward(request, response);
} else {
String err_info_1 = "";
String err_info_2 = "";
if (!name.equals(Myname)) {
err_info_1 = "用户名错误!";
}
if (!password.equals(Mypassword)) {
err_info_2 = "密码错误!";
}
String err_info_ouput = err_info_1 + err_info_2;
// 重定向到login.jsp 并使用request传递错误 的信息
request.setAttribute("err_info", err_info_ouput);
request.getRequestDispatcher("/login.jsp").forward(request, response);
}
}
}
这个是最初的书写 版本,虽然各功能都已经实现了,但是代码有点混乱,后续会继续优化的。