Anlee
asked on
How to get selected data from a select box in a JSP?
Hi there,
I know this is a simple question to you. However, it is not to me as I already send half day on it without lucky.
I have a simple JSP file that populates a select box containing several strings, and a push button OK. I am wondering how to get the selected data from the select box, pass the data to a mission (I already know how to do this), and redirect to another JSP when the push button is pressed.
I found some samples using JavaScript. However, I don’t know how to use it with JSP and I think Java code in the JSP can do the same job.
Could anyone here please help me with this or points me a sample resource?
Thank you in advance,
Anlee
I know this is a simple question to you. However, it is not to me as I already send half day on it without lucky.
I have a simple JSP file that populates a select box containing several strings, and a push button OK. I am wondering how to get the selected data from the select box, pass the data to a mission (I already know how to do this), and redirect to another JSP when the push button is pressed.
I found some samples using JavaScript. However, I don’t know how to use it with JSP and I think Java code in the JSP can do the same job.
Could anyone here please help me with this or points me a sample resource?
Thank you in advance,
Anlee
ASKER
Thanks for the quick response.
I must get the data from the select box(es) in this JSP and validate them, and then send them (with other data) to another JSP. In the other words, I should have fully control to the data. Thus your solution is not applicable in mu case. Any other idea?
Anlee
I must get the data from the select box(es) in this JSP and validate them, and then send them (with other data) to another JSP. In the other words, I should have fully control to the data. Thus your solution is not applicable in mu case. Any other idea?
Anlee
Or you try this
main.html
======
<html>
<form action="process.jsp" method="post">
<SELECT NAME="mylist">
<OPTION VALUE="m1">Cape Fear
<OPTION VALUE="m2">The Good, the Bad and the Ugly
<OPTION VALUE="m3">The Omen
<OPTION VALUE="m4">The Godfather
<OPTION VALUE="m5">Forrest Gump
</SELECT>
<SELECT NAME="mylist2">
<OPTION VALUE="m1">Cape Fear
<OPTION VALUE="m2">The Good, the Bad and the Ugly
<OPTION VALUE="m3">The Omen
<OPTION VALUE="m4">The Godfather
<OPTION VALUE="m5">Forrest Gump
</SELECT>
<input type="submit" value="Send Data">
</form>
</html>
process.jsp
========
<%
String mylist1 = request.getParameter("myli st1");
String mylist2 = request.getParameter("myli st2");
//do validate mylist1, mylist2
// if everything ok, send them(with other data) to another JSP
String otherData = "abc";
request.setAttribute("othe r1", otherData);
RequestDispatcher rd = request.getRequestDispatch er("anothe r.jsp");
rd.forward(request, response);
%>
another.jsp
========
<%
String mylist1 = request.getParameter("myli st1");
String mylist2 = request.getParameter("myli st2");
String otherData = request.getParameter("othe r1");
%>
<html>
<%=mylist1%>
<%=mylist2%>
<%=otherData%>
</html>
Hope this help
main.html
======
<html>
<form action="process.jsp" method="post">
<SELECT NAME="mylist">
<OPTION VALUE="m1">Cape Fear
<OPTION VALUE="m2">The Good, the Bad and the Ugly
<OPTION VALUE="m3">The Omen
<OPTION VALUE="m4">The Godfather
<OPTION VALUE="m5">Forrest Gump
</SELECT>
<SELECT NAME="mylist2">
<OPTION VALUE="m1">Cape Fear
<OPTION VALUE="m2">The Good, the Bad and the Ugly
<OPTION VALUE="m3">The Omen
<OPTION VALUE="m4">The Godfather
<OPTION VALUE="m5">Forrest Gump
</SELECT>
<input type="submit" value="Send Data">
</form>
</html>
process.jsp
========
<%
String mylist1 = request.getParameter("myli
String mylist2 = request.getParameter("myli
//do validate mylist1, mylist2
// if everything ok, send them(with other data) to another JSP
String otherData = "abc";
request.setAttribute("othe
RequestDispatcher rd = request.getRequestDispatch
rd.forward(request, response);
%>
another.jsp
========
<%
String mylist1 = request.getParameter("myli
String mylist2 = request.getParameter("myli
String otherData = request.getParameter("othe
%>
<html>
<%=mylist1%>
<%=mylist2%>
<%=otherData%>
</html>
Hope this help
ASKER
Your solution is saying that in order to get the selected data from the select boxes, I have to go to another JSP. Is it true? Is there another solution that gets the selected data RIGHT in the current JSP?
Anlee
Anlee
ASKER
Your solution is great if the number of select boxes is fixed. However, in my case, the number of the select boxes is dynamic, it can easily be determined in the current JSP, and be difficult in the another JSP. That is why I am asking the solution.
Anlee
Anlee
You can submit data to current jsp
process.jsp
========
<%
String mylist1 = request.getParameter("myli st1");
String mylist2 = request.getParameter("myli st2");
//do validate mylist1, mylist2
// if everything ok, send them(with other data) to another JSP
String otherData = "abc";
request.setAttribute("othe r1", otherData);
RequestDispatcher rd = request.getRequestDispatch er("anothe r.jsp");
rd.forward(request, response);
%>
<html>
<form action="process.jsp" method="post">
<SELECT NAME="mylist">
<OPTION VALUE="m1">Cape Fear
<OPTION VALUE="m2">The Good, the Bad and the Ugly
<OPTION VALUE="m3">The Omen
<OPTION VALUE="m4">The Godfather
<OPTION VALUE="m5">Forrest Gump
</SELECT>
<SELECT NAME="mylist2">
<OPTION VALUE="m1">Cape Fear
<OPTION VALUE="m2">The Good, the Bad and the Ugly
<OPTION VALUE="m3">The Omen
<OPTION VALUE="m4">The Godfather
<OPTION VALUE="m5">Forrest Gump
</SELECT>
<input type="submit" value="Send Data">
</form>
</html>
process.jsp
========
<%
String mylist1 = request.getParameter("myli
String mylist2 = request.getParameter("myli
//do validate mylist1, mylist2
// if everything ok, send them(with other data) to another JSP
String otherData = "abc";
request.setAttribute("othe
RequestDispatcher rd = request.getRequestDispatch
rd.forward(request, response);
%>
<html>
<form action="process.jsp" method="post">
<SELECT NAME="mylist">
<OPTION VALUE="m1">Cape Fear
<OPTION VALUE="m2">The Good, the Bad and the Ugly
<OPTION VALUE="m3">The Omen
<OPTION VALUE="m4">The Godfather
<OPTION VALUE="m5">Forrest Gump
</SELECT>
<SELECT NAME="mylist2">
<OPTION VALUE="m1">Cape Fear
<OPTION VALUE="m2">The Good, the Bad and the Ugly
<OPTION VALUE="m3">The Omen
<OPTION VALUE="m4">The Godfather
<OPTION VALUE="m5">Forrest Gump
</SELECT>
<input type="submit" value="Send Data">
</form>
</html>
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
All your samples are working great. Thank you very much!
Anlee
Anlee
So:
<form action="process.jsp" method="post">
<SELECT NAME="mylist">
<OPTION VALUE="m1">Cape Fear
<OPTION VALUE="m2">The Good, the Bad and the Ugly
<OPTION VALUE="m3">The Omen
<OPTION VALUE="m4">The Godfather
<OPTION VALUE="m5">Forrest Gump
</SELECT>
<input type="submit" value="Send Data">
</form>
and in process.jsp
<%@ taglib prefix="c" uri="http://java.sun.com/jstl/core" %>
<html>
<head>
<title>Process.jsp</title>
</head>
<body>
You selected:<c:out value="${param.mylist}">
</body>
</html>