Servlet API Request Response HttpSession

Servlet API Request Response HttpSession

In the context of Java web applications using frameworks like Struts or similar, accessing Servlet API objects such as HttpServletRequest, HttpServletResponse, and HttpSession within action classes is a common requirement. These objects provide access to the HTTP request, response, and session attributes, allowing developers to handle and manipulate user data and interactions effectively.

Servlet API Request Response HttpSession

Explanation

  • 1. HttpServletRequest:
    • Definition:  Represents the request made by the client to the server. It contains information about the request, such as parameters, headers, and attributes.
    • Usage:  Typically used to retrieve data submitted by the client (e.g., form data), request parameters, headers, and to set attributes that can be accessed by subsequent components.
  •  2. HttpServletResponse:
    • Definition:  Represents the response returned by the server to the client. It allows manipulation of response headers and content before it is sent back to the client.
    • Usage:  Used to set response headers, set cookies, and write content to the response stream (e.g., HTML or JSON responses).
  •  3. HttpSession:
    • Definition:  Represents a session object that stores information between HTTP requests from the same client. It allows storing and retrieving session attributes associated with a user’s session.
    • Usage:  Used to store and retrieve session-specific data, such as user authentication details, shopping cart items, etc.

Example in Java

Let’s demonstrate how you can access these Servlet API objects within a Struts action class.

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

public class MyAction extends ActionSupport implements ServletRequestAware, ServletResponseAware, SessionAware {
    
    private HttpServletRequest servletRequest;
    private HttpServletResponse servletResponse;
    private HttpSession session;

    // Action method
    public String execute() throws Exception {
        // Example usage of HttpServletRequest
        String username = servletRequest.getParameter("username");
        
        // Example usage of HttpServletResponse
        servletResponse.setContentType("text/plain");
        servletResponse.getWriter().write("Hello, " + username);
        
        // Example usage of HttpSession
        session.setAttribute("loggedInUser", username);
        
        return SUCCESS;
    }

    // Setter methods required by ServletRequestAware, ServletResponseAware, and SessionAware
    @Override
    public void setServletRequest(HttpServletRequest request) {
        this.servletRequest = request;
    }

    @Override
    public void setServletResponse(HttpServletResponse response) {
        this.servletResponse = response;
    }

    @Override
    public void setSession(Map<String, Object> sessionMap) {
        this.session = (HttpSession) sessionMap.get("session");
    }
}

Explanation

  • 1.  Implementing Aware Interfaces:
    • The action class MyAction implements ServletRequestAware, ServletResponseAware, and SessionAware interfaces. This allows Struts to inject the corresponding Servlet API objects (HttpServletRequest, HttpServletResponse, and HttpSession) into the action class.
  • 2.  Accessing Servlet API Objects:
    • In the execute() method, HttpServletRequest (servletRequest) is used to retrieve a parameter (username) submitted by the client.
    • HttpServletResponse (servletResponse) is used to set the content type and write a response back to the client.
    • HttpSession (session) is used to set a session attribute (loggedInUser) which can be accessed across multiple requests during the user’s session.
  • 3.  Setter Methods:
    • The setter methods (setServletRequest, setServletResponse, setSession) are overridden to receive the corresponding Servlet API objects injected by Struts.

Conclusion

Using Struts and implementing the ServletRequestAware, ServletResponseAware, and SessionAware interfaces is a straightforward approach to access Servlet API objects within action classes. This approach ensures compatibility with Struts framework and allows seamless integration of Servlet API functionalities into your application logic.