Listener 显示项目 访问量
package com.zhonggong.listerning;
import javax.servlet.ServletContext;
import javax.servlet.http.HttpSessionEvent;
import javax.servlet.http.HttpSessionListener;
public class StudentListerning implements HttpSessionListener{
// 在session创建的时候被调用 ,只要浏览,与服务器有就会创建这个对象
@Override
public void sessionCreated(HttpSessionEvent event) {
//ServletContext = application ,实现数据共享
ServletContext seContext =event.getSession().getServletContext();
Integer count =(Integer)seContext.getAttribute("count");
//是否是第一次访问
if(count==null){
count=1;
}else{
count++;
}
//把获取的值放到共享对象;里面seContext
seContext.setAttribute("count", count);
System.out.println("这个方法已经被调用sessionCreated");
}
//session失效的时候调用
@Override
public void sessionDestroyed(HttpSessionEvent event) {
ServletContext servletContext = event.getSession().getServletContext();
//获取值来进行判断
Integer count=(Integer)servletContext.getAttribute("count");
if(count==null){
count=0;
}else{
count--;
}
servletContext.setAttribute("count", count);
System.out.println("********"+"sessionDestroyed"+"**************");
}
}
在另一jsp中获取count
<!--总计在线人数 -->
<%=session.getServletContext().getAttribute("count")%>
监听器在xml中的配置
<listener>
<listener-class>com.zhonggong.listerning.StudentListerning</listener-class>
</listener>
过滤器
package com.zhonggong.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 com.zhonggong.entity.Student;
public class EncodingFilter implements Filter{
//销毁
@Override
public void destroy() {
// TODO Auto-generated method stub
}
//Service
@Override
public void doFilter(ServletRequest request, ServletResponse response,
FilterChain filter) throws IOException, ServletException {
HttpServletRequest hRequest = (HttpServletRequest) request;
HttpServletResponse hResponse = (HttpServletResponse) response;
Student student = (Student) hRequest.getSession().getAttribute("student");
if(student ==null){
/* http://localhost:8080/jsp/error.jsp
*/ hResponse.sendRedirect("/jsp/error.jsp");
}else {
filter.doFilter(request, response);
}
//设置编码格式
/* request.setCharacterEncoding("utf-8");
response.setCharacterEncoding("utf-8");
//如果有下一个过滤器,传递给下一个过滤 ,直接传递给web servlet
filter.doFilter(request, response);
*/
}
//初始化 ,只是创建过滤器的时候走这个方法
@Override
public void init(FilterConfig arg0) throws ServletException {
// TODO Auto-generated method stub
}
}
通过登入页面提交表单后进入的servlet会new Student();
过滤器在xml中的配置
<filter>
<filter-name>encodingFilter</filter-name>
<filter-class>com.zhonggong.filter.EncodingFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>encodingFilter</filter-name>
<url-pattern>/jsp/admin/*</url-pattern>
</filter-mapping>
利用cookie保存登录的数据
登录jsp
<%@page import="java.net.URLDecoder"%>
<%@page import="java.net.URLEncoder"%>
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>My JSP 'login.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">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
</head>
<!--绝对路径 -->
<%-- ${pageContext.request.contextPath};
/filter --%>
<%-- <%
/*创建一个cookie */
/* URLDecoder */
Cookie co = new Cookie("name",URLEncoder.encode("付家伟","utf-8"));
/*设置cookie的有效期 */
co.setMaxAge(60*60);
/* 添加到响应里 */
response.addCookie(co);
%> --%>
<%
String userName="";
String upwd="";
Cookie [] cookies= request.getCookies();
if(cookies !=null && cookies.length>0){
for(int i=0;i<cookies.length;i++){
if(cookies[i].getName().equals("userName")){
userName = URLDecoder.decode(cookies[i].getValue(), "utf-8");
}else if(cookies[i].getName().equals("upwd")){
upwd = cookies[i].getValue();
}
}
}
%>
<body>
<form action="${pageContext.request.contextPath}/servlet" method="post">
<p>
用户名:<input type="text" name="userName" value="<%=userName%>">
</p>
<p>
密码:<input type="password" name="pwd" value="<%=upwd%>">
</p>
<p>
<input type="submit" value="确定">
</p>
</form>
</body>
</html>
对应的servlet里利用cookie存值
package com.zhonggong.servlet;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.zhonggong.entity.Student;
public class UserServlet extends HttpServlet{
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
doPost(req, resp);
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
String userName=req.getParameter("userName");
String upwd = req.getParameter("pwd");
Student student = new Student();
if(student !=null){
req.setAttribute("userName", userName);
req.setAttribute("upwd", upwd);
req.getRequestDispatcher("jsp/admin/show.jsp").forward(req, resp);
/* resp.sendRedirect("jsp/admin/show.jsp");*/
}
}
}
servlet再往show.jsp转发,login.jsp随后接受cookie的值
<%
String userName =(String)request.getAttribute("userName");
String upwd =(String)request.getAttribute("upwd");
/*用户名的cookie */
Cookie cookieName= new Cookie("userName",userName);
/*密码 */
Cookie cookiePwd = new Cookie("upwd",upwd);
response.addCookie(cookieName);
response.addCookie(cookiePwd);
%>
cookie的存值与取值
/* 创建一个cookie */
//Cookie co=new Cookie("name","leif");
Cookie co=new Cookie("name",URLEncoder.encode("嗯哼", "utf-8"));
/* 设置cookie的有效期,单位为秒 */
co.setMaxAge(60*60);
/* 添加到响应里 */
response.addCookie(co);
<%
Cookie[] cookies=request.getCookies();
if(cookies !=null && cookies.length>0){
for(int i=0;i<cookies.length;i++){
out.print("取的键"+cookies[i].getName()+"</br>"+"取的值"+URLDecoder.decode(cookies[i].getValue(), "utf-8"));
}
}
%>
<!-- 绝对路径,获取最顶层的目录 /filter -->
${pageContext.request.contextPath};