java 从 JSP 或 HTML 向 servlet 发送多个参数

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6004346/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 13:56:53  来源:igfitidea点击:

Sending multiple parameters to servlets from either JSP or HTML

javaservletsparameters

提问by Jeevan Dongre

I need to send a particular parameters to Servlet from JSP page. E.g: if I click on the Facebook icon on the web page, then I should send "facebook" as parameter to my Servlet and my Servlet will respond according to the parameter received from JSP file or HTML file.

我需要从 JSP 页面向 Servlet 发送特定参数。例如:如果我点击网页上的 Facebook 图标,那么我应该将“facebook”作为参数发送到我的 Servlet,我的 Servlet 将根据从 JSP 文件或 HTML 文件收到的参数进行响应。

回答by pickypg

This is a very open ended question, but the easiest way is to specify parameters in a query string.

这是一个非常开放的问题,但最简单的方法是在查询字符串中指定参数。

If you have the following servlet:

如果您有以下 servlet:

/mysite/messageServlet

/mysite/messageServlet

Then you can send it parameters using the query string like so:

然后您可以使用查询字符串向其发送参数,如下所示:

/mysite/messageServlet?param1=value1&param2=value2

/mysite/messageServlet?param1=value1¶m2=value2

Within the servlet, you could check your requestfor parameters using getParameter(name)if you know the name(s), or getParameterNames(). It's a little more involved, specifically with consideration to URL Encoding and statically placing these links, but this will get you started.

在这个servlet,你可以检查你request使用的参数getParameter(name),如果你知道姓名(或名称),或getParameterNames()。它涉及更多,特别是考虑到 URL 编码和静态放置这些链接,但这会让您开始。

String message = request.getParameter("message");
if ("facebook".equals(message))
{
    // do something
}

Storing links with multiple parameters in the querystring requires you to encode the URL for HTML because "&" is a reserved HTML entity.

在查询字符串中存储具有多个参数的链接需要您对 HTML 的 URL 进行编码,因为“ &”是一个保留的 HTML 实体。

<a href="/servlets/messageServlet?param1=value&amp;param2=value2">Send Messages</a>

Note that the &is &amp;.

请注意,&&amp;

回答by BalusC

Just wrap the icon in a link with a query string like so

只需将图标包装在一个带有查询字符串的链接中,就像这样

<a href="servleturl?name=facebook"><img src="facebook.png" /></a>

In the doGet()method of the servlet just get and handle it as follows

doGet()servlet的方法中直接get和处理如下

String name = request.getParameter("name");

if ("facebook".equals(name)) {
    // Do your specific thing here.
}

See also:

也可以看看:

回答by Kal

One way is to have hidden form variables in your jsp page that get populated on click.

一种方法是在您的 jsp 页面中隐藏表单变量,这些变量在点击时被填充。

<form action="post" ....>
<input type="hidden" id="hiddenVar" value="">
<a hfref="#" onclick="doSomething();">Facebook</a>
</form>
<script>
      function doSomething() {
              var hiddenVar= document.getElementById('hiddenVar');
              hiddenVar.value = "facebook";
              form.submit();

      }
 </script>

This gives you flexibility to control what gets passed to your servlet dynamically without having to embed urls in your href

这使您可以灵活地控制动态传递给 servlet 的内容,而无需在您的 href 中嵌入 url