Solved

How to get selected data from a select box in a JSP?

Posted on 2004-08-17
8
615 Views
Last Modified: 2008-01-09
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
0
Comment
Question by:Anlee
  • 4
  • 3
8 Comments
 
LVL 3

Expert Comment

by:w0lver
ID: 11825961
just send select box values to another .jsp page?

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>
0
 

Author Comment

by:Anlee
ID: 11826604
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
0
 
LVL 11

Expert Comment

by:sompol_kiatkamolchai
ID: 11826899
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("mylist1");
String mylist2 = request.getParameter("mylist2");
//do validate mylist1, mylist2
// if everything ok, send them(with other data) to another JSP
String otherData = "abc";
request.setAttribute("other1", otherData);
RequestDispatcher rd = request.getRequestDispatcher("another.jsp");
rd.forward(request, response);
%>

another.jsp
========
<%
String mylist1 = request.getParameter("mylist1");
String mylist2 = request.getParameter("mylist2");
String otherData = request.getParameter("other1");
%>
<html>
<%=mylist1%>
<%=mylist2%>
<%=otherData%>
</html>

Hope this help
0
 

Author Comment

by:Anlee
ID: 11827119
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
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

Author Comment

by:Anlee
ID: 11827139
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
0
 
LVL 11

Expert Comment

by:sompol_kiatkamolchai
ID: 11827373
You can submit data to current jsp

process.jsp
========
<%
String mylist1 = request.getParameter("mylist1");
String mylist2 = request.getParameter("mylist2");
//do validate mylist1, mylist2
// if everything ok, send them(with other data) to another JSP
String otherData = "abc";
request.setAttribute("other1", otherData);
RequestDispatcher rd = request.getRequestDispatcher("another.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>
0
 
LVL 11

Accepted Solution

by:
sompol_kiatkamolchai earned 125 total points
ID: 11827404
And for dynamic select box, try this example and apply to your work.
Tip:Enumeration is and utility in java that can keep objects and provide api for common iterate those objects.
process.jsp
=======
<%@ page import="java.util.*"%>
<%
Enumeration enum = request.getParameterNames();
while(enum.hasMoreElements()) {
  String param = (String)enum.nextElement();
  String paramValue = request.getParameter(param);
  out.println(param + "=" + paramValue);
}
%>
<html>
  <form action="process.jsp" method="post">
<%
for(int i=0; i<5; i++) {
%>
            <SELECT NAME="mylist<%=i%>">
                    <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="Submit">
  </form>
</html>
0
 

Author Comment

by:Anlee
ID: 11840163
All your samples are working great. Thank you very much!

Anlee
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
bean mapper frameworks 5 59
dateFashion java challenge 9 132
memory representation of int[][][] a=new int[2][][]; 10 59
how to add new optional parameter to JSP 1 29
Some code to ensure data integrity when using macros within Excel. Also included code that helps secure your data within an Excel workbook.
A customer recently asked me about anti-malware and the different deployment options available for his business. Daily news about cyberattacks, zero-day vulnerabilities, and companies that suffered a security breach made him wonder if the endpoint a…
This Micro Tutorial hows how you can integrate  Mac OSX to a Windows Active Directory Domain. Apple has made it easy to allow users to bind their macs to a windows domain with relative ease. The following video show how to bind OSX Mavericks to …
In this video I am going to show you how to back up and restore Office 365 mailboxes using CodeTwo Backup for Office 365. Learn more about the tool used in this video here: http://www.codetwo.com/backup-for-office-365/ (http://www.codetwo.com/ba…

896 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now