Struts2 learning (3) Web resource acquisition

Mainly introduce four ways to obtain Web resources

1. Use Struts2Aware interceptor

To use the Struts2 Aware interceptor to obtain web resources, it must first be carried out in the Action , and then the ServletRequestAware, ServletResponseAware, and ServletContextAware interfaces must be implemented to obtain the corresponding ServletRequest , ServletResponse , and ServletContext objects.

package cn.lovepi.chapter03.action;

import com.opensymphony.xwork2.ActionSupport;
import org.apache.struts2.interceptor.ServletRequestAware;
import org.apache.struts2.interceptor.ServletResponseAware;
import org.apache.struts2.util.ServletContextAware;

import javax.servlet.ServletContext;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Created by icarus on 2016/7/9.
 * Use Struts2 aware interceptor to get web resources
 * First this is an action
 * Then you have to implement the corresponding interface to get the corresponding web class
 */
public class FirstAction extends ActionSupport implements ServletRequestAware,ServletResponseAware,ServletContextAware{
    // Set the corresponding request and response objects and context objects in the class
    private ServletRequest request;
    private ServletResponse response;
    private ServletContext context;
    /**
     * action execution method
     * @return
     * @throws Exception
     */
    @Override
    public String execute() throws Exception {
        String username=request.getParameter("username");
        System.out.println("The first interceptor gets resources: "+username);
        return "success";
    }

    /**
     * Set the ServletRequest passed to the action
     * @param httpServletRequest
     */
    @Override
    public void setServletRequest(HttpServletRequest httpServletRequest) {
        request=httpServletRequest;
    }

    /**
     * Set the ServletResponse passed to the action
     * @param httpServletResponse
     */
    @Override
    public void setServletResponse(HttpServletResponse httpServletResponse) {
        response=httpServletResponse;
    }

    /**
     * Set the ServletContext passed to the action, ie application
     * @param servletContext
     */
    @Override
    public void setServletContext(ServletContext servletContext) {
        context=servletContext;
    }
}


Second, use the Struts2RequestAware interceptor

Next, use the RequestAware interceptor to obtain web resources, as long as you implement the RequestAware interface, and then implement the setRequest method. The difference is that the ServletRequest , ServletResponse , and ServletContext objects are placed in a collection, and the key value in Struts2 needs to be used to obtain the corresponding object.


package cn.lovepi.chapter03.action;

import com.opensymphony.xwork2.ActionSupport;
import org.apache.struts2.StrutsStatics;
import org.apache.struts2.interceptor.RequestAware;

import javax.servlet.ServletContext;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import java.util.Map;

/**
 * Created by icarus on 2016/7/9.
 * Use RequestAware interceptor to get web resources
 * Need to implement RequestAware interface
 */
public class SecondAction extends ActionSupport implements RequestAware{
    private ServletResponse response;
    private ServletRequest request;
    private ServletContext context;

    /**
     * Corresponding to action method
     * @return
     * @throws Exception
     */
    @Override
    public String execute() throws Exception {
        String username=request.getParameter("username");
        System.out.println("The second method gets web resources: "+username);
        return "success";
    }

    /**
     * Get the corresponding web resources and use the map collection method
     * That is, key-value pair acquisition
     * @param map
     */
    @Override
    public void setRequest(Map<String, Object> map) {
        request= (ServletRequest) map.get(StrutsStatics.HTTP_REQUEST);
        response= (ServletResponse) map.get(StrutsStatics.HTTP_RESPONSE);
        context= (ServletContext) map.get(StrutsStatics.SERVLET_CONTEXT);
    }
}


Third, use Struts2 built-in static object ActionContext

This way to get web resources does not need to implement any interface


package cn.lovepi.chapter03.action;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import org.apache.struts2.ServletActionContext;

import javax.servlet.ServletContext;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;

/**
 * Created by icarus on 2016/7/9.
 * Struts2 gets the third method of web resource object
 * Use Struts2 built-in static object ActionContext
 * Use this method without implementing any interface
 * -----------------------------------
 * Note: HttpServlet is just a subclass of Servlet
 */
public class ThirdAction extends ActionSupport{
    @Override
    public String execute() throws Exception {
        ActionContext ac=ActionContext.getContext();
        ServletRequest request= (ServletRequest) ac.get(ServletActionContext.HTTP_REQUEST);
        ServletContext context= (ServletContext) ac.get(ServletActionContext.SERVLET_CONTEXT);
        ServletResponse response= (ServletResponse) ac.get(ServletActionContext.HTTP_RESPONSE);
        String username=request.getParameter("username");
        System.out.println("The third way: "+username);
        return "success";
    }
}

Fourth, use Struts2 built-in static object ServletActionContext

This is the easiest and most used method


package cn.lovepi.chapter03.action;

import com.opensymphony.xwork2.ActionSupport;
import org.apache.struts2.ServletActionContext;

import javax.servlet.ServletContext;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletResponse;

/**
 * Created by icarus on 2016/7/9.
 * The fourth way for Struts2 to get web resources
 * Use Struts2 built-in object ServletActionContext
 * This is the best of the four methods
 * You don't need to implement any interface, you can also get the required objects as needed
 */
public class FourthAction extends ActionSupport{
    @Override
    public String execute() throws Exception {
        ServletRequest request= ServletActionContext.getRequest();
        ServletResponse response=ServletActionContext.getResponse();
        ServletContext context=ServletActionContext.getServletContext();
        String username=request.getParameter("username");
        System.out.println("The fourth way: "+username);
        return "success";
    }
}

Summarize:

       Corresponding web resources can be obtained through four methods , but when using Aware interceptor, you need to implement the corresponding interface to obtain the corresponding resources, while when using RequestAware , you only need to implement one interface to obtain all web resource objects. There is no need to implement any interface for using the built-in static objects of Struts2 to obtain Web resources. Compared with the first two methods of using interceptors, the method of using built-in static objects is more recommended. In the latter two methods: using the built-in object ActionContext returns a map collection, and you need to use the corresponding key to obtain the required resource object. The ServletActionContext can directly obtain the corresponding web resource object according to different methods.

       Although using the built-in object ServletActionContext to obtain Web resources is the best way, it is recommended. However, we also have to understand the first three ways to obtain web resources, because in some projects, others may use the first three ways to obtain web resources. But in daily development, we must be proficient in the fourth way to obtain web resources, that is, the ServletActionContext way.


Additional instructions:

        ServletContext , that is, Application , the server object, as long as the server is not closed, this object will always exist. This information is stored in the server, and it is strictly forbidden to store general data in the application object, because this can easily lead to server memory overflow and program crash.

       His practical application scenario is: in the driving school test system, users only need to register to answer the questions for free, and the number of users is huge. On the page, only one question appears at a time, and the next question is displayed after the answer is completed. If our data processing each time is as shown in the following figure:


As shown in the figure above, we use the Application object here to store the corresponding topic information, which saves us the step of going to the database to get the next topic every time. In this case, as soon as the system starts, it will get all the topic information from the database and save it into the Application object, and then the user only needs to access the ServletContext , and then directly display the data in the corresponding Application in the interface.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325716884&siteId=291194637