Html 通过 JSP 页面中的超链接传递值
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11714740/
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
Passing a value through hyperlink in JSP page
提问by user1561790
I have a very simple JSP page first.jsp that gives reference to so many other JSP pages through hyperlink.
我有一个非常简单的 JSP 页面 first.jsp,它通过超链接提供对许多其他 JSP 页面的引用。
I am trying to pass a value through the hyperlink in first.jsp and retrieve it in second.jsp using request.getparameter().
我试图通过 first.jsp 中的超链接传递一个值,并使用 request.getparameter() 在 second.jsp 中检索它。
I used:
我用了:
<a href="second.jsp?value=1">Second JSP</a>
and in the second.jsp page I used:
在我使用的 second.jsp 页面中:
alert( "value = " + <%= request.getParameter("value") %>);
but when I try to run the code and click on Second JSPI get a 404 page not found error for:second.jsp?value=1
但是当我尝试运行代码并单击 Second JSP 时,我收到一个 404 page not found 错误:second.jsp?value=1
回答by Edmon
It looks like your URL to second JSP might not be good. "second.jsp" is not visible to your web app server the way you specified.
看起来您指向第二个 JSP 的 URL 可能不太好。“second.jsp”对您的 Web 应用程序服务器不可见,以您指定的方式。
To fix this:
要解决此问题:
- If your jsps are resolved through a web app try:
- 如果您的 jsps 是通过网络应用程序解析的,请尝试:
<a href="<webapp name>/second.jsp?value=1">Second JSP</a>
<a href="<webapp name>/second.jsp?value=1">Second JSP</a>
- if you have
jsp
or similar directory that is visible outside of web app context do something like:
- 如果您有
jsp
或类似的目录在 Web 应用程序上下文之外可见,请执行以下操作:
<a href="jsp/second.jsp?value=1">Second JSP</a>
<a href="jsp/second.jsp?value=1">Second JSP</a>
回答by Uma Sankar
Try getQueryString()
method to access the value that you passed through the URL.
TrygetQueryString()
方法访问您通过 URL 传递的值。
Then use string methods to extract the value form the URL.
然后使用字符串方法从 URL 中提取值。
For example, you pass the parameter as you mentioned
例如,您按照您提到的方式传递参数
Use getQueryString()
method in second.jsp
, it will return the string value = 1
as the result.
使用getQueryString()
方法 in second.jsp
,它将返回字符串value = 1
作为结果。
回答by albert Jegani
Html:
<a href="birth1.jsp?key=14APR010315381">
<center>
<font size="2" color="black">MIKE LOBO</font></center>
</a>
Jsp:
request.getParameter("key");
I m using in my project and its working fine
我在我的项目中使用它并且它工作正常