作业打卡
登录页面:
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>2-5作业</title>
</head>
<body>
<div style="margin-left:40px;">
<h1>系统登录</h1>
</div>
<form action="/servlet/login" id="form" method="post">
<ul style="list-style-type: none">
<li >用户名:</li>
<li><input id="name" name="nm" style="width:200px;"></li>
<li>密码:</li>
<li><input type="password" id="password" name="password" style="width:200px;"></li>
<li style="margin-top:10px;"><input type="submit" value="登录" id="btn"></li>
</ul>
</form>
<script type="text/javascript">
//创建登录事件:
document.getElementById("form").onsubmit=function(){
//正则表达式设置登录信息
var regexN=/^[a-z|A-Z|0-q]{1,10}$/;
var regexP=/^[a-z|A-Z|0-q]{1,10}$/;
//获取登录和密码的value值
var name=document.getElementById("name").value;
var password=document.getElementById("password").value;
//进行匹配
if(regexN.test(name)==false){
alert("用户名不符合当前格式!");
return false;
}else if(regexP.test(password)==false){
alert("密码不符合当前格式!");
return false;
}else{
alert("登录成功");
return true;
}
}
</script>
</body>
</html>
主页:
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<h2>默认主页</h2>
</body>
</html>
提示已登录页面:
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<script type="text/javascript">
alert("你尚未登录;或者账号在异地登录;请重新登录");
</script>
<div style="margin-left:40px;">
<h1>系统登录</h1>
</div>
<form action="/servlet/login" id="form" method="post">
<ul style="list-style-type: none">
<li >用户名:</li>
<li><input id="name" name="nm" style="width:200px;"></li>
<li>密码:</li>
<li><input type="password" id="password" name="password" style="width:200px;"></li>
<li style="margin-top:10px;"><input type="submit" value="登录" id="btn"></li>
</ul>
</form>
<script type="text/javascript">
//创建登录事件:
document.getElementById("form").onsubmit=function(){
//正则表达式设置登录信息
var regexN=/^[a-z|A-Z|0-q]{1,10}$/;
var regexP=/^[a-z|A-Z|0-q]{1,10}$/;
//获取登录和密码的value值
var name=document.getElementById("name").value;
var password=document.getElementById("password").value;
//进行匹配
if(regexN.test(name)==false){
alert("用户名不符合当前格式!");
return false;
}else if(regexP.test(password)==false){
alert("密码不符合当前格式!");
return false;
}else{
alert("登录成功");
return true;
}
}
</script>
</html>
过滤器:
package com.imooc.homework;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
//2-5作业,过滤器
public class LoginFilter implements Filter {
@Override
public void destroy() {
// TODO Auto-generated method stub
}
@Override
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
throws IOException, ServletException {
//强制转换
HttpServletRequest req=(HttpServletRequest)request;
HttpServletResponse res=(HttpServletResponse)response;
//获取表单的值
// String form=req.getParameter("form");
//获取session的对象,因为当登录成功的收SessionID里的值肯定存在的,所以进行判断
HttpSession session=req.getSession();
// String Username=(String) session.getAttribute("user"); 错的
//获取账号文本框的值
String name=request.getParameter("nm");
//获取当前uri
String uri=req.getRequestURI();
System.out.println("本次获取的uri:"+uri);
//进行判断
if(uri.startsWith("/login")) { //当访问的url开头为login的时候,不拦截,直接放行,进入到登录页面;
chain.doFilter(request, response);
//因为前端已判定过了。如果账号信息不对的话,是无法提交的,所以这里的name肯定是正确的信息;
}else if(name!=null){ //如果name的值!=null的时候,说明登录成功了,跳转到主页;
System.out.println("本次账号信息:"+name);
chain.doFilter(request, response);
}else {
res.sendRedirect("/login.html"); //拦截一切未登录的uri;
}
}
@Override
public void init(FilterConfig arg0) throws ServletException {
// TODO Auto-generated method stub
}
}
监听器:
package com.imooc.homework;
import java.util.ArrayList;
import java.util.List;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
import javax.servlet.ServletRequestEvent;
import javax.servlet.ServletRequestListener;
import javax.servlet.http.HttpServletRequest;
//2-5作业
public class LoginListener implements ServletRequestListener, ServletContextListener {
@Override
public void contextDestroyed(ServletContextEvent arg0) {
// TODO Auto-generated method stub
}
@Override
public void contextInitialized(ServletContextEvent sce) {
// 创建1个集合存放文本框name值, 初始化context时
List<String> nameList=new ArrayList();
//把集合存自定义属性存放到context中
sce.getServletContext().setAttribute("nameList", nameList);
}
@Override
public void requestDestroyed(ServletRequestEvent arg0) {
// TODO Auto-generated method stub
}
@Override
public void requestInitialized(ServletRequestEvent sre) {
// //每次初始化请求时,获取属性
// HttpServletRequest request=(HttpServletRequest) sre.getServletRequest();
// //获取前端属性
// String nm=request.getParameter("nm");
// //获取context对象内的集合
// List nameList=(List) request.getServletContext().getAttribute("nameList");
// //对集合进行判断,如果集合内存在name信息,则进行删除,并跳转到提示页面,如果不存在则添加
// if(nameList.indexOf(nm)==-1) {
// nameList.add(nm);
// sre.getServletContext().setAttribute("nameList", nameList);
// System.out.println("监听器:"+nameList);
//
// }
//
//
}
}
处理已登录过信息的页面:
package com.imooc.homework;
//作业2-5
import java.io.IOException;
import java.util.List;
import javax.servlet.ServletContext;
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;
/**
* @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 {
}
/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//获取前端id信息
String name=request.getParameter("nm");
//获取context
ServletContext context=request.getServletContext();
List nameList=(List)context.getAttribute("nameList");
//进行判断
if(nameList.indexOf(name)==-1) { //集合中没查询到name的值,也就是说这值没有登录过,
nameList.add(name); //因为没有查询到这值,进行添加到集合内;
System.out.println("此账号未登录过,可登录:"+nameList);
request.getRequestDispatcher("/homepage.html").forward(request, response);
}else {
// Integer index=nameList.indexOf(name);
nameList.remove(name); //name值已存在,需提示不可登录,同时删除这个值;
System.out.println("此账号已存在,不可登录:"+nameList);
request.getRequestDispatcher("/error.html").forward(request, response);
}
}
}
老师我没有按照题中的session方法去实现。 我自己实现了下感觉没什么问题了。请老师帮忙看看。
25
收起
正在回答 回答被采纳积分+1
2回答
java工程师2020版
- 参与学习 人
- 提交作业 9393 份
- 解答问题 16556 个
综合就业常年第一,编程排行常年霸榜,无需脱产即可学习,北上广深月薪过万 无论你是未就业的学生还是想转行的在职人员,不需要基础,只要你有梦想,想高薪
了解课程
恭喜解决一个难题,获得1积分~
来为老师/同学的回答评分吧
0 星