ServletContext的添加与获取

(1)关于对ServletContext的理解:




(2)向servletcontext中添加属性

[java]  view plain  copy
  1. package com.tsinghua;  
  2.   
  3. import javax.servlet.http.*;  
  4.   
  5. import javax.servlet.*;  
  6.   
  7. import java.io.*;  
  8.   
  9. public class ServletContextTest1 extends HttpServlet {  
  10.       
  11.   
  12.     //处理get请求  
  13.     //req: 用于获得客户端(浏览器)的信息  
  14.     //res: 用于向客户端(浏览器)返回信息  
  15.     public void doGet(HttpServletRequest req,HttpServletResponse res){  
  16.           
  17.         //业务逻辑   
  18.           
  19.         try {  
  20.                       
  21.             //中文乱码  
  22.             res.setContentType("text/html;charset=gbk");  
  23.               
  24.             PrintWriter pw=res.getWriter();  
  25.               
  26.             //1得到servletcontext  
  27.           
  28.             ServletContext sc=this.getServletContext();  
  29.               
  30.             //2添加属性   
  31.             sc.setAttribute("myInfo","我是xx");  
  32.               
  33.             pw.println ("给sevlet context中添加一个属性 myInfo 该属性对应的值是一个字符串我是xx<br>");  
  34.               
  35.               
  36.             //比较session  
  37.             HttpSession hs=req.getSession(true);  
  38.             hs.setAttribute("test","中国人");  
  39.             hs.setMaxInactiveInterval(60*3);  
  40.             pw.println("向session中添加一个test属性 他的值是 中国人<br>");     
  41.               
  42.               
  43.             //===当然也可以在servletcontext中放入一个对象  
  44. //          Cat myCat=new Cat("小明",30);  
  45. //            
  46. //          sc.setAttribute("cat1",myCat);  
  47. //            
  48. //            
  49. //          pw.println ("给sevlet context中添加一个属性 cat1 该属性对应的值是一个猫对象<br>");  
  50.               
  51.         }  
  52.         catch (Exception ex) {  
  53.               
  54.             ex.printStackTrace();  
  55.         }  
  56.     }  
  57.       
  58.     //处理post请求  
  59.     //req: 用于获得客户端(浏览器)的信息  
  60.     //res: 用于向客户端(浏览器)返回信息  
  61.     public void doPost(HttpServletRequest req,HttpServletResponse res){  
  62.           
  63.         this.doGet(req,res);  
  64.           
  65.     }  
  66. }  
  67.   
  68. class Cat{  
  69.       
  70.     private String name;  
  71.     private int age;  
  72.     public Cat(String name,int age){  
  73.           
  74.         this.name=name;  
  75.         this.age=age;  
  76.     }  
  77.       
  78.     public String getName(){  
  79.         return this.name;  
  80.     }  
  81.       
  82.     public int getAge(){  
  83.           
  84.         return this.age;  
  85.     }  
  86. }  

(3)从servletcontext中得到属性

[java]  view plain  copy
  1. package com.tsinghua;  
  2.   
  3. import javax.servlet.http.*;  
  4.   
  5. import javax.servlet.*;  
  6.   
  7. import java.io.*;  
  8.   
  9.   
  10. public class ServletContextTest2 extends HttpServlet {  
  11.       
  12.   
  13.     //处理get请求  
  14.     //req: 用于获得客户端(浏览器)的信息  
  15.     //res: 用于向客户端(浏览器)返回信息  
  16.     public void doGet(HttpServletRequest req,HttpServletResponse res){  
  17.           
  18.         //业务逻辑   
  19.           
  20.         try {  
  21.                       
  22.             //中文乱码  
  23.             res.setContentType("text/html;charset=gbk");  
  24.               
  25.             PrintWriter pw=res.getWriter();  
  26.               
  27.             //1得到servlet context  
  28.               
  29.             ServletContext sc=this.getServletContext();  
  30.               
  31.             //2得到属性和它对应的值  
  32.               
  33.             String info=(String)sc.getAttribute("myInfo");  
  34.               
  35.             pw.println ("从servlet context中得到属性 myinfo 它对应的值是"+info+"<br>");  
  36.               
  37.               
  38.           
  39.               
  40.             //比较session  
  41.             HttpSession hs=req.getSession(true);  
  42.             String val=(String)hs.getAttribute("test");  
  43.             pw.println("session 中的 test="+val+"<br>");  
  44.               
  45.               
  46.             //得到另外一个属性  
  47. //          Cat myCat=(Cat)sc.getAttribute("cat1");  
  48. //            
  49. //          pw.println ("从servlet context中得到属性 cat1 他的名字是"+  
  50. //          myCat.getName()+" 他的年龄是"+myCat.getAge()+"<br>");  
  51.               
  52.           
  53.                   
  54.         }  
  55.         catch (Exception ex) {  
  56.               
  57.             ex.printStackTrace();  
  58.         }  
  59.     }  
  60.       
  61.     //处理post请求  
  62.     //req: 用于获得客户端(浏览器)的信息  
  63.     //res: 用于向客户端(浏览器)返回信息  
  64.     public void doPost(HttpServletRequest req,HttpServletResponse res){  
  65.           
  66.         this.doGet(req,res);  
  67.           
  68.     }  

猜你喜欢

转载自blog.csdn.net/try_and_do/article/details/79849269