Map<String, HttpSession> map=(Map<String, HttpSession>) sc.getAttribute("users");//保存所有登录有关的map对象...

本文介绍了一个使用Java Servlet API实现的在线用户监听器,该监听器通过监听HTTP会话属性的变化来跟踪用户的登录状态,并将活跃用户信息保存到ServletContext中共享的Map集合里。
package com.tfy.heima.listener;


import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;


import javax.servlet.ServletContext;
import javax.servlet.http.HttpSession;
import javax.servlet.http.HttpSessionAttributeListener;
import javax.servlet.http.HttpSessionBindingEvent;


import com.tfy.heima.domain.User;


public class OnlineUserListener implements HttpSessionAttributeListener {
//Map<String,HttpSession>
public void attributeAdded(HttpSessionBindingEvent se) {
HttpSession session=se.getSession();
Object obj=session.getAttribute("user");
if(obj!=null && obj instanceof User){
User user=(User)obj;
ServletContext sc=session.getServletContext();
Map<String, HttpSession> map=(Map<String, HttpSession>) sc.getAttribute("users");//保存所有登录有关的map对象
if(map==null){
map=Collections.synchronizedMap(new HashMap<String, HttpSession>());
sc.setAttribute("users", map);
}
map.put(user.getUsername(), session);
}
}


public void attributeRemoved(HttpSessionBindingEvent se) {


}


public void attributeReplaced(HttpSessionBindingEvent se) {


}


}
<%@ page contentType="text/html;charset=UTF-8" %> <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %> <html> <head> <title>班级列表</title> </head> <body> <h3>班级列表</h3> <a href="addClass.jsp">添加班级</a> <table border="1"> <tr> <th>班级名称</th> <th>操作</th> </tr> <c:forEach var="cls" items="${classist}"> <tr> <td>${cls.cname} (ID: ${cls.cid})</td> <td> <a href="chooseTerm.jsp?cid=${cls.cid}">查看成绩</a> <a href="Student?cid=${cls.cid}">查看学生</a> </td> </tr> </c:forEach> </table> </body> </html> <%@ page contentType="text/html;charset=UTF-8" language="java" %> <html> <head> <title>Title</title> </head> <body> <c:forEach var="gls" items="${gradeList}"> <tr> <td>${gls.term} (${gls.gid})</td> <td> <a href="ShowGrade?cid=${gls.gid}&term=第一学期">查看第一学期成绩</a><br> <a href="ShowGrade?cid=${gls.gid}&term=第二学期">查看第二学期成绩</a><br> <a href="ShowGrade?cid=${gls.gid}&term=第三学期">查看第三学期成绩</a><br> <a href="ShowGrade?cid=${gls.gid}&term=第四学期">查看第四学期成绩</a> </td> </tr> </c:forEach> </body> </html> <%@ page contentType="text/html;charset=UTF-8" language="java" %> <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %> <html> <head> <title>成绩列表</title> </head> <body> <h3>班级ID:${cid} | 学期:${term}</h3> <table border="1"> <tr> <th>学生姓名</th> <th>数学</th> <th>语文</th> <th>英语</th> </tr> <c:forEach var="grade" items="${gradeList}"> <tr> <td>${grade.studentName}</td> <td>${grade.math}</td> <td>${grade.chinese}</td> <td>${grade.english}</td> </tr> </c:forEach> </table> <br> <a href="javascript:history.back()">返回</a> </body> </html> @RequestMapping("/ShowGrade") public ModelAndView showGrade(HttpSession session, String term, ModelAndView mv) { Integer cid = (Integer) session.getAttribute("cid"); List<Map<String, Object>> gradeList = gradeService.getGradeByTerm(cid, term); mv.addObject("gradeList", gradeList); mv.addObject("term", term); mv.addObject("cid", cid); mv.setViewName("ShowGrade.jsp"); return mv; }<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd"> <mapper namespace="com.buka.dao.GradeDao"> <resultMap id="grade" type="com.buka.po.Grade"> <result property="name" column="name"></result> <result property="term" column="term"></result> <result property="math" column="math"></result> <result property="chinese" column="chinese"></result> <result property="English" column="English"></result> <result property="sid" column="sid"></result> <association property="cid" javaType="com.buka.po.Student"> <id property="cid" column="cid"></id> </association> </resultMap> <insert id="addGrade" parameterType="com.buka.po.Grade"> INSERT INTO grade (gid, term, sid, math, chinese, English) VALUES (#{gid}, #{term}, #{sid}, #{math}, #{chinese}, #{English}) </insert> <select id="getGradeByTerm" resultMap="grade"> SELECT s.name AS studentName, g.math AS math, g.chinese AS chinese, g.English AS english, g.term AS term, s.cid AS cid FROM grade g INNER JOIN student s ON g.sid = s.sid WHERE s.cid = #{cid} AND g.term = #{term} ORDER BY s.name </select> </mapper>我该如何解决cid无法在页面中传值的问题
09-18
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值