简体   繁体   English

servlet查询字符串

[英]servlet Query String

I am using query string for sending parameter to other page in form action tag but when I am fetching the value on the next page, it is showing null and null is stored in the database. 我正在使用查询字符串将参数发送到表单操作标签中的其他页面,但是当我在下一页获取值时,它显示null并且null存储在数据库中。

My code is as follows: 我的代码如下:

out.println("<html>");
out.println("<form action=SubmitTimesheet?manager="+manager+">");

and on the other page I am trying to fetch the value of parameter in following manner: 在另一页上,我尝试以以下方式获取parameter的值:

String manager=request.getParameter("manager");
out.println(manager);

But it is printing null and further if I am trying to store the fetched value in database it is storing null. 但是它正在打印null并且如果我尝试将获取的值存储在数据库中,它将存储null。

Please help. 请帮忙。

Don't add the manager query parameter if the manager value is null. 如果manager值为null,则不要添加manager查询参数。

Also remember to encode query parameters. 还请记住对查询参数进行编码。

And finally, the value of the action attribute should be quoted. 最后,应引用action属性的值。

out.print("<form action=\"SubmitTimesheet");
if (manager != null)
    out.print("?manager=" + URLEncoder.encode(manager, "UTF-8"));
out.println("\">");

Instead of the "UTF-8" string literal, you can also use StandardCharsets.UTF_8 . 除了使用"UTF-8"字符串文字,还可以使用StandardCharsets.UTF_8


UPDATE 更新

In reality, you shouldn't add query parameters to the action of a <form> . 实际上,您不应将查询参数添加到<form>的操作中。 The form will post all the values of <input> and other form related elements, so what you should do, is add the manager value as a hidden input field: 该表单将发布<input>所有值以及其他与表单相关的元素,因此您应该做的是将manager值添加为隐藏的输入字段:

out.println("<html>");
out.println("<form action=\"SubmitTimesheet\">");
if (manager != null)
    out.print("<input name=\"manager\" type=\"hidden\" value=\"" +
              StringEscapeUtils.escapeHtml4(manager) + "\">");

The above uses the library suggested by answer to question Recommended method for escaping HTML in Java . 上面使用了由问题答案推荐的库推荐的Java转义HTML方法

i have use session object and passed the value of manager in session variable instead of query string. 我已经使用了会话对象,并在会话变量而不是查询字符串中传递了manager的值。 i have used the following code: 我使用了以下代码:

ses.putValue("manager", manager); ses.putValue(“ manager”,manager);

on the first servlet page and then on the second servlet page i fetched the value like this : 在第一个servlet页面上,然后在第二个servlet页面上,我这样获取值:

String manager=ses.getValue("manager").toString();

out.println(manager);

now it is printing the correct value instead of null 现在它正在打印正确的值,而不是null

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

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