Struts2 得到HTTPServletRequest

创建一个action类,HeaderReader.java 

 

 

ackage info.icontraining.struts2;

import java.util.Enumeration;

import javax.servlet.http.HttpServletRequest;
import org.apache.struts2.interceptor.ServletRequestAware;
import com.opensymphony.xwork2.ActionSupport;

public class HeaderReader extends ActionSupport implements ServletRequestAware {

   private HttpServletRequest request;
 
   public void setServletRequest(HttpServletRequest request) {
      this.request = request;
   }
 
   public String execute() {

      Enumeration e = request.getHeaderNames();
      String headerName = null;

      while(e.hasMoreElements()) {
         headerName = (String) e.nextElement();
         System.out.println(headerName + ": "); 
         System.out.println(request.getHeader(headerName) + "\n");
      }

      return SUCCESS;
   } 
}

 

配置Action

 

<action name="headerReader" class="info.icontraining.struts2.HeaderReader">

   <result>headers.html</result> 

</action>

 

 

你可能感兴趣的:(Struts2 得到HTTPServletRequest)