1.1 概述
监听器:主要是用来监听特定对象的创建或销毁、属性的变化的!
是一个实现特定接口的普通java类!
对象:
自己创建自己用 (不用监听)
别人创建自己用 (需要监听)
Servlet中哪些对象需要监听?
request / session / servletContext
分别对应的是request监听器、session相关监听器、servletContext监听器
监听器(listener)
监听器接口:
一、监听对象创建/销毁的监听器接口
InterfaceServletRequestListener 监听request对象的创建或销毁
InterfaceHttpSessionListener 监听session对象的创建或销毁
InterfaceServletContextListener 监听servletContext对象的创建或销毁
二、监听对象属性的变化
InterfaceServletRequestAttributeListener 监听request对象属性变化: 添加、移除、修改
InterfaceHttpSessionAttributeListener 监听session对象属性变化: 添加、移除、修改
InterfaceServletContextAttributeListener 监听servletContext对象属性变化
三、session相关监听器
InterfaceHttpSessionBindingListener 监听对象绑定到session上的事件
InterfaceHttpSessionActivationListener(了解) 监听session序列化及反序列化的事件
404(路径写错)
500(服务器错误,调试)
1.2 生命周期监听器
声明周期监听器:监听对象的创建、销毁的过程!
监听器开发步骤:
1. 写一个普通java类,实现相关接口;
2. 配置(web.xml)
A.ServletRequestListener
监听request对象的创建或销毁。
MyRequestListener.java
package cn.itcast.a_life;
import javax.servlet.ServletRequestEvent;
import javax.servlet.ServletRequestListener;
/**
* 监听request对象的创建或销毁
*/
public class MyRequestListener implements ServletRequestListener{
// 对象销毁
@Override
public void requestDestroyed(ServletRequestEvent sre) {
// 获取request中存放的数据
Object obj = sre.getServletRequest().getAttribute("cn");
System.out.println(obj);
System.out.println("MyRequestListener.requestDestroyed()");
}
// 对象创建
@Override
public void requestInitialized(ServletRequestEvent sre) {
System.out.println("MyRequestListener.requestInitialized()");
}
}
Web.xml
<!-- 监听request对象创建、销毁 -->
<listener>
<listener-class>cn.itcast.a_life.MyRequestListener</listener-class>
</listener>
listener_test.jsp
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@page import="cn.itcast.c_session.Admin"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>My JSP 'index.jsp' starting page</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
</head>
<body>
test!
<% request.setAttribute("cn","China"); %>
</body>
</html>
B. HttpSessionListener
监听session对象的创建或销毁。
C. ServletContextListener
监听servletContext对象的创建或销毁。
MyServletContextListener.javapackage cn.itcast.a_life;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
/**
* 监听ServletContext对象创建或销毁
*/
public class MyServletContextListener implements ServletContextListener{
@Override
public void contextDestroyed(ServletContextEvent sce) {
System.out.println("MyServletContextListener.contextDestroyed()");
}
@Override
public void contextInitialized(ServletContextEvent sce) {
System.out.println("1..........MyServletContextListener.contextInitialized()");
}
}
MySessionListener.java
package cn.itcast.a_life;
import javax.servlet.http.HttpSessionEvent;
import javax.servlet.http.HttpSessionListener;
/**
* 监听Session对象创建、销毁
*/
public class MySessionListener implements HttpSessionListener{
// 创建
@Override
public void sessionCreated(HttpSessionEvent se) {
System.out.println("MySessionListener.sessionCreated()");
}
// 销毁
@Override
public void sessionDestroyed(HttpSessionEvent se) {
System.out.println("MySessionListener.sessionDestroyed()");
}
}
listener_test.jsp
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@page import="cn.itcast.c_session.Admin"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>My JSP 'index.jsp' starting page</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
</head>
<body>
test!
<%
//request.setAttribute("cn","China");
//session.invalidate(); //销毁session
//session.setAttribute("userName","Jack");
//session.removeAttribute("userName");
//session.setAttribute("userName","Jack_new");
session.setAttribute("userInfo",new Admin());
session.removeAttribute("userInfo");
%>
</body>
</html>
1.3 属性监听器
监听:request/session/servletContext对象属性的变化!
ServletRequestAttributeListener
HttpSessionAttributeListener
ServletContextAttributeListener
总结:先写类,实现接口; 再配置
MySessionAttrListener.javapackage cn.itcast.b_attr;
import javax.servlet.http.HttpSession;
import javax.servlet.http.HttpSessionAttributeListener;
import javax.servlet.http.HttpSessionBindingEvent;
/**
* 监听session对象属性的变化
*/
public class MySessionAttrListener implements HttpSessionAttributeListener {
// 属性添加
@Override
public void attributeAdded(HttpSessionBindingEvent se) {
// 先获取session对象
HttpSession session = se.getSession();
// 获取添加的属性
Object obj = session.getAttribute("userName");
// 测试
System.out.println("添加的属性:" + obj);
}
// 属性移除
@Override
public void attributeRemoved(HttpSessionBindingEvent se) {
System.out.println("属性移除");
}
// 属性被替换
@Override
public void attributeReplaced(HttpSessionBindingEvent se) {
// 获取sesison对象
HttpSession session = se.getSession();
// 获取替换前的值
Object old = se.getValue();
System.out.println("原来的值:" + old);
// 获取新值
Object obj_new = session.getAttribute("userName");
System.out.println("新值:" + obj_new);
}
}
listener_test.jsp
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@page import="cn.itcast.c_session.Admin"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>My JSP 'index.jsp' starting page</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
</head>
<body>
test!
<%
//request.setAttribute("cn","China");
//session.invalidate(); //销毁session
//session.setAttribute("userName","Jack");
//session.removeAttribute("userName");
//session.setAttribute("userName","Jack_new");
session.setAttribute("userInfo",new Admin());
session.removeAttribute("userInfo");
%>
</body>
</html>
web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
<!-- session的最大活跃时间 -->
<session-config>
<session-timeout>60</session-timeout>
</session-config>
<!-- 监听request对象创建、销毁 -->
<listener>
<listener-class>cn.itcast.a_life.MyRequestListener</listener-class>
</listener>
<!-- 监听session对象创建、销毁 -->
<listener>
<listener-class>cn.itcast.a_life.MySessionListener</listener-class>
</listener>
<!-- 监听servletContext对象创建、销毁 -->
<listener>
<listener-class>cn.itcast.a_life.MyServletContextListener</listener-class>
</listener>
<!-- 属性监听器 -->
<listener>
<listener-class>cn.itcast.b_attr.MySessionAttrListener</listener-class>
</listener>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>
1.4 其他监听器: session相关监听器
HttpSessionBindingListener
监听对象绑定/解除绑定到sesison上的事件!
步骤:
对象实现接口; 再把对象绑定/解除绑定到session上就会触发监听代码。
作用:
(上线提醒!)
Admin.javapackage cn.itcast.c_session;
import javax.servlet.http.HttpSessionBindingEvent;
import javax.servlet.http.HttpSessionBindingListener;
/**
* 监听此对象绑定到session上的过程,需要实现session特定接口
*/
public class Admin implements HttpSessionBindingListener {
private int id;
private String name;
public Admin() {
super();
}
public Admin(int id, String name) {
super();
this.id = id;
this.name = name;
}
// 构造函数
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
// 对象放入session
@Override
public void valueBound(HttpSessionBindingEvent event) {
System.out.println("Admin对象已经放入session");
}
// 对象从session中移除
@Override
public void valueUnbound(HttpSessionBindingEvent event) {
System.out.println("Admin对象从session中移除!");
}
}
1.4 案例
需求:做一个在线列表提醒的功能!
用户--à 登陆
---à 显示登陆信息,列表展示。(list.jsp)
--à 显示在线用户列表 (list.jsp)
-à 列表点击进入“在线列表页面” onlineuser.jsp
实现:
1. 先增加退出功能; 再把session活跃时间1min;
2. 写监听器,监听servletContext对象的创建: 初始化集合(onlineuserlist)
3. 登陆功能:用户登陆时候,把数据保存到servletContext中
4. List.jsp 增加超链接, 点击时候提交直接跳转到online.jsp
5. 写监听器:监听session销毁,把当前登陆用户从onlineuserlist移除!
Admin.java
package cn.itcast.entity;
/**
* 1. 管理员实体类开发
*/
public class Admin {
private int id;
private String userName;
private String pwd;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
public String getPwd() {
return pwd;
}
public void setPwd(String pwd) {
this.pwd = pwd;
}
}
Employee.java
package cn.itcast.entity;
/**
* 1. 员工
*/
public class Employee {
private int empId;
private String empName;
private int dept_id;
public int getEmpId() {
return empId;
}
public void setEmpId(int empId) {
this.empId = empId;
}
public String getEmpName() {
return empName;
}
public void setEmpName(String empName) {
this.empName = empName;
}
public int getDept_id() {
return dept_id;
}
public void setDept_id(int deptId) {
dept_id = deptId;
}
}
IAdminDao.java
package cn.itcast.dao;
import cn.itcast.entity.Admin;
/**
* 2. 管理员数据访问层接口设计
*/
public interface IAdminDao {
/**
* 根据用户名密码查询
* @param admin
* @return
*/
Admin findByNameAndPwd(Admin admin);
}
IEmployeeDao.java
package cn.itcast.dao;
import java.util.List;
import cn.itcast.entity.Employee;
/**
* 2. 员工数据访问层接口设计
*/
public interface IEmployeeDao {
/**
* 查询所有的员工
* @return
*/
List<Employee> getAll();
}
AdminDao.java
package cn.itcast.dao.impl;
import java.sql.SQLException;
import org.apache.commons.dbutils.handlers.BeanHandler;
import cn.itcast.dao.IAdminDao;
import cn.itcast.entity.Admin;
import cn.itcast.utils.JdbcUtils;
public class AdminDao implements IAdminDao {
@Override
public Admin findByNameAndPwd(Admin admin) {
try {
String sql = "select * from admin where userName=? and pwd=?";
return JdbcUtils.getQueryRuner()//
.query(sql,
new BeanHandler<Admin>(Admin.class),
admin.getUserName(),
admin.getPwd());
} catch (SQLException e) {
throw new RuntimeException(e);
}
}
}
EmployeeDao.java
package cn.itcast.dao.impl;
import java.sql.SQLException;
import java.util.List;
import org.apache.commons.dbutils.handlers.BeanListHandler;
import cn.itcast.dao.IEmployeeDao;
import cn.itcast.entity.Employee;
import cn.itcast.utils.JdbcUtils;
public class EmployeeDao implements IEmployeeDao {
@Override
public List<Employee> getAll() {
String sql = "select * from employee";
try {
return JdbcUtils.getQueryRuner()//
.query(sql, new BeanListHandler<Employee>(Employee.class));
} catch (SQLException e) {
throw new RuntimeException(e);
}
}
}
IAdminService.java
package cn.itcast.service;
import cn.itcast.entity.Admin;
/**
* 3. 管理员业务逻辑层
*/
public interface IAdminService {
/**
* 根据用户名密码查询
* @param admin
* @return
*/
Admin findByNameAndPwd(Admin admin);
}
IEmployeeService.java
package cn.itcast.service;
import java.util.List;
import cn.itcast.entity.Employee;
/**
* 2. 员工业务逻辑层
*/
public interface IEmployeeService {
/**
* 查询所有的员工
* @return
*/
List<Employee> getAll();
}
AdminService.java
package cn.itcast.service.impl;
import cn.itcast.dao.IAdminDao;
import cn.itcast.dao.impl.AdminDao;
import cn.itcast.entity.Admin;
import cn.itcast.service.IAdminService;
public class AdminService implements IAdminService{
// 创建dao对象
private IAdminDao adminDao = new AdminDao();
@Override
public Admin findByNameAndPwd(Admin admin) {
try {
return adminDao.findByNameAndPwd(admin);
} catch (Exception e) {
throw new RuntimeException(e);
}
}
}
EmployeeService.java
package cn.itcast.service.impl;
import java.util.List;
import cn.itcast.dao.IEmployeeDao;
import cn.itcast.dao.impl.EmployeeDao;
import cn.itcast.entity.Employee;
import cn.itcast.service.IEmployeeService;
public class EmployeeService implements IEmployeeService {
private IEmployeeDao employeeDao = new EmployeeDao();
@Override
public List<Employee> getAll() {
try {
return employeeDao.getAll();
} catch (Exception e) {
throw new RuntimeException(e);
}
}
}
AdminServlet.java
package cn.itcast.servlet;
import java.io.IOException;
import java.util.List;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import cn.itcast.entity.Admin;
import cn.itcast.service.IAdminService;
import cn.itcast.service.impl.AdminService;
/**
* 用户管理servlet 1. 登陆 2. 退出
*/
public class AdminServlet extends HttpServlet {
// Service实例
private IAdminService adminService = new AdminService();
// 跳转资源
private String uri;
protected void doGet(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
// 区别不同的操作类型
String method = request.getParameter("method");
// 判断
if ("login".equals(method)) {
// 登陆操作, 调用登陆方法
login(request, response);
}
else if ("out".equals(method)) {
// 退出操作,调用推出方法
out(request, response);
}
}
// 1 登陆
private void login(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
request.setCharacterEncoding("UTF-8");
// 1. 获取参数
String userName = request.getParameter("userName");
String pwd = request.getParameter("pwd");
// 封装
Admin admin = new Admin();
admin.setUserName(userName);
admin.setPwd(pwd);
try {
// 2. 调用service处理业务
Admin loginInfo = adminService.findByNameAndPwd(admin);
// 判断:
if (loginInfo == null) {
// 登陆失败
uri = "/login.jsp";
} else {
// 登陆成功
// 先,保存数据到session
request.getSession().setAttribute("loginInfo", loginInfo);
//【在线列表: 1. 先从servletContext中拿到在线列表集合; (onLineUserList)
// 2. 当前用户放入“在线列表集合中”】
// 实现1:先得到servletContext对象
ServletContext sc = getServletContext();
// 实现2: 再获取在线列表集合
List<Admin> onlineList = (List<Admin>) sc.getAttribute("onlineList");
// 判断
if (onlineList != null){
// 实现3: 添加当前登陆者
onlineList.add(loginInfo);
//sc.setAttribute("onlineList", onlineList); // 对象引用传递,不需要写也可以
}
// 再,跳转到首页显示servlet(/index)
uri = "/index";
}
} catch (Exception e) {
// 测试
e.printStackTrace();
// 错误
uri = "/error/error.jsp";
}
// 3. 跳转
request.getRequestDispatcher(uri).forward(request, response);
}
// 2 退出
private void out(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
//1. 获取session
HttpSession session = request.getSession(false);
//2.判断:
if (session!=null) {
// 从session中移除用户
// session.removeAttribute("loginInfo"); //?
// 销毁session
session.invalidate();
}
//3. 跳转(登陆)
response.sendRedirect(request.getContextPath() + "/login.jsp");
}
protected void doPost(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
this.doGet(req, resp);
}
}
IndexServlet.java
package cn.itcast.servlet;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import cn.itcast.entity.Admin;
import cn.itcast.entity.Employee;
import cn.itcast.service.IAdminService;
import cn.itcast.service.IEmployeeService;
import cn.itcast.service.impl.AdminService;
import cn.itcast.service.impl.EmployeeService;
public class IndexServlet extends HttpServlet {
// Service实例
private IEmployeeService employeeService = new EmployeeService();
// 跳转资源
private String uri;
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
try {
// 调用service查询所有
List<Employee> list = employeeService.getAll();
request.setAttribute("listEmp", list);
// 进入首页jsp
uri = "/list.jsp";
} catch (Exception e) {
e.printStackTrace();
uri = "/error/error.jsp";
}
// 转发
request.getRequestDispatcher(uri).forward(request, response);
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
this.doGet(request, response);
}
}
JdbcUtils.java
package cn.itcast.utils;
import javax.sql.DataSource;
import org.apache.commons.dbutils.QueryRunner;
import com.mchange.v2.c3p0.ComboPooledDataSource;
/**
* 工具类
* 1. 初始化C3P0连接池
* 2. 创建DbUtils核心工具类对象
*/
public class JdbcUtils {
/**
* 1. 初始化C3P0连接池
*/
private static DataSource dataSource;
static {
dataSource = new ComboPooledDataSource();
}
/**
* 2. 创建DbUtils核心工具类对象
*/
public static QueryRunner getQueryRuner(){
// 创建QueryRunner对象,传入连接池对象
// 在创建QueryRunner对象的时候,如果传入了数据源对象;
// 那么在使用QueryRunner对象方法的时候,就不需要传入连接对象;
// 会自动从数据源中获取连接(不用关闭连接)
return new QueryRunner(dataSource);
}
}LoginFilter.java
package cn.itcast.filter;
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.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
/**
* 登陆验证过滤器
*
* http://localhost:8080/emp_sys/login.jsp 可以直接访问
http://localhost:8080/emp_sys/login 可以直接访问
http://localhost:8080/emp_sys/index 不能直接访问
http://localhost:8080/emp_sys/list.jsp 不能直接访问
*/
public class LoginFilter implements Filter {
private String uri;
/**
* 分析:
*
1. 先指定放行的资源,哪些资源不需要拦截:
login.jsp + /login (request对象可以获取)
2. 获取session,从session中获取登陆用户
3. 判断是否为空:
为空, 说明没有登陆, 跳转到登陆
不为空, 已经登陆,放行!
*/
@Override
public void doFilter(ServletRequest req, ServletResponse res,
FilterChain chain) throws IOException, ServletException {
//0. 转换
HttpServletRequest request = (HttpServletRequest) req;
HttpServletResponse response = (HttpServletResponse) res;
//1. 获取请求资源,截取
String uri = request.getRequestURI(); // /emp_sys/login.jsp
// 截取 【login.jsp或login】
String requestPath = uri.substring(uri.lastIndexOf("/") + 1, uri.length());
//2. 判断: 先放行一些资源:/login.jsp、/login
if ("admin".equals(requestPath) || "login.jsp".equals(requestPath)) {
// 放行
chain.doFilter(request, response);
}
else {
//3. 对其他资源进行拦截
//3.1 先获取Session、获取session中的登陆用户(loginInfo)
HttpSession session = request.getSession(false);
// 判断
if (session != null) {
Object obj = session.getAttribute("loginInfo");
//3.2如果获取的内容不为空,说明已经登陆,放行
if (obj != null) {
// 放行
chain.doFilter(request, response);
return;
} else {
//3.3如果获取的内容为空,说明没有登陆; 跳转到登陆
uri = "/login.jsp";
}
} else {
// 肯定没有登陆
uri = "/login.jsp";
}
request.getRequestDispatcher(uri).forward(request, response);
}
}
@Override
public void init(FilterConfig filterConfig) throws ServletException {
}
@Override
public void destroy() {
}
}
OnlineAdminListener.java
package cn.itcast.listener;
import java.util.ArrayList;
import java.util.List;
import javax.servlet.ServletContext;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
import cn.itcast.entity.Admin;
/**
* 初始化在线列表集合监听器
*/
public class OnlineAdminListener implements ServletContextListener {
//1. ServletContext对象创建
@Override
public void contextInitialized(ServletContextEvent sce) {
// 创建集合:存放在线用户
// 每次当用户登陆后,就往这个集合中添加添加当前登陆者
List<Admin> onlineList = new ArrayList<Admin>();
// 放入ServletContext中
sce.getServletContext().setAttribute("onlineList", onlineList);
}
//2. ServletContext对象销毁
@Override
public void contextDestroyed(ServletContextEvent sce) {
// 获取ServletContext
ServletContext sc = sce.getServletContext();
// 获取在线列表
Object obj = sc.getAttribute("onlineList");
// 移除在线列表集合
if (obj != null) {
sc.removeAttribute("onlineList");
}
}
}
SessionListener.java
package cn.itcast.listener;
import java.util.List;
import javax.servlet.ServletContext;
import javax.servlet.http.HttpSession;
import javax.servlet.http.HttpSessionEvent;
import javax.servlet.http.HttpSessionListener;
import cn.itcast.entity.Admin;
/**
* 监听Session销毁的动作:
* 当服务器销毁session的时候,从在线列表集合中移除当亲的登陆用户
*/
public class SessionListener implements HttpSessionListener{
@Override
public void sessionDestroyed(HttpSessionEvent se) {
//1. 获取Session对象、ServletContext对象
HttpSession session = se.getSession();
ServletContext sc = session.getServletContext();
//2. 获取Session中存储的当前登陆用户
Object obj = session.getAttribute("loginInfo");//?
//3. 获取ServletContext中存储的在线用户列表集合
List<Admin> list = (List<Admin>) sc.getAttribute("onlineList");
// 先判断
if (obj != null){
//4. 把“当前登陆用户”从在线列表集合中移除
list.remove(obj);
}
}
@Override
public void sessionCreated(HttpSessionEvent se) {
}
}
c3p0-config.xml
<c3p0-config>
<default-config>
<property name="driverClass">com.mysql.jdbc.Driver</property>
<property name="jdbcUrl">jdbc:mysql:///jdbc_demo</property>
<property name="user">root</property>
<property name="password">root</property>
<property name="initialPoolSize">5</property>
<property name="maxPoolSize">10</property>
</default-config>
<named-config name="oracleConfig">
<property name="driverClass">com.mysql.jdbc.Driver</property>
<property name="jdbcUrl">jdbc:mysql:///day17</property>
<property name="user">root</property>
<property name="password">root</property>
<property name="initialPoolSize">5</property>
<property name="maxPoolSize">10</property>
</named-config>
</c3p0-config>
index.jsp
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>My JSP 'index.jsp' starting page</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
</head>
<body>
This is my JSP page. <br>
</body>
</html>
head.jsp
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<h3 align="center">
欢迎你,${sessionScope.loginInfo.userName };
<a href="${pageContext.request.contextPath }/admin?method=out">退出</a>
<a href="${pageContext.request.contextPath }/online.jsp">在线列表展示</a>
</h3>list.jsp
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<!-- 引入jstl核心标签库 -->
<%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>My JSP 'index.jsp' starting page</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
</head>
<body>
<!-- 引入头部页面 -->
<jsp:include page="/public/head.jsp"></jsp:include>
<!-- 列表展示数据 -->
<table align="center" border="1" width="80%" cellpadding="3" cellspacing="0">
<tr>
<td>序号</td>
<td>编号</td>
<td>员工名称</td>
</tr>
<c:if test="${not empty requestScope.listEmp}">
<c:forEach var="emp" items="${requestScope.listEmp}" varStatus="vs">
<tr>
<td>${vs.count }</td>
<td>${emp.empId }</td>
<td>${emp.empName }</td>
</tr>
</c:forEach>
</c:if>
</table>
</body>
</html>
login.jsp
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>My JSP 'index.jsp' starting page</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
</head>
<body>
<form name="frmLogin" action="${pageContext.request.contextPath }/admin?method=login" method="post">
<table align="center" border="1">
<tr>
<td>用户名</td>
<td>
<input type="text" name="userName">
</td>
</tr>
<tr>
<td>密码</td>
<td>
<input type="password" name="pwd">
</td>
</tr>
<tr>
<td>
<input type="submit" value="亲,点我登陆!">
</td>
</tr>
</table>
</form>
</body>
</html>
online.jsp
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<!-- 引入jstl核心标签库 -->
<%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>在线用户列表</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
</head>
<body>
<!-- 引入头部页面 -->
<jsp:include page="/public/head.jsp"></jsp:include>
<!-- 在线用户 -->
<table align="center" border="1" width="80%" cellpadding="3" cellspacing="0">
<tr>
<td colspan="2" align="center"><h3>在线列表展示:</h3></td>
</tr>
<tr>
<td>编号</td>
<td>员工名称</td>
</tr>
<c:if test="${not empty applicationScope.onlineList}">
<c:forEach var="admin" items="${applicationScope.onlineList}">
<tr>
<td>${admin.id }</td>
<td>${admin.userName }</td>
</tr>
</c:forEach>
</c:if>
</table>
</body>
</html>
1029

被折叠的 条评论
为什么被折叠?



