Struts Learning Note (3) accessing SERVLETAPI in---action gets the elements of the real type

Source: Internet
Author: User

First, the source code:

Struts.xml file:

<?xml version= "1.0" encoding= "UTF-8"? ><! DOCTYPE struts Public "-//apache software foundation//dtd struts Configuration 2.3//en" "http://struts.apache.org/dtds/ Struts-2.3.dtd "><struts><constant name=" struts.action.extension "value=" Action,do, "/><constant Name= "Struts.devmode" value= "true"/><constant name= "Struts.enable.DynamicMethodInvocation" value= "true"/ ><package name= "Default" namespace= "/" extends= "Struts-default" ><default-action-ref name= "Default"/ ><action name= "test1" class= "org. Test.action.TestAction1 "method=" test "><result>/success.jsp</result></action><action Name= "test2" class= "org. Test.action.TestAction2 "method=" test "><result>/success.jsp</result></action><action Name= "test3" class= "org. Test.action.TestAction3 "method=" test "><result>/success.jsp</result></action><action Name= "test4" class= "org. Test.action.TestAction4 "method=" test "><result>/succEss.jsp</result></action><action name= "Default" ><result>/index.jsp</result></ Action></package></struts>

success.jsp page:

<%@ page language= "java" import= "java.util.*" pageencoding= "UTF-8"%><! DOCTYPE HTML PUBLIC "-//w3c//dtd HTML 4.01 transitional//en" >


method One: Access by Servletactioncontext

Testaction3.java file:

Package Org. Test.action;import Java.io.ioexception;import Java.io.printwriter;import Java.util.map;import Javax.servlet.servletcontext;import Javax.servlet.http.httpservletrequest;import Javax.servlet.http.httpservletresponse;import Javax.servlet.http.httpsession;import Org.apache.struts2.servletactioncontext;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 TestAction3 extends Actionsupport {private httpservletrequest Request;private httpservletresponse response;private HttpSession session;private servletcontext application;public TestAction3 () {///constructor gets four objects in this.request = Servletactioncontext.getrequest (); this.response = Servletactioncontext.getresponse (); this.session = This.request.getSession (); this.application = Servletactioncontext.getservletcontext ();} Public String test () throws IOException {System.out.println ("testAction3:test "); This.request.setAttribute (" Name "," Jack "), This.session.setAttribute (" name "," Rose "); This.application.setAttribute ("name", "Dean"); return SUCCESS;}}

Operation Result:



Description:

1.testaction3 's
Class: Request,response,session,application object that defines the real type
Construction method: Get request,response and applicationthrough Servletactioncontext, session can be by Request.getsession ();
Test () Method: Set the property with the SetAttribute method
2.success.jsp: View the value of the Name property of the Request,session,application object through the El language



method Two: IOC (Control inversion), by implementing interface Servletrequestaware,servletresponseaware, Servletcontextaware to access
Testaction4.java File:

Package Org. Test.action;import Java.io.ioexception;import Java.io.printwriter;import Java.util.map;import Javax.servlet.servletcontext;import Javax.servlet.http.httpservletrequest;import Javax.servlet.http.httpservletresponse;import Javax.servlet.http.httpsession;import Org.apache.struts2.servletactioncontext;import Org.apache.struts2.interceptor.applicationaware;import Org.apache.struts2.interceptor.requestaware;import Org.apache.struts2.interceptor.servletrequestaware;import Org.apache.struts2.interceptor.servletresponseaware;import Org.apache.struts2.interceptor.sessionaware;import Org.apache.struts2.util.servletcontextaware;import Com.opensymphony.xwork2.actionsupport;public class TestAction4 Extends Actionsupport implements Servletrequestaware,servletresponseaware, Servletcontextaware {private HttpServletRequest request;private httpservletresponse response;private HttpSession session;private ServletContext Application; @Overridepublic void Setservletresponse (HttpServletResponse resPonse) {//Implement the Set method of the interface, initialize responsethis.response = response;} @Overridepublic void Setservletrequest (HttpServletRequest request) {this.request = Request;} @Overridepublic void Setservletcontext (ServletContext application) {this.application = Application;} Public String test () throws IOException {this.session = This.request.getSession (); System.out.println ("Testaction4:test"); This.request.setAttribute ("Name", "slag"); This.session.setAttribute ("name") , "genius"); This.application.setAttribute ("Name", "Learning God"); return SUCCESS;}}

Operation Result:



Description:

1. instantiate the Request,response,application object by implementing the Set method of the interface, and TestAction4 itself does not instantiate three objects. The session is instantiated by Request.getsession ().

Struts Learning Note (3) accessing SERVLETAPI in---action gets the elements of the real type

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.