Java 如何使用 request.setAttribute() 通过单击链接来设置和传递值

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/37958716/
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-08-11 19:49:58  来源:igfitidea点击:

How to use request.setAttribute() to set and pass value by clicking on a link

javajspservlets

提问by Kashiii

I want to pass value of a rowNo from one jsp file to another jsp file(to display image on that row) by using request.setAttribute() and request.getAttribute() whenever user clicks on a link.

我想在用户单击链接时使用 request.setAttribute() 和 request.getAttribute() 将 rowNo 的值从一个 jsp 文件传递​​到另一个 jsp 文件(以在该行上显示图像)。

But when I try to use request.getAttribute() on second jsp page it gives following exception: org.apache.jasper.JasperException: java.lang.NumberFormatException: null

但是当我尝试在第二个 jsp 页面上使用 request.getAttribute() 时,它给出了以下异常:org.apache.jasper.JasperException: java.lang.NumberFormatException: null

On Index.jsp page I have :

在 Index.jsp 页面上,我有:

<%!
 int temp = 1;

 %>

<a href="single.jsp" onclick="<%= request.setAttribute("val", temp) %>" > 
<i class="glyphicon glyphicon-menu-right icon"></i> </a>

And on single.jsp page :

在 single.jsp 页面上:

<%

dbConnect con = new dbConnect(); 
ResultSet r = con.getConnection().executeQuery("select * from tblpic "); 

String tp = (String) (request.getAttribute("val"));

int i = Integer.parseInt(tp);

r.absolute(i);

%>

<div class="thumb-image"> <img src="<%= r.getString(2) %>" data-imagezoom="true" class="img-responsive"> </div>

r.absolute(i) is used to move to that specific row in table.

r.absolute(i) 用于移动到表中的特定行。

I have added this file on both jsp pages(I don't know it is necessary or not) <%@page import = "javax.servlet.http.HttpServletRequest"%>

我在两个jsp页面都添加了这个文件(不知道有没有必要)<%@page import = "javax.servlet.http.HttpServletRequest"%>

I am working on net beans and struts framework.

我正在研究 net bean 和 struts 框架。

采纳答案by Piyush Aghera

You have mixed server side code and client side code together. "request" is a java object and part of servelet request handling. it works at server side only while rendering jsp in servlet container.

您将服务器端代码和客户端代码混合在一起。“request”是一个java对象,是servlet请求处理的一部分。它仅在服务器端在 servlet 容器中呈现 jsp 时工作。

"onclick" is a java script click trigger run at browser side.

“onclick”是一个在浏览器端运行的java脚本点击触发器。

Once page get displayed "request" is not logger valid. and on clicking of submitting new request..new object of request will created.

一旦页面显示“请求”,记录器无效。并点击提交新请求..新的请求对象将被创建。

For you, can easily pass parameter argument to jsp as follow.

对您来说,可以轻松地将参数参数传递给 jsp,如下所示。

on first jsp:

在第一个jsp上:

<a href="single.jsp?val=temp" >

<a href="single.jsp?val=temp" >

on second jsp:

在第二个jsp上:

<%=request.getParameter("val")%>

<%=request.getParameter("val")%>

回答by PVR

I would prefer session.setAttribute()instead of request.setAttribute()to pass value from one jsp to another.

我宁愿session.setAttribute(),而不是 request.setAttribute()从一个JSP价值传递到另一个。

So your code will be:

所以你的代码将是:

<a href="single.jsp" onclick="<%= session.setAttribute("val", temp) %>" > 

And

String tp = (String) (session.getAttribute("val"));

回答by BeginnersSake

You are setting values in request scope. Request scope attributes are only accessible in the same request. Whenever the end user clicks on the link(provided by ), a new request is generated hence you loose the attributes that you set in the previous request.

您正在请求范围内设置值。请求范围属性只能在同一个请求中访问。每当最终用户单击链接(由 提供)时,都会生成一个新请求,因此您会丢失在前一个请求中设置的属性。

To Solve the problem you could do

解决你可以做的问题

1) URL forwarding as explained by Piyush Aghera

1) 由 Piyush Aghera 解释的 URL 转发

2) You can store the value in the session as explained by PVR

2)您可以按照 PVR 的说明将值存储在会话中

3) If you prefer to store the values in request attribute only, then I would suggest you to use RequestDispatcher to forward your request. This would work as your request on the next page would remain same.

3)如果您更喜欢仅将值存储在请求属性中,那么我建议您使用 RequestDispatcher 转发您的请求。这将起作用,因为您在下一页上的请求将保持不变。