如何使用 HTML 将参数传递给 Java
How to pass parameter to Java using HTML
我不熟悉 Java 和一般的编程。作为背景,我正在研究我的简单调查系统。我拥有插入 MySQL 和从 MySQL 中拉出并在我的 JSP 页面上显示调查所需的所有 classes/methods。
这是我的Index.jsp
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Survey System</title>
</head>
<body>
<h1>Survey List</h1>
<%
ResultSet resultset = new controller.SelectSurvey().SelectSurvey();
if (request.getParameter("btnControlSurvey") != null){
response.sendRedirect("controlsurvey.jsp");
}
if (request.getParameter("btnTakeSurvey") != null){
response.sendRedirect("takesurvey.jsp");
}
%>
<form name = 'surveylist' action="index.jsp" method="POST">
<table border="0">
<thead>
<tr>
<th></th>
<th></th>
</tr>
</thead>
<tbody>
<tr>
<td>
<select id="listsurvey">
<% while (resultset.next()) {
%>
<option><%= resultset.getString("survey_ID")%> - <%= resultset.getString("title")%></option>
<% } %>
</select>
</td>
<td>
<input type="submit" value="TAKE SURVEY" name = "btnTakeSurvey" />
</td>
</tr>
</tbody>
<td>
<input type="submit" value="CONTROL SURVEY" name = "btnControlSurvey" />
</td>
</table>
</form>
</body>
单击“提交”按钮后,如何将带有 ID listsurvey 的下拉列表中选定的 surveyID 值传递到 Takesurvey.jsp(见下文)中的硬编码 surveyID1 变量?
<%
int surveyID1 = 1;
ResultSet rsSurvey = new controller.SelectSurvey().SelectSurveyByID(surveyID1);
ResultSet rsSurveyQuestion = new controller.SelectSurvey().SelectQuestionByID(surveyID1);
%>
我发现至少有以下三种方法可以做到这一点。什么是最简单的方法,请给我举个例子好吗?
正在将值放入会话对象。
将值放入应用程序对象
将值放在重定向的末尾 URL.
非常感谢您的意见。
谢谢你,祝你有美好的一天。
不确定您在第二个代码段中做什么,但您可以使用
获取表单参数
String surveyId = request.getParameter(listsurvey);
看起来您正在寻找另一边的 int,但它以 String 的形式出现,所以只需使用Integer.parseInt() 从请求中提取后。
注意:请求变量已经在您的 JSP 中隐式可用。您不必申报。
您需要使用 GET 或 POST 在 HTTP 页面请求中传递调查 ID。然后在 servlet / JSP 你可以使用 request.getParameter("surveyID")
我通过使用 request.getParameters 解决了这个问题,并为每个按钮使用了不同的形式,并且效果如愿。
再次感谢大家的投入。
我不熟悉 Java 和一般的编程。作为背景,我正在研究我的简单调查系统。我拥有插入 MySQL 和从 MySQL 中拉出并在我的 JSP 页面上显示调查所需的所有 classes/methods。
这是我的Index.jsp
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Survey System</title>
</head>
<body>
<h1>Survey List</h1>
<%
ResultSet resultset = new controller.SelectSurvey().SelectSurvey();
if (request.getParameter("btnControlSurvey") != null){
response.sendRedirect("controlsurvey.jsp");
}
if (request.getParameter("btnTakeSurvey") != null){
response.sendRedirect("takesurvey.jsp");
}
%>
<form name = 'surveylist' action="index.jsp" method="POST">
<table border="0">
<thead>
<tr>
<th></th>
<th></th>
</tr>
</thead>
<tbody>
<tr>
<td>
<select id="listsurvey">
<% while (resultset.next()) {
%>
<option><%= resultset.getString("survey_ID")%> - <%= resultset.getString("title")%></option>
<% } %>
</select>
</td>
<td>
<input type="submit" value="TAKE SURVEY" name = "btnTakeSurvey" />
</td>
</tr>
</tbody>
<td>
<input type="submit" value="CONTROL SURVEY" name = "btnControlSurvey" />
</td>
</table>
</form>
</body>
单击“提交”按钮后,如何将带有 ID listsurvey 的下拉列表中选定的 surveyID 值传递到 Takesurvey.jsp(见下文)中的硬编码 surveyID1 变量?
<%
int surveyID1 = 1;
ResultSet rsSurvey = new controller.SelectSurvey().SelectSurveyByID(surveyID1);
ResultSet rsSurveyQuestion = new controller.SelectSurvey().SelectQuestionByID(surveyID1);
%>
我发现至少有以下三种方法可以做到这一点。什么是最简单的方法,请给我举个例子好吗?
正在将值放入会话对象。 将值放入应用程序对象 将值放在重定向的末尾 URL.
非常感谢您的意见。
谢谢你,祝你有美好的一天。
不确定您在第二个代码段中做什么,但您可以使用
获取表单参数String surveyId = request.getParameter(listsurvey);
看起来您正在寻找另一边的 int,但它以 String 的形式出现,所以只需使用Integer.parseInt() 从请求中提取后。
注意:请求变量已经在您的 JSP 中隐式可用。您不必申报。
您需要使用 GET 或 POST 在 HTTP 页面请求中传递调查 ID。然后在 servlet / JSP 你可以使用 request.getParameter("surveyID")
我通过使用 request.getParameters 解决了这个问题,并为每个按钮使用了不同的形式,并且效果如愿。
再次感谢大家的投入。