简体   繁体   中英

JSP pass hidden input value to servlet when the page load

I should be using getRemoteUser functionality to get the logged in user. Until the authentication part get created I am trying to hard code the user in the jsp page and pass that in the my servlet. But when I try to print out the value its null:

<input type="hidden" name="userId" id="userId" value="123456789" />

Here is how I tried to get the user:

    String fakeUser = request.getParameter("userId");
    PrintWriter out = response.getWriter();
    out.println(fakeUser);
    System.out.println(fakeUser)

I also tried the solution mentioned following Stackoverflow post but that didn't work either. passing value from jsp to servlet

As you are trying to use hidden-form field I assume that you are trying to do some sort of state management .

try something like this

<form action="urlOfYourServlet" method="post">
    Enter your name : <input type ="text" name = "name"> 
    <input type="hidden" name="hidden" value="Welcome">
    <input type="submit" value="submit">
</form> 

In servlet

 String getHiddenValue=request.getParameter("hidden");
 String name=request.getParameter("name");
 System.out.println(name+" Hidden field Value is :"+getHiddenValue);

Disadvantage :

  1. Only textual information can be persisted between request.
  2. This method works only when the request is submitted through input form

Instead try url-redirecting or HttpSession

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM