user67722
user67722

Reputation: 3277

Objects of session and request

In struts, where does the session object created & which class or method creates it? Likewise where does the request object created & which class or method invokes it?

Thanks in advance

Upvotes: 0

Views: 338

Answers (3)

Harit Vishwakarma
Harit Vishwakarma

Reputation: 2441

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

public class MyAction extends ActionSupport implements SessionAware,ServletRequestAware
{
    Map<String,Object> session;
    HttpServletRequest servletRequest;

    public void setSession(Map<String, Object> session) {

        this.session = session;
    }

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

    public String execute()
    {
       return SUCCESS;
    }
}

Whenever this action is called setServletRequest is called first and then setSession().

The object of request & session are created by the web container you are using and passed to corresponding methods.

Upvotes: 1

oscargm
oscargm

Reputation: 61

In Struts an ActionForm instance can be stored in the HttpSession or in the HttpServletRequest.

It depends on the scope defined in the action tag inside the struts-config.xml file.

An ActionForm usually gets stored using the name defined in the action-form tag.

Upvotes: 0

krosenvold
krosenvold

Reputation: 77171

The request object is created inside your servlet container (tomcat/jetty/whatever).

The session is basically created by whoever calls getSession on HttpServletRequest first. Normally web frameworks do this only when someone actually declares they need the session. If you want to find out when this happens, I suggest you use "go to implementation" in your IDE and set a breakpoint and run the application (note there are two overloads)

Upvotes: 1

Related Questions