I want to store a HttpServletRequest
object in a HttpSession
as an attribute.
protected void processRequest(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
request.getSession().setAttribute("ses",request);
Here is how I get it back in another servlet,
HttpSession ses=r.getSession(false);
HttpServletRequest rq=(HttpServletRequest)ses.getAttribute("ses");
Here rq is not nul when I check it in a if
statement. My problem is, when I try to get a parameter which is in the request object, a null-point exception is thrown. How can I store a request as I can get parameters back again?
This is not possible and it makes technically also no sense. The HTTP servlet request instance is expired/garbaged when the associated HTTP servlet response is committed and finished. The HTTP servlet request instance is therefore not valid anymore in a different request. Invoking any methods on it will throw exceptions in all colors because the stored instance does not point to anything anymore. The initial HTTP servlet request has already finished doing its job is already been expired/garbaged.
You need to extract exactly the information you need from the request and store that information in the session instead of trying to access it in a different request. E.g.
String foo = request.getParameter("foo");
request.getSession().setAttribute("foo", foo);
and then
String foo = (String) request.getSession().getAttribute("foo");