Get HttpServletRequest Struts 2

Get HttpServletRequest Struts 2

To access HttpServletRequest object in struts 2 either you use ServletActionContext or implement ServletRequestAware as below:

  • Using ServletActionContext
package com.javahonk.action;

import javax.servlet.http.HttpServletRequest;

import org.apache.struts2.ServletActionContext;
import org.apache.struts2.convention.annotation.Action;

public class GetHttpServletRequestAction {

	@Action(value = "/HttpServletRequest")
	public void downloadCSVFileAction() throws Exception {
		
		HttpServletRequest request = ServletActionContext.getRequest();
		
	}

}
  • By implementing ServletRequestAware interface:
package com.javahonk.action;

import javax.servlet.http.HttpServletRequest;

import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.interceptor.ServletRequestAware;

public class HttpServletRequestAction implements ServletRequestAware {

	private HttpServletRequest request;

	@Action(value = "/HttpServletRequestAction")
	public void downloadCSVFileAction() throws Exception {

		HttpServletRequest request = getRequest();
	}

	public HttpServletRequest getRequest() {
		return request;
	}

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

}
  • For more details struts 2 please refer its official tutorial

Leave a Reply

Your email address will not be published. Required fields are marked *