简体   繁体   English

如何通过超链接从JSP将对象发送到Servlet?

[英]How can I send an object to a Servlet from a JSP via hyperlink?

How can I send a an object got it from a bean in a JSP page to a Servlet after clicking an hyperlink?? 单击超链接后,如何将一个对象从JSP页面中的Bean发送到Servlet?

something like... 就像是...

...
<td align="center"><% if(j.getClubActual().isIsResource()){ request.setAttribute("equipo", j.getClubActual());%>
<a href="teamServlet" type="submit" target="_blank"><%= j.getClubActual().getNombre()%></a><%}%>
</td>
...

But when I try to recover it in the teamServlet, the request object is empty. 但是,当我尝试在teamServlet中恢复它时,请求对象为空。

Thanks in advance. 提前致谢。

What object you are trying to send? 您要发送什么对象?

If you want to "send" objects, I think your safest bet is to use HTTP session to do that. 如果您想“发送”对象,我认为您最安全的选择就是使用HTTP会话来实现。 You can't send "objects" (or beans) with a hyperlink to a servlet. 您不能将带有超链接的“对象”(或bean)发送到servlet。 You can only send parameters values usually in form of strings. 您通常只能以字符串形式发送参数值。

The lifetime of a HTTP request ends when its associated HTTP response is finished sending the data (read: the HTML page generated by JSP). 当HTTP请求的关联HTTP响应完成发送数据时,其生存期结束(请参阅:JSP生成的HTML页面)。 Clicking a link will create a brand new HTTP request which doesn't contain the attributes of any previous request at all. 单击链接将创建一个全新的HTTP请求,该请求完全不包含任何先前请求的属性。

You need to send the unique identifier of the Java object in question as a request parameter. 您需要发送所涉及的Java对象的唯一标识符作为请求参数。 Ultimately, the HTML must end up to basically look like this: 最终,HTML最终必须基本上看起来像这样:

<a href="teamServlet?clubId=123">link</a>

In the servlet, you can get the request parameter as follows: 在Servlet中,您可以按以下方式获取request参数:

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

You can use this value to re-obtain the Java object associated with the given ID from some data store. 您可以使用此值从某些数据存储中重新获取与给定ID关联的Java对象。

Club club = clubService.find(Long.valueOf(clubId));
// ...

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

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