方法一:
Struts2的Action访问Servlet API
可以通过实现装配接口没,完成对Servlet API的访问
* ServletRequestAware取得HttpServletRequest对象
* ServletResponseAware取得HttpServletResponse对象
* ServletContextAware取得ServletContext对象(工具类)
package com.weiyuan.test; import javax.servlet.ServletContext; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.apache.struts2.interceptor.ServletRequestAware; import org.apache.struts2.interceptor.ServletResponseAware; import org.apache.struts2.util.ServletContextAware; /** * struct2的action可以不继承任何框架的接口 * 1、默认调用的是execute()方法 * */ public class LoginAction implements ServletRequestAware,ServletResponseAware,ServletContextAware { private String username; private String password; private HttpServletRequest request; private HttpServletResponse reponse; private ServletContext servletContext; public String getUsername() { return username; } public void setUsername(String username) { this.username = username; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } public String execute() throws Exception{ /** * * 这个地方就是可以进行和原来的操作了 * */ if("admin".equals(username)&&"admin".equals(password)){ return "success"; }else{ return "error"; } } @Override public void setServletContext(ServletContext servletContext) { // TODO Auto-generated method stub } @Override public void setServletResponse(HttpServletResponse reponse) { // TODO Auto-generated method stub } @Override public void setServletRequest(HttpServletRequest request) { // TODO Auto-generated method stub this.request = request; } }
方法二:
Struts2的Action访问Servlet API
可以通过ServletActionContext提供的静态方法取得Servlet API
* getPageContext();
* getRequest();
* getResponse();
* getServletContext();