2011-11-23 132 views
0
<c:forEach var="it" items="${sessionScope.projDetails}"> 
    <tr> 
     <td>${it.pname}</td> 
     <td>${it.pID}</td> 
     <td>${it.fdate}</td> 
     <td>${it.tdate}</td> 
     <td> <a href="${it.address}" target="_blank">Related Documents</a></td> 
     <td>${it.pdesc}</td> 
     <form name="myForm" action="showProj"> 
      <td><input id="button" type="submit" name="${it.pID}" value="View Team"> 
      </td> 
     </form> 
</c:forEach> 

参考上面的代码,我从某个servlet获取会话对象projDetails,并在JSP中显示其内容。由于数组列表projDetails具有多个记录,所以字段pID也假定不同的值,并且显示将是具有许多行的表格。
现在我想调用一个servlet showProj当用户点击“查看团队”(将在每行)基于该行的“pID”。 有人能让我知道如何将用户点击JSP的特定pID传递给servlet吗?如何将会话值作为请求参数从JSP传递到servlet?

回答

3

相反的<input>每个不同的PID,你可以使用链接通过将PID作为查询字符串给servlet,是这样的:

<a href="/showProj?pID=${it.pID}">View Team</a> 

showProj servlet代码,你会访问通过doGet方法里面request对象查询字符串,是这样的:

public void doGet(HttpServletRequest request, HttpServletResponse response) 
    throws ServletException, IOException 
{ 
    String pID = request.getParameter("pID"); 
    //more code... 
} 

下面是Java servlet为一些参考:

HttpServletRequest object
Servlet tutorials

+0

我尝试了上面的代码,但它说:“所请求的资源不可用”,甚至船尾呃提供servlet的完整URL,我怀疑我们是否通过JSP中的“href”访问servlet? – Vinod

+0

It works now, i just put a "." in Vinod

-1

定义的onclick函数上的按钮并传递参数

<form name="myForm" action="showProj"> 
     <input type='hidden' id='pId' name='pId'> 
     <td><input id="button" type="submit" name="${it.pID}" value="View Team" onclick="populatePid(this.name)"> 
     </td> 
..... 

定义javascript函数:

function populatePid(name) { 
    document.getElementById('pId') = name; 
} 

和在servlet:

public void doGet(HttpServletRequest request, HttpServletResponse response) 
throws ServletException, IOException 
{ 
    String pID = request.getParameter("pId"); 
    ....... 
} 
+0

I tried with the above code but the "pID" in servlet contains "null" – Vinod

+0

Just want to point out, though not sure if that is a problem, that do take care of the variable names. The 'name' attribute of the 'hidden' should be the one you should be using in the servlet to get the value which in the above case is 'pId'. FInally, you will see request parameter being populated in the url when you submit the form e.g. xxx.com?pId=abc. If that happens it is certain that servlet will be getting value abc under the name pId. If that doesn't happens then there is a problem with HTML/JS code. –

+0

Am getting this when i submit the form "/showProj?pId=&445=View+Team", I have put the "String pID = request.getParameter("pId");" inside protected void processRequest(HttpServletRequest req, HttpServletResponse response) throws ServletException, IOException { String pID = req.getParameter("pId"); ........} I got pID as null.. and after your comments i have tried putting the "String pID = request.getParameter("pId");" inside protected void doGet(HttpServletRequest req, HttpServletResponse response) throws ServletException, IOException { String pID=req.getParameter("pId") ......} – Vinod

1

pID沿着隐藏的输入字段传递。

<td> 
    <form action="showProj"> 
     <input type="hidden" name="pID" value="${it.pID}"> 
     <input type="submit" value="View Team"> 
    </form> 
</td> 

(请注意,我重新安排了<form><td>,使其有效的HTML,我也从按钮移除id,因为它是在HTML无效的具有相同id多个元素)

这样你可以在servlet得到它,如下所示:

String pID = request.getParameter("pID"); 
// ... 
相关问题