java 通过servlet的url模式将参数从jsp传递给servlet
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/29248190/
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 parameter from jsp to servlet through url pattern of servlet
提问by Mahender Reddy Yasa
I got a requirement to send one parameter through url pattern of servlet (like we send through <a href="example.jsp?id=1">send</a>
) in the same way I need it through url pattern.
我需要通过 servlet 的 url 模式发送一个参数(就像我们通过 url 发送一样<a href="example.jsp?id=1">send</a>
),就像我通过 url 模式需要它一样。
I do this with other possibilities like
我用其他可能性来做这个
- I can send that parameter as hidden type
- i can put in a request and session objects
- 我可以将该参数作为隐藏类型发送
- 我可以放入请求和会话对象
these methods are working fine no problem
这些方法工作正常没问题
but through url it's not taking? I want to know whether it is possible or not?
但通过网址它不采取?我想知道这是否可能?
the code I have tried
我试过的代码
jsp page
jsp页面
<a href="download?filename=<%=filename%>" target="_blank"> <font color="black"><%=filename%> </font></a>
servlet code
小服务程序代码
String filename=request.getParameter("filename");
and i need one answer can we pass parameter through url pattern if yes how? i.e like same as through <a href="example?id=1">send</a>
or differently?
我需要一个答案,如果是的话,我们可以通过 url 模式传递参数吗?即像通过<a href="example?id=1">send</a>
或不同?
回答by saikumarm
I am just trying to give you an example
我只是想给你举个例子
.jsp FILE
.jsp 文件
<% String filename ="nameofFile.txt"; %>
<a href="download?filename=<%= filename %>" ></a>
SERVLET CODE
服务代码
String filename = (String)request.getParameter("filename");
BufferedReader fir= new BufferedReader(new FileReader(new FileInputStream(filename)));
PrintWriter out = response.getWriter();
while(fir.ready())
out.println(fir.readLine())
I think you are getting blank page because you are not sending any response back to the client, here out.println
will actually send response back to the client
我认为您得到空白页是因为您没有将任何响应发送回客户端,这里out.println
实际上会将响应发送回客户端
回答by Braj
回答by Mahender Reddy Yasa
Yes you can send as like trough jsp.
是的,您可以像通过 jsp 一样发送。
i just tested now it's working fine the blank page is coming because of other statements written in your servlet code so make sure servlet code is correct.
我刚刚测试过它现在工作正常,由于在您的 servlet 代码中写入了其他语句,因此出现空白页,因此请确保 servlet 代码正确。
回答by Junaid
It is possible but it looks like your browser clearsparameters after ?...
in action="..."
attribute. In that case try passing it via <input type="hidden" .../>
like
这是可能的,但看起来您的浏览器在属性之后清除了参数。在这种情况下,请尝试通过like传递它?...
action="..."
<input type="hidden" .../>
<form action="sendFileToServlet" method="get">
<input type="hidden" name="filename" value="<%=filename%>"/>
<input type="submit" value="Send" />
</form>
This way form should add them to URL as ?filename=
value of <%=filename%>
.
这种方式表单应该将它们作为?filename=
值添加到 URL中<%=filename%>
。