Add and retrieve ServletContext, and retrieve servletcontext

Source: Internet
Author: User

Add and retrieve ServletContext, and retrieve servletcontext

(1) Understanding of ServletContext:




(2) Add attributes to servletcontext

Package com. tsinghua; import javax. servlet. http. *; import javax. servlet. *; import java. io. *; public class ServletContextTest1 extends HttpServlet {// process get requests // req: used to obtain information about the client (browser) // res: used to send information to the client (browser) public void doGet (HttpServletRequest req, HttpServletResponse res) {// business logic try {// Chinese garbled res. setContentType ("text/html; charset = gbk"); PrintWriter pw = res. getWriter (); // 1 to get servletcontextServletContext SC = this. getServletContext (); // 2 Add the SC attribute. setAttribute ("myInfo", "I am xx"); pw. println ("add a property myInfo to sevlet context. The value corresponding to this property is a string I am xx <br>"); // compare sessionHttpSession hs = req. getSession (true); hs. setAttribute ("test", "Chinese"); hs. setMaxInactiveInterval (60*3); pw. println ("add a test attribute to the session. Its value is Chinese <br> "); /// = Of course, you can also add an object in servletcontext // Cat myCat = new Cat ("James", 30); // SC. setAttribute ("cat1", myCat); // pw. println ("add an attribute cat1 to sevlet context. The value of this attribute is a cat object <br>");} catch (Exception ex) {ex. printStackTrace () ;}/// process post requests // req: used to obtain client (browser) Information // res: used to send to client (browser) 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) Get attributes from servletcontext

Package com. tsinghua; import javax. servlet. http. *; import javax. servlet. *; import java. io. *; public class ServletContextTest2 extends HttpServlet {// process get requests // req: used to obtain information about the client (browser) // res: used to send information to the client (browser) public void doGet (HttpServletRequest req, HttpServletResponse res) {// business logic try {// Chinese garbled res. setContentType ("text/html; charset = gbk"); PrintWriter pw = res. getWriter (); // 1 to get servlet contextServletContext SC = this. getServletContext (); // 2 get the property and its corresponding value String info = (String) SC. getAttribute ("myInfo"); pw. println ("obtain the property myinfo from the servlet context. Its value is" + info + "<br>"); // compare sessionHttpSession hs = req. getSession (true); String val = (String) hs. getAttribute ("test"); pw. println ("test =" + val + "<br>" in the session); // obtain another property // Cat myCat = (Cat) SC. getAttribute ("cat1"); // pw. println ("Get attribute cat1 from servlet context. Its name is" + // myCat. getName () + "his age is" + myCat. getAge () + "<br>");} catch (Exception ex) {ex. printStackTrace () ;}/// process post requests // req: used to obtain client (browser) Information // res: used to send to client (browser) public void doPost (HttpServletRequest req, HttpServletResponse res) {this. doGet (req, res );}}



How can I get the corresponding servlet through servletContext?

The session can only be obtained through request. getSession ().
ServletContext indicates that the context cannot get the session through it.

How does struts2 obtain ServletContext?

Implement your class ServletContextAware.
 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.