Java 如何在另一个 jsp 页面上获取选定的单选按钮值?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/21746094/
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
How can I get the selected radio button value on another jsp page?
提问by learner
This is the 1st page. I'm facing problem with the radio button value being NULL on the submission handling page.
这是第一页。我在提交处理页面上遇到了单选按钮值为 NULL 的问题。
<html>
<head>
<title>LoginForm</title>
</head>
<body >
<font size="20"><marquee behavior="alternate">Spice Digital</marquee></font>
<h1>Login Page</h1>
<h2>Signup Details</h2>
<form action="LoginCheck.jsp" method="post">
<br/>Username:<input type="text" name="username">
<br/>Password:<input type="password" name="password">
<br/>select your gender:
<select name=dropdown>
<option name=one value=one> Male </option>
<option name=two value=two> Female </option>
</select>
<br/>select your department:
<input type="radio" name=myradio value="1"/>MCA
<input type="radio" name=myradio value="2"/>B.Tech
<input type="radio" name=myradio value="3"/>Other
<br/> select your choices:
<input type="checkbox" name=mybox value="1"/>java
<input type="checkbox" name=mybox value="2"/>c++
<input type="checkbox" name=mybox value="3"/>c
<input type="checkbox" name=mybox value="4"/>sql
<br/>enter you comments here:
<textarea name=mytextarea cols=20 rows=5>
</textarea>
<br/><input type="submit" value="Login">
</form>
</body>
</html>
This is the 2nd page. I'm unable to get the selected value here. Am I using request.getParameter()
wrong?.
这是第 2 页。我无法在此处获取所选值。我用request.getParameter()
错了吗?。
<html>
<head>
<title>JSP Page</title>
</head>
<body BACKGROUND="C:\Documents and Settings\temp- 00940\workspace\login\WebContent\background.jpg">
<br/><br/><br/><br/><br/>
<form action="LoginForm.jsp" method="get">
<font size="20"><marquee>Spice Digital </marquee></font>
<h2>
<%
String a=session.getAttribute("username").toString();
out.println("Welcome "+a+"..!!");
%>
<br>
<%
String b=request.getParameter("myradio");
out.println("Your department is "+b);
%>
<br/>
<%
String c=request.getParameter("mybox");
out.println("Your choice is "+c);
%>
</h2>
<br/>
<a href="http://www.Google.com">Click here to google search</a>
<br/>
<br/><br/>
<br/><br/><br/>
<a href="Logout.jsp">LogOut</a>
</form>
</body>
</html>
采纳答案by Harsh
It is working fine with me on eclipse. Modified the code a little for simplicity.
它在 eclipse 上对我来说很好用。为简单起见,稍微修改了代码。
If not routing data to some other page then In in this you should have the name of your JSP file which your data should get displayed.
如果没有将数据路由到其他页面,那么在此您应该有您的数据应该显示的 JSP 文件的名称。
Here is the modified code.
这是修改后的代码。
index.jsp
索引.jsp
<html>
<head>
<title>LoginForm</title>
</head>
<body >
<font size="20"><marquee behavior="alternate">Spice Digital</marquee></font>
<h1>Login Page</h1>
<h2>Signup Details</h2>
<form action="new.jsp" method="post">
<br/>Username:<input type="text" name="username">
<br/>Password:<input type="password" name="password">
<br/>select your gender:
<select name=dropdown>
<option name=one value=one> Male </option>
<option name=two value=two> Female </option>
</select>
<br/>select your department:
<input type="radio" name=myradio value="1"/>MCA
<input type="radio" name=myradio value="2"/>B.Tech
<input type="radio" name=myradio value="3"/>Other
<br/> select your choices:
<input type="checkbox" name=mybox value="1"/>java
<input type="checkbox" name=mybox value="2"/>c++
<input type="checkbox" name=mybox value="3"/>c
<input type="checkbox" name=mybox value="4"/>sql
<br/>enter you comments here:
<textarea name=mytextarea cols=20 rows=5>
</textarea>
<br/><input type="submit" value="Login">
</form>
</body>
</html>
new.jsp
新建.jsp
<html>
<head>
<title>JSP Page</title>
</head>
<body BACKGROUND="C:\Documents and Settings\temp-00940\workspace\login\WebContent\background.jpg">
<br/><br/><br/><br/><br/>
<form action="LoginForm.jsp" method="get">
<font size="20"><marquee>Spice Digital </marquee></font>
<h2>
<%
// String a=session.getAttribute("username").toString();
// out.println("Welcome "+a+"..!!");
%>
<br>
<%
String b=request.getParameter("myradio");
out.println("Your department is "+b);
%>
<br/>
<%
String c=request.getParameter("mybox");
out.println("Your choice is "+c);
%>
</h2>
<br/>
<a href="http://www.Google.com">Click here to google search</a>
<br/>
<br/><br/>
<br/><br/><br/>
<a href="Logout.jsp">LogOut</a>
</form>
</body>
</html>
problem could be with you routing as you are sending your form first to logincheck.jsp then to your desired jsp.
问题可能出在您的路由上,因为您首先将表单发送到 logincheck.jsp,然后再发送到您想要的 jsp。
回答by learner
<html>
<head>
<title>Example</title>
</head>
<body>
<form name="form-name" action="LoginCheck.jsp" method="post">
<input type="radio" name="myradio" value="1" checked="checked"/>MCA
<input type="radio" name="myradio" value="2"/>B.Tech
<input type="radio" name="myradio" value="3"/>Other
<input name="goto" type="submit" value="Login">
</form>
</body>
</html
Fetching request values:
获取请求值:
<%@ page contentType="text/html; charset=iso-8859-1" language="java" %>
<%
String myRadio= request.getParameter("myradio");
%>
To determine which button is selected, use request.getParameter("myradio")
要确定选择了哪个按钮,请使用 request.getParameter("myradio")
For values processing:
对于值处理:
if ("1".equals(myRadio)) {
// processing here
}
回答by Santhosh
i guess its because you have not specified the value for name attribute within quotes ,
我猜是因为您没有在引号中指定 name 属性的值,
Name is getting displayed because you have it inside quotes,
名称正在显示,因为您将它放在引号内,
<br/>Username:<input type="text" name="username">
So try this,
所以试试这个,
select your gender:
<select name="dropdown">
<option name="one" value="one> Male </option>
<option name="two" value="two"> Female </option>
</select>
<br/>select your department:
<input type="radio" name="myradio" value="1"/>MCA
<input type="radio" name="myradio" value="2"/>B.Tech
<input type="radio" name="myradio" value="3"/>Other
<br/> select your choices:
<input type="checkbox" name="mybox" value="1"/>java
<input type="checkbox" name="mybox"value="2"/>c++
<input type="checkbox" name="mybox"value="3"/>c
<input type="checkbox" name="mybox"value="4"/>sql
<br/>enter you comments here:
<textarea name="mytextarea" cols=20 rows=5>
</textarea>
Hope it helps !!
希望能帮助到你 !!
回答by Ali Safari
My answer may be of use for others:
我的回答可能对其他人有用:
I will show it with an example for both radio and drop-down buttons as both are used to select a singular response from a list: File: index.jsp
我将用一个单选按钮和下拉按钮的例子来展示它,因为它们都用于从列表中选择一个单一的响应:文件:index.jsp
<form action="./StudentServlet" method="post">
<div class="form-label-group">
<label for="studentCourses">Student Course Group</label>
<select id="studentCourses" name="studentCourses">
<%for (int i = 1; i <= 6; i++) {%>
<option value= "1ITF<%=i%>">1ITF<%=i%></option>
<%}%>
</select>
</div>
<div class="form-label-group">
<label for="studentPreference">Preference at the moment:</label>
<%String[] chosenArray = {"APP", "BIT", "EMDEV", "INFRA", "ANDR"};
for (int j = 0; j < chosenArray.length; j++) {%>
<p><input type="radio" name="studentPreference" value="<%=chosenArray[j]%>" id="<%=chosenArray[j]%>"/>
<label for="<%=chosenArray[j]%>"><%=chosenArray[j]%></label>
</p>
<%}%>
<input type="submit" name="studentFormSubmit" id="studentFormSubmit">
</div>
</form>
File: StudentServlet.java
文件:StudentServlet.java
protected void processRequest(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
String studentCourse = request.getParameter("studentCourses");
String studentPreference = request.getParameter("studentPreference");
Student coursePreference= new Student(studentCourse, studentPreference);
if (request.getParameter("studentFormSubmit") != null) {
request.setAttribute("coursePreference", coursePreference);
request.getRequestDispatcher("DisplayResult.jsp").forward(request, response);
}
}
And in file DisplayResult.jsp
在文件 DisplayResult.jsp 中
<h1>Course Details</h1>
<div>
<%
Student coursePreference= = (Student) request.getAttribute("coursePreference");
%>
<p>Student Course: <%=coursePreference.getStudentCourse()%></p>
<p>Student Preference: <%=coursePreference.getStudentPreference()%></p>
</div>