ServletContext的添加與擷取,servletcontext擷取

來源:互聯網
上載者:User

ServletContext的添加與擷取,servletcontext擷取

(1)關於對ServletContext的理解:




(2)向servletcontext中添加屬性

package com.tsinghua;import javax.servlet.http.*;import javax.servlet.*;import java.io.*;public class ServletContextTest1 extends HttpServlet {//處理get請求//req: 用於獲得用戶端(瀏覽器)的資訊//res: 用於向用戶端(瀏覽器)返回資訊public void doGet(HttpServletRequest req,HttpServletResponse res){//商務邏輯 try {//中文亂碼res.setContentType("text/html;charset=gbk");PrintWriter pw=res.getWriter();//1得到servletcontextServletContext sc=this.getServletContext();//2添加屬性 sc.setAttribute("myInfo","我是xx");pw.println ("給sevlet context中添加一個屬性 myInfo 該屬性對應的值是一個字串我是xx<br>");//比較sessionHttpSession hs=req.getSession(true);hs.setAttribute("test","中國人");hs.setMaxInactiveInterval(60*3);pw.println("向session中添加一個test屬性 他的值是 中國人<br>");//===當然也可以在servletcontext中放入一個對象//Cat myCat=new Cat("小明",30);////sc.setAttribute("cat1",myCat);//////pw.println ("給sevlet context中添加一個屬性 cat1 該屬性對應的值是一個貓對象<br>");    }    catch (Exception ex) {        ex.printStackTrace();    }}//處理post請求//req: 用於獲得用戶端(瀏覽器)的資訊//res: 用於向用戶端(瀏覽器)返回資訊public void doPost(HttpServletRequest req,HttpServletResponse res){this.doGet(req,res);}}class Cat{private String name;private int age;public Cat(String name,int age){this.name=name;this.age=age;}public String getName(){return this.name;}public int getAge(){return this.age;}}

(3)從servletcontext中得到屬性

package com.tsinghua;import javax.servlet.http.*;import javax.servlet.*;import java.io.*;public class ServletContextTest2 extends HttpServlet {//處理get請求//req: 用於獲得用戶端(瀏覽器)的資訊//res: 用於向用戶端(瀏覽器)返回資訊public void doGet(HttpServletRequest req,HttpServletResponse res){//商務邏輯 try {//中文亂碼res.setContentType("text/html;charset=gbk");PrintWriter pw=res.getWriter();//1得到servlet contextServletContext sc=this.getServletContext();//2得到屬性和它對應的值String info=(String)sc.getAttribute("myInfo");pw.println ("從servlet context中得到屬性 myinfo 它對應的值是"+info+"<br>");//比較sessionHttpSession hs=req.getSession(true);String val=(String)hs.getAttribute("test");pw.println("session 中的 test="+val+"<br>");//得到另外一個屬性//Cat myCat=(Cat)sc.getAttribute("cat1");////pw.println ("從servlet context中得到屬性 cat1 他的名字是"+//myCat.getName()+" 他的年齡是"+myCat.getAge()+"<br>");    }    catch (Exception ex) {        ex.printStackTrace();    }}//處理post請求//req: 用於獲得用戶端(瀏覽器)的資訊//res: 用於向用戶端(瀏覽器)返回資訊public void doPost(HttpServletRequest req,HttpServletResponse res){this.doGet(req,res);}}



怎通過servletContext得到對應的servlet?

session只能通過request.getSession()擷取
servletContext是上下文 不能通過它擷取session
 
struts2 怎擷取 ServletContext

把你的類實現ServletContextAware 就可以了
 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.