?
//Test_Session.java
package com.symc.web_project_mie.service;
import java.io.IOException;
import java.io.Writer;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.ServletContext;
public class Test_Session extends HttpServlet {
private static final long serialVersionUID=1l;
protected void doGet(HttpServletRequest request,HttpServletResponse response){
try {
response.setContentType("text/html;charset=utf-8");//设置响应体
Writer out=response.getWriter();
out.write("<hr>");
Cookie[] cookies=request.getCookies();//拿到cookie
boolean flag=false;
if(cookies==null){ //拿到的cookie数组为空
addCookie(response,out);//添加cookie
}else{
for(Cookie cookie:cookies){ //拿到的cookie数组不为空
if(cookie.getName().equals("user")){ //拿到cookie的值
out.write("欢迎回来"+cookie.getValue());
flag=true;
break;
}
}if(!flag){
addCookie(response,out);
}
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
protected void doPost(HttpServletRequest request,HttpServletResponse response){
doGet(request,response);
}
public void addCookie(HttpServletResponse response,Writer out){
try {
int serial=getSerialFromContext();//到上下文中
out.write("欢迎您,新用户"+serial);
Cookie cookie=new Cookie("user","user"+serial);
cookie.setMaxAge(3600);//一小时内有效
cookie.setPath("/");//对整个web应用都有效
response.addCookie(cookie);//将cookie写给用户
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public int getSerialFromContext(){
int serial=1;
ServletContext context=this.getServletContext();
Object serial_obj=context.getAttribute("user_serial");//用户序列号
if(serial_obj!=null){
serial=Integer.parseInt(serial_obj.toString())+1;
// context.setAttribute("user_serial", serial);
// }else{
context.setAttribute("user_serial",serial);
}
return serial;
}
}
//web.xml
<servlet>
<servlet-name>test_cookie</servlet-name>
<servlet-class>com.symc.web_project_mie.service.Test_Session</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>test_cookie</servlet-name>
<url-pattern>/testCookie</url-pattern>
</servlet-mapping>
//header.jsp
<%@ page language="java" contentType="text/html; charset=utf-8"
pageEncoding="utf-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<link href="CSS/mycss.css" rel="stylesheet" type="text/css">
<title>mypage</title>
</head>
<body>
<%
//计数器 刷新一次当前访问次数+1
int access_count=1; //定义访客数目为1
Object user_access=application.getAttribute("user_access");//直接调用上下文(application),
if(user_access!=null){ //判断是否为空
access_count=Integer.parseInt(user_access.toString())+1;
}
application.setAttribute("user_access",access_count);
out.write("<div>当前访问次数:"+access_count+"</div>");
%>
</body>
</html>
//test_cookie.jsp
<%@ page language="java" contentType="text/html; charset=utf-8"
pageEncoding="utf-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>mypage</title>
</head>
<body>
<%@include file="/header.jsp"%>
<br />
<a href="testCookie">测试Cookie</a>
</body>
</html>
|