Java EE----Struts2 Action access servlet API

Source: Internet
Author: User

Previously Servlet doget (Httpservletrequest,httpservletresponse), the Doget method could not be tested individually, and the request and response objects must be constructed to test


The Execute method of the Struts2 action is very easy to test independently, and this method does not rely on the servlet API


Servlet Common API objects: HttpServletRequest, HttpSession, ServletContext (three server-side data ranges), HttpServletResponse


1. Using Actioncontext with servlet API decoupling operation
1) Create Instance Actioncontext.getcontext ()
2) Get request parameter Actioncontext.getparameters () return map<string,string[]> representative request parameter Map equivalent to Request.getparametermap
3) Save a data actioncontext.put (Key,value) to the request; Equivalent to Request.setattribute ()
4) A Data actioncontext.get (key) reading the request range is equivalent to Request.getattribute ()
5) Read Session property Actioncontext.getsession () returns map Actioncontext.getsession (). Get (key) is equivalent to Session.getattribute ();
Actioncontext.getsession (). put (Key,value) equivalent to Session.setattribute ()
6) Read and write Servletcontexnt properties actioncontext.getapplication return to Map actioncontext.getapplication (). Get (Key) Equivalent to Servletcontext.getattribute
Actioncontext.getapplication (). put (Key,value) level in Servletcontext.setattribute


* The Servlet object was manipulated using Actioncontext, but the servlet API was not used
* For example: destroying session objects, adding cookies cannot be done by Actioncontext


2. Direct access to SERVLETAPI (Scheme one interface injection)
Servletcontextaware
void Setservletcontext (Javax.servlet.ServletContext context)


Servletrequestaware
void Setservletrequest (Javax.servlet.http.HttpServletRequest request)


Servletresponseaware
void Setservletresponse (Javax.servlet.http.HttpServletResponse response)


3. Direct access to the servlet API (scenario two: Servletactioncontext)
Static PageContext Getpagecontext ()
Static HttpServletRequest Getrequest ()
Static HttpServletResponse GetResponse ()
Static ServletContext Getservletcontext ()

Java EE----Struts2 Action access servlet API

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.