开发者

in JSP is there a way to pass HttpServletRequest object as an attribute to another HttpServletRequest object

I am using Struts/JSP for a webapp. I have a page A where I am collecting user defined parameters (as request parameters can't make them session params) and then I go to page B to ask yes/no kind of a question to the user. In case of yes I need to come back to page A and continue regular processing. But obviously the request object for page A is gone.

Is there a way to set page A's request object as parameter in page B so that when I come back to page A i have the same request object I had when i was there (on page A) the first time.

I need something like below:

page A --(req1)------> page B (set r开发者_开发知识库eq.setAttr('prevReq', req1)) ------> page A (req = req.getAttr('prevReq'))

Any help is appreciated.


No, you can't do what you have in mind. Do you understand how the HTTP request-response cycle works?

  • User sends HTTP request to the server using a browser.
  • Server processes the request (your servlet or JSP is called).
  • Your servlet or JSP produces a response which normally consists of an HTML page.
  • The server sends the response back to the browser.

There is no way that you can save the request for page A, and then in page B respond to that request to make the browser go back to page A. That's just not how the request-response cycle works.

What you can do, is store data in the session object. You can call request.getSession() to get a HttpSession object, in which you can store data for the duration of the session of that user. In page A, you can get the data out of the session object again.


In a multi page process you will need to store all the intermittently gathered data into the session. See HttpServletRequest.getSession() and HttpSession.setAttribute(String, Object).


Use hidden input elements (input type="hidden") wherein you retain the request parameters of the form submit. Don't duplicate/store it as request attribute. They get lost when the response is finished.

Since I don't do struts, here's a basic example how the JSP should look like (leaving input labels and obvious security issues like XSS outside consideration, Struts should be smart enough to handle it itself).

Page A:

<form>
    <input type="text" name="input1" value="${param.input1}">
    <input type="text" name="input2" value="${param.input2}">
    <input type="text" name="input3" value="${param.input3}">
    <input type="hidden" name="yesorno" value="${param.yesorno}">
    <input type="submit" value="go to page B">
    <input type="submit" value="submit">
</form>

Page B

<form>
    <input type="checkbox" name="yesorno" value="yes" ${!empty param.yesorno ? 'checked' : ''}>
    <input type="hidden" name="input1" value="${param.input1}">
    <input type="hidden" name="input2" value="${param.input2}">
    <input type="hidden" name="input3" value="${param.input3}">
    <input type="submit">
</form>
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜