简体   繁体   English

如何将两个值传递给Java servlet?

[英]How can I pass two values to a Java servlet?

I am developing a JSF application which has a servlet to display binary images. 我正在开发一个具有servlet来显示二进制图像的JSF应用程序。 I have it working with one parameter but wanted to pass two parameters. 我有一个参数,但想传递两个参数。 It only seems to get the first parameter. 它似乎只获得第一个参数。 My mapping looks like this. 我的映射如下所示。

<servlet>
    <servlet-name>imageServlet</servlet-name>
    <servlet-class>com.myapp.system.ImageServlet</servlet-class>
</servlet>

<servlet-mapping>
    <servlet-name>imageServlet</servlet-name>
    <url-pattern>/image/*</url-pattern>
</servlet-mapping>

When I call the servlet it looks like this 当我调用servlet时,它看起来像这样

<p:graphicImage value="image?app=avatar?id=#{bean.imgId}" />

In my servlet I get the parameters like so 在我的servlet中,我得到像这样的参数

 String id = request.getParameter("id");
 String app = request.getParameter("app");

If I output the app string it displays this 如果我输出应用程序字符串,它将显示此内容

 avatar?id=166

How can I allow the servlet url-patter to take in two values and be able to parse them using the getParameter method. 如何允许servlet url-patter接受两个值,并能够使用getParameter方法解析它们。 Thanks. 谢谢。

The JSP tag should be written as: JSP标记应写为:

<p:graphicImage value="image?app=avatar&amp;id=#{bean.imgId}" />

Parameters in the query part of a URL should be separated by '&'s not '?'s. URL的查询部分中的参数应以'&'而不是'?'分隔。

(You can ignore this, but them you won't be able to use ServletRequest.getParameter(String) to retrieve the parameter values. Instead you'll have to use ServletRequest.getQueryString() and parse the individual parameters yourself.) (您可以忽略这一点,但是您将无法使用ServletRequest.getParameter(String)来检索参数值。相反,您必须使用ServletRequest.getQueryString()并自己解析各个参数。)

用于编码URL参数的格式是键-值对与符号(分离& ):

<p:graphicImage value="image?app=avatar&id=#{bean.imgId}" />

除第一个参数外,在所有参数之前使用与号:

<p:graphicImage value="image?app=avatar&id=#{bean.imgId}" />
<p:graphicImage value="image?app=avatar&id=#{bean.imgId}" />

&而不是第二个?

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

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