版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/qq_41690324/article/details/83652101
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>登录页面</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>
<form action="/WebTask/Task/Task07/login-action.jsp" method="post">
用户名<input name="username">(只能由字母组成,3~12位) <br>密码<input
type="password" name="password">(6~12位)<br> <input
type="submit" value="登录">
</form>
<!-获取值,有就输出,没有就为空 --->
${requestScope["kong"] }${requestScope["YWLJ"] } ${requestScope["BPP"] }
</body>
</html>
login-action.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ page import="java.io.*,java.util.*"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>业务逻辑判断</title>
</head>
<body>
<%
//判断是否为空
if (request.getParameter("username").equals("")
&& request.getParameter("password").equals("")) {
//为空就将请求转发到登录页面报错
request.setAttribute("kong", "您输入的内容为空");
request.getRequestDispatcher("/Task/Task07/login.jsp").forward(
request, response);
} else {
//不是空就创建
String username = request.getParameter("username");
String password = request.getParameter("password");
//判断用户名密码是否符合业务逻辑
if (username.matches("[a-zA-Z]{3,12}")
&& password.matches("[a-zA-Z0-9]{6,12}")) {
//判断用户名密码是否正确,正确就跳转到欢迎页面
if (username.equals("tom") && password.equals("123456")) {
out.print("welcome!" + username);
//使用一个指定的名称绑定一个对象到session会话,request也有此方法
session.setAttribute("username", username);
//跳转到欢迎页面
response.sendRedirect("/WebTask/index.jsp");
} else {
//用户名密码不正确时
request.setAttribute("BPP", "您输入的用户名密码不匹配");
request.getRequestDispatcher("/Task/Task07/login.jsp")
.forward(request, response);
}
} else {
//不符合业务逻辑时
request.setAttribute("YWLJ", "请按要求输入用户名和密码");
request.getRequestDispatcher("/Task/Task07/login.jsp")
.forward(request, response);
}
}
%>
</body>
</html>
index.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme() + "://"
+ request.getServerName() + ":" + request.getServerPort()
+ path + "/";
%>
<%!int fontSize = 0;%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>欢迎页面</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">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
</head>
<body>
现在的时间是:
<%=(new java.util.Date()).toLocaleString()%><br>
<%
out.print("<h1>Welcome!" + session.getAttribute("username")+"</h1>");
%>
</body>
</html>
演示