没有思路了,不知道该怎么办
问题描述:
相关代码:
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>用户登录</title>
<script type="text/javascript" src="js/jquery-3.3.1.min.js"></script>
</head>
<body>相关代码:
<div id="login_h">
<div id="login_h_title">
<h1>系统登录</h1>
</div>
<div id="login_body">
<form id="body_form" action="http://localhost:8080/user-login-project/servlet/login" method="get">
<p>用户名:</p>
<input type="text" id="userName" name="userName" placeholder="请输入中文/字母/数字的用户名【4-7位】"/>
<p>密码:</p>
<input type="password" id="pwd" name="pwd" placeholder="字母开头,数字字母6-10位组成"/><br>
<input type="submit" id="sub" name="sub" value="登录">
</form>
</div>
</div>
<script type="text/javascript">
$.ajax({
"url":"http://localhost:8080/user-login-project/servlet/login",
"type":"get",
"dataType":"json",
"success":function(json){
}
})
/* 给表单添加提交事件 */
/* 给相应的输入框添加正则表达式,限制输入内容 */
document.getElementById("body_form").onsubmit=function(){
var regexUserName=/^[\u4e00-\u9fa5A-Za-z1-9]{1}[\u4e00-\u9fa5A-Za-z0-9]{3,6}$/;
var regexPwd=/^[A-Za-z]{1}[0-9A-Za-z]{5,9}$/;
var userName=document.getElementById("userName").value;
var pwd=document.getElementById("pwd").value;
if(regexUserName.test(userName)==false){
alert("用户名不符合当前格式!");
return false;
}else if(regexPwd.test(pwd)==false){
alert("密码不不符合格式");
return false;
}else{
alert("校验成功!");
}
}
</script>
</body>
</html>
相关代码:
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<!DOCTYPE html>
<html>
<script type="text/javascript" src="js/jquery-3.3.1.min.js"></script>
<head>
<script type="text/javascript">
</script>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<h1 style="color:red;">您好,${sessionScope.loginMap},您已登录!</h1>
</body>
</html>
相关代码:
package com.roan.login;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;
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;
/**
* Servlet implementation class LoginServlet
*/
@WebServlet("/servlet/login")
public class LoginServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
private static Map<String, String> loginMap = new HashMap<>();
/**
* @see HttpServlet#HttpServlet()
*/
public LoginServlet() {
super();
// TODO Auto-generated constructor stub
}
/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
* response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
// 创建用户会话对象,存储用户信息
HttpSession session = request.getSession();
// 获取login界面传来的用户名密码参数
String name = request.getParameter("userName");
String pwd = request.getParameter("pwd");
// 判断集合是否为空
if (loginMap.size() != 0) {
// 遍历用户信息
Set<Entry<String, String>> entrySet = loginMap.entrySet();
for (Entry<String, String> entry : entrySet) {
if (entry.getKey().equals(name)) {
loginMap.remove(entry.getKey());
request.getSession().removeAttribute(entry.getKey());
request.getSession().removeAttribute(entry.getValue());
loginMap.put(name, pwd);
} else {
loginMap.put(name, pwd);
request.getSession().setAttribute("name", name);
request.getSession().setAttribute("pwd", pwd);
}
}
}else{
loginMap.put(name, pwd);
request.getSession().setAttribute("name", name);
request.getSession().setAttribute("pwd", pwd);
}
// 发送请求,将该请求下发到登录成功页面
request.getRequestDispatcher("/login-success.jsp").forward(request, response);
}
}
相关代码:
package com.roan.login;
import javax.servlet.http.HttpSession;
import javax.servlet.http.HttpSessionAttributeListener;
import javax.servlet.http.HttpSessionBindingEvent;
import javax.servlet.http.HttpSessionEvent;
public class SessionAttributeListener implements HttpSessionAttributeListener{
private static HttpSession session=null;
@Override
public void attributeAdded(HttpSessionBindingEvent event) {
// 初始化
session=event.getSession();
String name=(String) session.getAttribute("name");
String pwd=(String) session.getAttribute("pwd");
System.out.println("[name="+name+"-pwd="+pwd+"]");
}
@Override
public void attributeRemoved(HttpSessionBindingEvent event) {
System.out.println("删除属性");
String name=(String) session.getAttribute("name");
String pwd=(String) session.getAttribute("pwd");
System.out.println("[name="+name+"-pwd="+pwd+"]");
}
@Override
public void attributeReplaced(HttpSessionBindingEvent event) {
System.out.println("新增属性");
String name=(String) session.getAttribute("name");
String pwd=(String) session.getAttribute("pwd");
System.out.println("[name="+name+"-pwd="+pwd+"]");
}
}
相关代码:
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" id="WebApp_ID" version="3.1">
<display-name>user-login-project</display-name>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.htm</welcome-file>
<welcome-file>default.jsp</welcome-file>
</welcome-file-list>
<listener>
<listener-class>com.roan.login.SessionAttributeListener</listener-class>
</listener>
</web-app>
70
收起
正在回答 回答被采纳积分+1
1回答
java工程师2020版
- 参与学习 人
- 提交作业 9400 份
- 解答问题 16556 个
综合就业常年第一,编程排行常年霸榜,无需脱产即可学习,北上广深月薪过万 无论你是未就业的学生还是想转行的在职人员,不需要基础,只要你有梦想,想高薪
了解课程
恭喜解决一个难题,获得1积分~
来为老师/同学的回答评分吧
0 星