0

I have a JSP that uses an tag with a hidden value to pass to the doGet in a servlet and so far this works well. However, can I change the hidden value dynamically? Currently it is

<input type="hidden" name="command" value="COMMAND1" />

but I want the COMMAND1 to be one of two commands. I tried changing the line above to this;

<input type="hidden" name="command" value="<%=request.getParameter
  ("USE_COMMAND") %>" >

Then the doGet in the servlet calls the procedure below and I tried setting the command as shown here but it doesn't work. Can this be done?

private void processCommand(HttpServletRequest request, HttpServletResponse 
  response) throws Exception {

    ....

    // dynamically set hidden command for JSP form GET
    String useCommand = "COMMAND2";
    request.setAttribute("USE_COMMAND", useCommand);

    ....

   // send to JSP page (view)
   RequestDispatcher dispatcher = request.getRequestDispatcher
     ("/theJSPPage.jsp");
   dispatcher.forward(request, response);

 }
AJF
  • 1,801
  • 4
  • 27
  • 54

1 Answers1

2

You need to use the getAttribute() method in your JSP:

<input type="hidden" name="command" value="<%=request.getAttribute("USE_COMMAND") %>" >

This post explains the difference: Difference between getAttribute() and getParameter()

Community
  • 1
  • 1
Michael Markidis
  • 4,163
  • 1
  • 14
  • 21