简体   繁体   English

页面加载时,JSP将隐藏的输入值传递给servlet

[英]JSP pass hidden input value to servlet when the page load

I should be using getRemoteUser functionality to get the logged in user. 我应该使用getRemoteUser功能来获取登录用户。 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. 在创建身份验证部分之前,我尝试在jsp页面中对用户进行硬编码,并将其传递到我的servlet中。 But when I try to print out the value its null: 但是,当我尝试打印出该值时,它为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. 我还尝试了Stackoverflow帖子后提到的解决方案,但该方法也不起作用。 passing value from jsp to servlet 将值从jsp传递到servlet

As you are trying to use hidden-form field I assume that you are trying to do some sort of state management . 当您尝试使用hidden-form field我假设您正在尝试进行某种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 在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 而是尝试url-redirectingHttpSession

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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