Maintaining form elements when using a servlet to redirect?

Discussion in 'Java' started by Jim Bancroft, Nov 26, 2006.

  1. Jim Bancroft

    Jim Bancroft Guest

    Hi everyone,

    We're trying to use MVC in our J2EE application. We have a servlet that
    acts as the controller. All our pages post to this servlet, which is
    supposed to read one of the input values and forward to the appropriate
    page. My question: is it possible for the servlet to "forward" all of the
    form inputs to the page too?

    Our code looks something like this:

    String action = request.getParameter("action");
    String aURL = locateTheAppropriateURL(action);
    RequestDispatcher requestDispatcher =
    request.getRequestDispatcher(aURL);
    requestDispatcher.forward(request, response);

    I don't see any mechanism for automatically passing along any other
    form/request variables to the new page. Is there a good way of doing this?
    Thanks!
    Jim Bancroft, Nov 26, 2006
    #1
    1. Advertising

  2. Jim Bancroft

    Guest

    > We're trying to use MVC in our J2EE application. We have a servlet that
    > acts as the controller. All our pages post to this servlet, which is
    > supposed to read one of the input values and forward to the appropriate
    > page. My question: is it possible for the servlet to "forward" all of the
    > form inputs to the page too?
    >
    > Our code looks something like this:
    >
    > String action = request.getParameter("action");
    > String aURL = locateTheAppropriateURL(action);
    > RequestDispatcher requestDispatcher =
    > request.getRequestDispatcher(aURL);
    > requestDispatcher.forward(request, response);
    >
    > I don't see any mechanism for automatically passing along any other
    > form/request variables to the new page. Is there a good way of doing this?
    > Thanks!



    there are a couple options. my suggestion for you?
    place everything in "session".


    HttpSession session = request.getSession();
    session.setAttribute(request.getParam("param1");
    session.setAttribute(request.getParam("param2");
    session.setAttribute(request.getParam("param3");
    session.setAttribute(request.getParam("param4");
    , Nov 27, 2006
    #2
    1. Advertising

  3. Jim Bancroft

    Lew Guest

    Jim Bancroft wrote:
    >> We're trying to use MVC in our J2EE application. We have a servlet that
    >> acts as the controller. All our pages post to this servlet, which is
    >> supposed to read one of the input values and forward to the appropriate
    >> page. My question: is it possible for the servlet to "forward" all of the
    >> form inputs to the page too?
    >>
    >> Our code looks something like this:
    >>
    >> String action = request.getParameter("action");
    >> String aURL = locateTheAppropriateURL(action);
    >> RequestDispatcher requestDispatcher =
    >> request.getRequestDispatcher(aURL);
    >> requestDispatcher.forward(request, response);
    >>
    >> I don't see any mechanism for automatically passing along any other
    >> form/request variables to the new page. Is there a good way of doing this?
    >> Thanks!


    wrote:
    > there are a couple options. my suggestion for you?
    > place everything in "session".
    >
    > HttpSession session = request.getSession();
    > session.setAttribute(request.getParam("param1");
    > session.setAttribute(request.getParam("param2");
    > session.setAttribute(request.getParam("param3");
    > session.setAttribute(request.getParam("param4");


    By definition, request "variables", by which I assume you mean "parameters",
    will come with the "request" parameter of the forward() method. So will any
    attributes set via request.setAttribute(). No further action or "mechanism"
    necessary.

    In the target resource you simply dereference with request.getParameter(),
    request.getAttribute() or the equivalent JSP constructs.

    You should *not* place variables into the session unless their usage requires
    session lifetime. It is silly to put them in the session just to pass them
    redundantly with the request.

    - Lew
    Lew, Dec 1, 2006
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. =?Utf-8?B?amNsYXJrMDczMQ==?=

    Redirect to self while maintaining view state

    =?Utf-8?B?amNsYXJrMDczMQ==?=, Apr 7, 2005, in forum: ASP .Net
    Replies:
    3
    Views:
    4,237
    Teemu Keiski
    Apr 9, 2005
  2. Brett Cramer
    Replies:
    2
    Views:
    3,882
    Brett Cramer
    Oct 16, 2003
  3. circuit_breaker
    Replies:
    2
    Views:
    1,978
    Jack Jia
    Apr 4, 2004
  4. Chris
    Replies:
    11
    Views:
    578
    Jonathan N. Little
    Nov 22, 2007
  5. Sal
    Replies:
    1
    Views:
    372
Loading...

Share This Page