A detailed explanation of the built-in objects in the JSP and the Web resources in struts

Source: Internet
Author: User

There are several built-in objects in the JSP:

Request : Inherit from HttpServletRequest, HttpServletRequest inherit ServletRequest,

Method of obtaining the Request object: Gets only in the Doget () and Dopost () methods obtained in the servlet

Role: Encapsulate user request information

response : Inherit from HttpServletResponse, HttpServletResponse inherit Servletresponse

Methods for obtaining response objects: only the Doget () and Dopost () methods obtained in the servlet are obtained

Role: Encapsulates the information returned by the server

session : inherited from HttpSession

Get session pair Image: Use request in Doget () and Dopost () methods that can only be obtained in the servlet

Role: Specify a session process with the server for each user, saving information about the session process

out: inherited from the JspWriter object,

Method of obtaining an Out object: Response.getwrite ()

Function: Print a string to a specific page, Ajax is the return information

Application : inherited from: Javax.servle.ServletContext

Ways to get Application objects:

Role: is a shared object, the whole site can be used,

PageContext :

Page:

Config:

exception

Access to Web resources in Struts2, which is the access in JSP built-in objects

  1. Decoupling method (Decoupling method: not fully inherited JSP built-in objects, only partial functionality,), the decoupling method has the following two kinds of
    1)-------by actioncontext (only get Request,session,application)

    ① get Actioncontext object: Actioncontext actioncontext=actioncontext.getactioncontext ();
    ② Gets the XX object and encapsulates it in Map: map<string,object> xxmap=actioncontext.getxx ();
    Request object Comparison special point: Map<string,object> requestmap= (map<stirng,object>) actioncontext.get ("request");
    ③ can then use the put (), get () of the map object to set and get the properties
    Xxxxmap.put ("Requestkey", "Requestvalue");
    Xxxmap.get ("Xxxkey");
    code example:

    1  Packagecom. Web.actioncontext;2 ImportJava.util.*;3 4 ImportCom.opensymphony.xwork2.ActionContext;5 6  Public classTextactioncontext {7     //principle by calling Actoncontext's GetXXX () method, XXX represents the built-in object of the JSP8     //---> Then call (map<string, object>) get (XXX), ... public static final String XXX = " Com.opensymphony.xwork2.ActionContext.XXX "9    returns a list of map<string,xxx> One     //so the put and get methods of map are finally manipulated by XXX objects. A      PublicString Execute () { -Actioncontext actioncontext=Actioncontext.getcontext (); -Map<string,object> applicationmap=actioncontext.getapplication (); theApplicationmap.put ("Applicationkey", "Applicationvalue"); -     //fetch of the request object -Map<string, object> requestmap= (map<string, object>) actioncontext.get ("Request"); -     return"Sucess"; +     } -      +}

    2)----method to implement the Xxxaware interface (most built-in objects can be obtained)
    ① Write a new Java class to inherit the Xxxaware interface and add the methods that need to be implemented
    Implement Xxxaware
    ② Declaration XXX Object
    Private map<sting,object> XXX-------->private map<string,object> request;
    and declare the Setxxx () method;
    ③ use the map's put, get to set the property;
    Notice----1) The corresponding map of the session is actually a sessionmap type, and if you call the Invalidate () method after casting, the session will be invalidated.
    2) If an action has multiple action methods, and each action method needs to use the Web resource object, the Actionaware interface is recommended

    1  Packagecom. Web.actionaware;2 3 ImportJava.util.Map;4 5 ImportOrg.apache.struts2.dispatcher.SessionMap;6 ImportOrg.apache.struts2.interceptor.ApplicationAware;7 ImportOrg.apache.struts2.interceptor.RequestAware;8 ImportOrg.apache.struts2.interceptor.SessionAware;9 Ten  Public classTextactionawareImplementsrequestaware,applicationaware,sessionaware{//Inheritance Interface One     PrivateMap<string, object>REQUESTMAP;//2 declaring a Requestmap object A     PrivateMap<string, object>Applicationmap; -     PrivateMap<string, object>Sessionmap; -      Public voidSetrequest (map<string, object>arg0) {//3 setting objects for Requestmap objects the         //TODO auto-generated Method Stub -          This. requestmap=arg0; -          -     } +      Public voidSetapplication (map<string, object>arg0) { -         //TODO auto-generated Method Stub +          This. applicationmap=arg0; A          at     } -      PublicString Execute () { -Requestmap.put ("Requestawarekey", "Requestawarevalue");//4 Setting Properties -Applicationmap.put ("Applicationawarekey", "Applicationawarevalue"); -Sessionmap.put ("Sessionawarekey", "Sessionawarevalue"); -         if(SessionmapinstanceofSessionmap) {//Here is to verify that the session object of the map type is not a real JSP built-in object, inSessionmap sm=(Sessionmap) sessionmap; - sm.invalidate (); to         } +         return"Sucess"; -     } the      Public voidSetsession (map<string, object>arg0) { *         //TODO auto-generated Method Stub $          This. sessionmap=arg0;Panax Notoginseng     } -  the}

  2. Coupling way: There are also the following two kinds of
    1)------Through native use SERVLETAPI-----servletactioncontext (most built-in objects can be obtained)

    Example:-------httpservletrequest request=servletactioncontext.getrequest ()
    Example:--------httpSession session=servletactioncontext.getrequest (). GetSession ();
    Example:--------ServletContext servletcontext=servletcontext.getservletcontext ();
    Similarities and differences of Notice:servletcontext and application
    Same: In fact ServletContext and application are the same, it is equivalent to a class created two different names of variables. In
    The ServletContext in the servlet is the Application object. All you have to do is open the JSP in the generated servlet after compilation
    The Jspservice () method can see the following declaration:
    ServletContext application = null;
    application = Pagecontext.getservletcontext ();
    Differences: The difference between the two is that application is used in JSPs and ServletContext is used in Servlets. Application and Page
    The request session is a built-in object in the JSP, and the property data stored in the background with ServletContext can be
    The Application object is obtained.
    code example

     Packagecom. Web.servletapi;ImportJavax.servlet.ServletContext;Importjavax.servlet.http.HttpSession;Importjavax.servlet.http.HttpServletRequest;ImportOrg.apache.struts2.ServletActionContext; Public classTextservletapi { PublicString Execute () {HttpServletRequest request=servletactioncontext.getrequest (); HttpSession Session=servletactioncontext.getrequest (). GetSession (); ServletContext ServletContext=Servletactioncontext.getservletcontext (); Request.setattribute ("Requestkey", "Servletapirequestvalue"); return"Sucess"; }}

    2)-------by implementing the Servletxxxaware interface you can inject the required native servlet objects by Struts2
    Example:---implements Servletrequestaware,servletcontextaware
    Notice: Do not call Actioncontext,getsession () in the member variable or constructor method and put it in other methods.

code example:

1  Packagecom. Web.servletxxxaware;2 3 ImportJavax.servlet.ServletContext;4 Importjavax.servlet.http.HttpServletRequest;5 ImportJavax.servlet.http.HttpServletResponse;6 Importjavax.servlet.http.HttpSession;7 8 ImportOrg.apache.struts2.interceptor.ServletRequestAware;9 ImportOrg.apache.struts2.interceptor.ServletResponseAware;Ten ImportOrg.apache.struts2.util.ServletContextAware; One  A  Public classTestservletxxxawareImplementsservletresponseaware,servletcontextaware,servletrequestaware{//interface to implement native built-in objects - httpservletrequest request;//2 declaring native objects - httpservletresponse response; the      Public voidSetservletresponse (httpservletresponse response) {//3 Set method for native object -         //TODO auto-generated Method Stub -          This. response=response; -          +          -     } +  A      Public voidSetservletcontext (ServletContext context) { at         //TODO auto-generated Method Stub -     } -  -      Public voidsetservletrequest (HttpServletRequest request) { -         //TODO auto-generated Method Stub -          This. request=request; in     } -      PublicString Execute () { toRequest.setattribute ("Requestkey", "Requestservletxxxawarevalue");//4 can call the native method directly +         return"Success"; -     } the}

A detailed explanation of the built-in objects in the JSP and the Web resources in struts

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.