Four methods of obtaining request and session in action

Source: Internet
Author: User

Four methods of obtaining request and session in action

In struts2, obtaining requests from actions and applying session objects are necessary steps in development. How can we obtain these objects from actions?
Struts2 provides four methods for us.
Are
Methods for obtaining objects such as request by non-IOC related to Servlet
Methods for obtaining objects such as requests from unrelated servlet IOC
Servlet-related non-IOC methods for obtaining objects such as requests
Servlet-related IOC methods for obtaining objects such as requests
The following is a separate description.

First, check the Struts. xml file.
File Content:
<? XML version = "1.0" encoding = "UTF-8"?>
<! Doctype struts public
"-// Apache Software Foundation // DTD struts configuration 2.0 // en"
Http://struts.apache.org/dtds/struts-2.0.dtd>

<Struts>

<Package name = "default" extends = "struts-Default">
<Action name = "login" class = "struts2.login. loginaction">
<Result name = "success"> result. jsp </result>
</Action>

<Action name = "login2" class = "struts2.login. login2action">
<Result name = "success"> result. jsp </result>
</Action>

<Action name = "login3" class = "struts2.login. login3action">
<Result name = "success"> result. jsp </result>
</Action>

<Action name = "login4" class = "struts2.login. login4action">
<Result name = "success"> result. jsp </result>
</Action>
</Package>

</Struts>

Loginaction, login2action, login3action, and login4action correspond to four actions.

File Name: result. jsp
File Content:
<% @ Page contenttype = "text/html; charset = gb2312" %>
<% @ Taglib uri = "/Struts-tags" prefix = "S" %>


the request attribute is $ {requestscope. attribute}

the session attribute is $ {sessionscope. attribute}

the application attribute is $ {applicationscope. attribute }< br>

File Name: getrequest. jsp
File Content:
<% @ Page contenttype = "text/html; charset = gb2312" %>
<% @ Taglib uri = "/Struts-tags" prefix = "S" %>
<HTML>
<A href = "login. Action"> methods for obtaining objects such as requests from unrelated non-IOC servlet </a> <br>
<A href = "login2.action"> methods for obtaining objects such as requests from unrelated servlet IOC </a> <br>
<A href = "login3.action"> servlet-related non-IOC methods for obtaining objects such as requests </a> <br>
<A href = "login4.action"> servlet-related IOC methods for obtaining objects such as requests </a> <br>
</Form>
</Html>

Then we will describe the four methods respectively,
1. servlet-independent non-IOC method for obtaining objects such as request
In this method, the actioncontext is obtained first, and then the map-type request and other objects are obtained.

Action file:
Package struts2.login;

Import java. util. Map;

Import com. opensymphony. xwork2.actioncontext;
Import com. opensymphony. xwork2.actionsupport;

Public class loginaction extends actionsupport {
Private actioncontext context;
Private map request;
Private map session;
Private map application;

@ Override
Public String execute () throws exception {
// Todo auto-generated method stub
This. Context = actioncontext. getcontext ();
This. Request = (MAP) This. Context. Get ("request ");
This. Session = This. Context. getsession ();
This. Application = This. Context. getapplication ();
This. Request. Put ("attribute", "request value servlet unrelated non-IOC ");
This. session. Put ("attribute", "session value servlet unrelated non-IOC ");
This. application. Put ("attribute", "non-IOC related to application value servlet ");
Return success;
}
}

2. servlet-independent IOC methods for obtaining objects such as requests
This method is used to implement specific interfaces, and the container is used to set objects such as requests. Note the following:CodeThe red text section in the example.

Action file:
Package struts2.login;

Import java. util. Map;

Import org. Apache. struts2.interceptor. applicationaware;
Import org. Apache. struts2.interceptor. requestaware;
Import org. Apache. struts2.interceptor. sessionaware;

Import com. opensymphony. xwork2.actionsupport;

Public class login2action extends actionsupport
Implements requestaware, sessionaware, applicationaware {
Private map request;
Private map session;
Private map application;

@ Override
Public String execute () throws exception {
// Todo auto-generated method stub
This. Request. Put ("attribute", "request value servlet unrelated IOC ");
This. session. Put ("attribute", "session value servlet unrelated IOC ");
This. application. Put ("attribute", "Application Value servlet unrelated IOC ");
Return success;
}

@ Override
Public void setrequest (Map <string, Object> arg0 ){
// Todo auto-generated method stub
This. Request = arg0;
}

@ Override
Public void setsession (Map <string, Object> arg0 ){
// Todo auto-generated method stub
This. Session = arg0;
}

@ Override
Public void setapplication (Map <string, Object> arg0 ){
// Todo auto-generated method stub
This. Application = arg0;
}
}

3. servlet-related non-IOC methods for obtaining objects such as request
This method can be used to obtain servlet-related requests and other objects. That is to say, the request object retrieved in this method is not only used to set attributes, but can also obtain http-related information.
For more information, see the blue text section in the code.

Action file:
Package struts2.login;

Import javax. servlet. servletcontext;
Import javax. servlet. http. httpservletrequest;
Import javax. servlet. http. httpsession;

Import org. Apache. struts2.servletactioncontext;

Import com. opensymphony. xwork2.actionsupport;

Public class login3action extends actionsupport {
Private httpservletrequest request;
Private httpsession session;
Private servletcontext application;

@ Override
Public String execute () throws exception {
// Todo auto-generated method stub
This. Request = servletactioncontext. getrequest ();
This. Session = This. Request. getsession ();
This. Application = servletactioncontext. getservletcontext ();
This. Request. setattribute ("attribute", "request value servlet-related non-IOC ");
This. session. setattribute ("attribute", "session value servlet-related non-IOC ");
This. application. setattribute ("attribute", "non-IOC related to application value servlet ");
Return success;
}

}

4. servlet-related IOC methods for obtaining objects such as request

This method can also obtain servlet-related requests and other objects. In other words, the request object retrieved in this method is not only used to set attributes, but can also obtain http-related information.
But the retrieval method is implemented through the interface, that is, it is set by the container of struts2. Please refer to the red part in the code.

Action file:
Package struts2.login;

Import javax. servlet. servletcontext;
Import javax. servlet. http. httpservletrequest;
Import javax. servlet. http. httpsession;

Import org. Apache. struts2.interceptor. servletrequestaware;
Import org. Apache. struts2.util. servletcontextaware;

Import com. opensymphony. xwork2.actionsupport;

Public class login4action extends actionsupport
Implements servletrequestaware, servletcontextaware {
Private httpservletrequest request;
Private httpsession session;
Private servletcontext application;

@ Override
Public String execute () throws exception {
// Todo auto-generated method stub
This. Request. setattribute ("attribute", "IOC related to request value servlet ");
This. Session = This. Request. getsession ();
This. session. setattribute ("attribute", "session value servlet-related IOC ");
This. application. setattribute ("attribute", "Application Value servlet-related IOC ");
Return success;
}

@ Override
Public void setservletrequest (httpservletrequest arg0 ){
// Todo auto-generated method stub
This. Request = arg0;
}

@ Override
Public void setservletcontext (servletcontext arg0 ){
// Todo auto-generated method stub
This. Application = arg0;
}
}

Well, let's take a look at the execution result graph. I don't know how many methods you know about taking objects such as request in action?

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.