2009-12-10 60 views
0

正如你在这里看到的,我有一个带有三个提交按钮的文本框,每个按钮都重定向到不同的jsp页面,但是在那些jsp页面中,当我做request.getParameter(“bid”)时,我得到的全部为空...我怎样才能解决这个最简单的方法?处理表单和多个按钮的文本框

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" 
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 
<html xmlns="http://www.w3.org/1999/xhtml"> 
<head> 
<title>View/Modify Student Information</title> 
<meta http-equiv="Content-type" content="text/html;charset=UTF-8" /> 
<script type="text/javascript" src="scripts/search.js"></script> 
</head> 
<body> 
<br/> 
<div align="center"> 
<h2>View/Modify Student Information</h2> 
<label> Student Banner ID: </label> 
<input type="text" name="bid" /> 
<br/> 
<br/> 
<form method = "post" action="rent.jsp"> 
<input type="text" name="bid1" /> 
<input type="submit" value="Total Rent" onClick="goto_rent()"/> 
</form> 
<form method = "post" action="adviser.jsp"> 
<input type="text" name="bid2" /> 
<input type="submit" value="Adviser Information" onClick="goto_adviser()"/> 
</form> 
<form method = "post" action="delete_std.jsp"> 
<input type="text" name="bid3" /> 
<input type="submit" value="Delete Student" onClick="goto_del()"/> 
</form> 

</div> 
</body> 
</html> 

回答

0

当我做的request.getParameter( “竞价”),我得到的是空...

因为它不放在同一个表单中。只需要在一个的形式。要区分按下的按钮,只需给每个相同的名称。最好的方法是创建一个servlet其采取行动。因此基于按下按钮:

<form action="studentServlet" method="post"> 
    <input type="text" name="bid" /> 
    ... 
    <input type="submit" name="action" value="Total Rent" /> 
    <input type="submit" name="action" value="Adviser Information" /> 
    <input type="submit" name="action" value="Delete Student" /> 
</form> 

com.example.StudentServlet类应该是这样的:

package com.example; 

import java.util.HashMap; 
import java.util.Map; 

import javax.servlet.http.HttpServlet; 
import javax.servlet.http.HttpServletRequest; 
import javax.servlet.http.HttpServletResponse; 

public class StudentServlet extends HttpServlet { 

    private static final Map<String, String> pages = new HashMap<String, String>() {{ 
     put("Total Rent", "/WEB-INF/rent.jsp"); 
     put("Adviser Information", "/WEB-INF/adviser.jsp"); 
     put("Delete Student", "/WEB-INF/delete_std.jsp"); 
    }}; 

    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { 
     String action = request.getParameter("action"); 
     String page = pages.get(action); 
     request.getRequestDispatcher(page).forward(request, response); 
    } 

} 

地图这个servlet在web.xml/studentServleturl-pattern(或所以,至少form action必须指向它)。 JSP页面位于/WEB-INF中,以防止在没有中介Servlet的情况下直接访问。

这样你可以在每个JSP页面由

<p>Your bid was ${param.bid}</p> 

希望这有助于访问bid参数。

+0

除此之外还有其他更简单的方法吗?因为我不知道什么 com.example.StudentServlet是... – 2009-12-10 16:54:13

+0

我想在我的jsp代码中使用request.getParamater(“bid”)而不是

您的出价是$ {param.bid}

2009-12-10 16:56:46

+0

1)它是一个扩展了'javax.servlet.http.HttpServlet'的Java类。我编辑了代码示例以演示更多。 2)它完全一样,但是使用表达式语言而不是十多年前不鼓励的老式脚本。我强烈建议通过一个体面的JSP/Servlet教程,以便你能找到某个地方。 – BalusC 2009-12-10 17:10:55