Implementing the Xxxaware interface to directly access the servlet API

Source: Internet
Author: User

Although STRUTS2 provides the Actioncontex class to access the Servlet API, it is not possible to obtain a Servlet API instance directly, in order to make
STRUTS2 has direct access to an instance of the Servlet API, and STRUTS2 also provides an interface:
Servletcontextaware implements the action for this interface, and can directly access the ServletContext in the Web App
Servletrequestaware implements the action of this interface, which can directly access the user's requested HttpServletRequest
Servletresponseaware implements the action for this interface, and can directly access the HttpServletResponse that responds to user requests
Implementing these interfaces requires only one more method. Implementing the Servletrequestaware interface requires implementing the public Setsetbletrequestaware (HttpServletRequest request). This method has a parameter httpservletrequest, which represents the user's request. Setsetbletrequestaware accesses the Servlet object in action by accessing the request object for the Web app and setting the object to the member property of the action, HttpServletRequest.
Examples are as follows:

Package com;


Import Javax.servlet.http.HttpServletRequest;
Import Org.apache.struts2.interceptor.ServletRequestAware;
Import Com.opensymphony.xwork2.ActionSupport;

public class Loginaction extends Actionsupport implements servletrequestaware{

private HttpServletRequest request;

@Override
Public String Execute () throws Exception {
Request.setattribute ("Success", "Walking in the Sun");
return SUCCESS;
}

@Override
public void Setservletrequest (HttpServletRequest request) {
This.request=request;

}
}

success.jsp output
<%@ page language= "java" import= "java.util.*" pageencoding= "Iso-8859-1"%>
<! DOCTYPE HTML PUBLIC "-//w3c//dtd HTML 4.01 transitional//en" >
<body>
${requestscope.success}
</body>

Implementing the Xxxaware interface to directly access the 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.