一、Session的使用
Session是服务器在需要时为每一个会话用户分配的服务器端的内存,供这个会话用户单独拥有,其他用户不得访问。以(String,Object)键值对存在。
//创建并将数据写入Session
HttpSessionhs = req.getSession(true);
hs.setAttribute("flag","yes");
hs.setAttribute("username",username);
hs.setAttribute("password",password);
hs.setMaxInactiveInterval(30);
//读取Session
HttpSessionhs = req.getSession(true);
String u =(String)hs.getAttribute("username");
String p =(String)hs.getAttribute("password");
二、创建Cookie并使用
Cookie是在服务器端创建,并且回写到会话客户端的文件,是一份份的数据,服务器在需要的时候可以从客户端读取,如2周内不用登陆功能。单独归某个客户端拥有,其他客户无法使用。以(String,String)键值对存在。
//登陆页面
package com.hpu.ych;
import javax.servlet.http.*;
import javax.servlet.*;
import java.io.*;
public class MyCookie extends HttpServlet{
protectedvoid doGet(HttpServletRequest req,HttpServletResponse res){
try{
res.setContentType("text/html;charset=gbk");//解决中文乱码
PrintWriterpw = res.getWriter();
Cookiec = new Cookie("color","red");//创建cookie,分别为名和值,都只能是字符串
//c.setPath("/cookie");
c.setMaxAge(30);//设置cookie的生存时间,否则不会被保存
res.addCookie(c);//将cookie写回到客户端
pw.println("Cookie已经创建!");
}catch(Exceptione){
e.printStackTrace();
}
}
protectedvoid doPost(HttpServletRequest req,HttpServletResponse res)throwsServletException,java.io.IOException{
this.doGet(req,res);
}
}
//读取cookie
package com.hpu.ych;
import javax.servlet.http.*;
import javax.servlet.*;
import java.io.*;
public class ReadCookie extends HttpServlet{
protectedvoid doGet(HttpServletRequest req,HttpServletResponse res){
//System.out.println("readcookie action");
try{
res.setContentType("text/html;charset=gbk");//解决中文乱码
PrintWriterpw = res.getWriter();
Cookie[]allCookies = req.getCookies();//取出所有Cookie
if(allCookies!= null){
for(inti=0; i<allCookies.length; i++){
Cookietemp = allCookies[i];
pw.println("Name:"+temp.getName()+ "---value:" + temp.getValue() + "---path:" +temp.getPath() +"<br>");
if(temp.getName().equals("Color")){
pw.println("Cookie读取成功!");
pw.println(temp.getValue());
//temp.setMaxAge(0);//可以删除Cookie!
break;
}
}
}else{
pw.println("没有cookie!");
}
}catch(Exceptione){
e.printStackTrace();
}
}
protectedvoid doPost(HttpServletRequest req,HttpServletResponse res)throwsServletException,java.io.IOException{
this.doGet(req,res);
}
}
三、ServletContext测创建和使用
ServeltContext与上面两者不同,它是服务器端的,供各个会话用户共享的内存空间,以(String,Object)键值对存在。声明周期从ServletContext创建开始,到服务器关闭结束。
1、创建ServletContext并写入内容
//ServletContext测试页面
package com.hpu.ych;
import javax.servlet.http.*;
import javax.servlet.*;
import java.io.*;
public class ServletContextTest1 extends HttpServlet{
protected voiddoGet(HttpServletRequest req,HttpServletResponse res){
try{
res.setContentType("text/html;charset=gbk");//解决中文乱码
PrintWriterpw = res.getWriter();
ServletContextsc = this.getServletContext();
sc.setAttribute("name","杨传华");
pw.println("ServletContext中写入name:" + "杨传华");
}catch(Exceptione){
e.printStackTrace();
}
}
protected voiddoPost(HttpServletRequest req,HttpServletResponse res)throwsServletException,java.io.IOException{
this.doGet(req,res);
}
}
2.读取ServletContext内容
读取ServletContext中写入的内容
//ServletContext测试页面
package com.hpu.ych;
import javax.servlet.http.*;
import javax.servlet.*;
import java.io.*;
public class ServletContextTest2 extends HttpServlet{
protected voiddoGet(HttpServletRequest req,HttpServletResponse res){
try{
res.setContentType("text/html;charset=gbk");//解决中文乱码
PrintWriterpw = res.getWriter();
ServletContextsc = this.getServletContext();
Stringvalue = (String)sc.getAttribute("name");
pw.println("name:"+ value);
}catch(Exceptione){
e.printStackTrace();
}
}
protected voiddoPost(HttpServletRequest req,HttpServletResponse res)throws ServletException,java.io.IOException{
this.doGet(req,res);
}
}
3、ServletContext简单应用,网站访问计数器
将访问次数保存到一个文件中,每次启动网站的时候(重写init()方法),将这个访问次数读取到ServletContext中,共各个用户共享,当网站重启或关闭时(重写destroy()方法),及时将数据写回文件保存,供下次读取。
public void init() throws ServletException{
BufferedReader br = null;
try{
br = new BufferedReader(new FileReader("D:/JavaLearnTest/times.txt"));//有问题
String sNum = br.readLine();
ServletContext sc = this.getServletContext();
sc.setAttribute("times",sNum);
System.out.println("数据被安全读出 !" + sc.getAttribute("times"));
}catch(IOException e){
e.printStackTrace();
}finally{
try{
if(br != null){
br.close();
}
}catch(IOException e){
e.printStackTrace();
}
}
}
public void destroy(){
BufferedWriter bw = null;
System.out.println("Destory Used!");
try{
bw = new BufferedWriter(new FileWriter("D:/JavaLearnTest/times.txt"));
System.out.println("数据被安全写回!");
bw.write(this.getServletContext().getAttribute("times").toString());
System.out.println("数据被安全写回!");
}catch(IOException e){
e.printStackTrace();
}finally{
try{
if(bw != null){
bw.close();
}
}catch(IOException e){
e.printStackTrace();
}
}
}