2-5作业检查
相关代码:
index.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%String flag = request.getParameter("flag"); %>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
<script type="text/javascript">
var flag= <%=flag%>;
if(flag == "1"){
alert("你尚未登录;或者账号在异地登录;请重新登录");
}
</script>
</head>
<body>
<form action = "save.jsp" method="get" class = "login">
账号<input name = "username" type="text" placeholder = "请输入账号">
密码<input name = "password" type="text" placeholder = "请输入密码">
<input name="submit" type = "submit" value = "登录">
</form>
</body>
</html>
相关代码:
save.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<%
String username = request.getParameter("username");
String password = request.getParameter("password");
//把账号存到LoginUser属性中
session.setAttribute("LoginUser",username);
response.sendRedirect("show.jsp");
%>
</body>
</html>
相关代码:
show.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<img alt="无法显示" src="images/111.png">
</body>
</html>
相关代码:
web.xml配置过滤器
<filter>
<filter-name>Filter1</filter-name>
<filter-class>com.imooc.project2.Filter1</filter-class>
</filter>
<filter-mapping>
<filter-name>Filter1</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
相关代码:
过滤器
package com.imooc.project2;
import java.io.IOException;
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.annotation.WebFilter;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
* Servlet Filter implementation class Filter1
*/
@WebFilter("/Filter1")
public class Filter1 implements Filter {
/**
* Default constructor.
*/
public Filter1() {
// TODO Auto-generated constructor stub
}
/**
* @see Filter#destroy()
*/
public void destroy() {
// TODO Auto-generated method stub
}
/**
* @see Filter#doFilter(ServletRequest, ServletResponse, FilterChain)
*/
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
// TODO Auto-generated method stub
// place your code here
// pass the request along the filter chain
HttpServletRequest req = (HttpServletRequest)request;
HttpServletResponse res = (HttpServletResponse)response;
//当另一处登录的时候,对应的session对象被销毁;此时会使loginUser为null
String loginUser = (String)req.getSession().getAttribute("LoginUser");
String url = req.getRequestURI();
if(url.equals("/FirstProject/index.jsp") || url.equals("/FirstProject/") || url.equals("/FirstProject/save.jsp") ){
chain.doFilter(req, res);
}
else {
if(loginUser == null) {
System.out.println("a");
System.out.println(url);
res.sendRedirect("index.jsp?flag=1");
}
else chain.doFilter(req, res);}
}
/**
* @see Filter#init(FilterConfig)
*/
public void init(FilterConfig fConfig) throws ServletException {
// TODO Auto-generated method stub
}
}
相关代码:
缓存存放类
package com.imooc.project2;
import java.util.HashMap;
import java.util.Map;
import javax.servlet.http.HttpSession;
public class LoginCheck {
private static LoginCheck instance = new LoginCheck();
private Map<String,String>LoginSessionId=new HashMap<String,String>();
//key值:登录用户登录名,value值:登录用户sessionId
private Map<String,HttpSession>LoginSession=new HashMap<String,HttpSession>();
//key值:登录用户sessionId,value值:登录用户session对象
public LoginCheck() {}
public static LoginCheck getInstance() {
return instance;
}
public void setSessionIdByUsername(String username , String sessionId) {
this.LoginSessionId.put(username,sessionId);
}
public void setSessionById(String sessionId , HttpSession session) {
this.LoginSession.put(sessionId,session);
}
public String getSessionIdByUsername(String username) {
return LoginSessionId.get(username);
}
public HttpSession getSessionBySessionId(String sessionId) {
return LoginSession.get(sessionId);
}
}
相关代码:
监听器
package com.imooc.project2;
import javax.servlet.annotation.WebListener;
import javax.servlet.http.HttpSession;
import javax.servlet.http.HttpSessionAttributeListener;
import javax.servlet.http.HttpSessionBindingEvent;
@WebListener
public class LoginListener implements HttpSessionAttributeListener {
@Override
public void attributeAdded(HttpSessionBindingEvent event) {
// TODO Auto-generated method stub
//获取session属性值:也就是输入的账号
String userNumber = (String)event.getValue();
//获取session属性名;
String username = event.getName();
//获取当前session对象
HttpSession session = event.getSession();
//获取当前sessionId
String sessionId = session.getId();
//获取缓存的sessionId
String sessionId2 = LoginCheck.getInstance().getSessionIdByUsername(userNumber);
if(username.equals("LoginUser")) {
if(sessionId2==null) {}
else {
HttpSession session2 = LoginCheck.getInstance().getSessionBySessionId(sessionId2);
//撤销session
session2.invalidate();
}
}
LoginCheck.getInstance().setSessionById(sessionId, session);
LoginCheck.getInstance().setSessionIdByUsername(userNumber, sessionId);
}
@Override
public void attributeRemoved(HttpSessionBindingEvent arg0) {
// TODO Auto-generated method stub
}
@Override
public void attributeReplaced(HttpSessionBindingEvent arg0) {
// TODO Auto-generated method stub
}
}
125
收起
正在回答 回答被采纳积分+1
java工程师2020版
- 参与学习 人
- 提交作业 9393 份
- 解答问题 16556 个
综合就业常年第一,编程排行常年霸榜,无需脱产即可学习,北上广深月薪过万 无论你是未就业的学生还是想转行的在职人员,不需要基础,只要你有梦想,想高薪
了解课程
恭喜解决一个难题,获得1积分~
来为老师/同学的回答评分吧
0 星