Struts 2 accessing HttpServletRequest object in Controller


        All Actions that want to have access to the servlet request object must implement ServletRequestAware interface.
        And Override void setServletRequest(javax.servlet.http.HttpServletRequest request)
        The method setServletRequest will set all the HTTP request object in implementing classes.

Code
package com.candidjava.controller;

import javax.servlet.http.HttpServletRequest;

import org.apache.struts2.ServletActionContext;
import org.apache.struts2.interceptor.ServletRequestAware;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;

public class EmployeeAction extends ActionSupport implements ServletRequestAware {
	  /**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	HttpServletRequest request;
	  public void setServletRequest(HttpServletRequest req) {
	    request = req;
	  }

	  @Override
	  public String execute() throws Exception {
	  String code = request.getParameter("code");
	  String name = request.getParameter("name");
	  String city = request.getParameter("city");
	  String salary = request.getParameter("salary");
	  request.setAttribute("code", code);
	  request.setAttribute("name", name);
	  request.setAttribute("city", city);
	  request.setAttribute("salary", salary);
	
	  return SUCCESS;
	  }
	}

Screenshot
        
        
Download
    Struts 2 servletrequestaware war
    Struts 2 servletrequestaware maven zip




Related Post

Comments


©candidjava.com