This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes JSP and the fly likes How pass the selected values of the Combo box to the servlet.... Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » JSP
Bookmark "How pass the selected values of the Combo box to the servlet...." Watch "How pass the selected values of the Combo box to the servlet...." New topic
Author

How pass the selected values of the Combo box to the servlet....

Narasimha Rao B.
Ranch Hand

Joined: Aug 26, 2002
Posts: 205
Hi,
In one of the Jsp page, i am having a combo box which will display the static data. Upon selecting the value from the drop down combo box and clicking on button, it will navigagte to the servlet and will display the selected value from the combo box.
Below are the codings for JSP and servlets. My problem here is, selected value of the combo box is not passing to the servlet.
Please tell me, where am i doing wrong and please give me the correct code. ( i am sure my JSP coding is not correct.)
I am not sure, whether this question belongs to Servlet forum or JSP forum, hence i am posting in both the places.
JSP.
-----------------------------------------------------
<html>
<head>
<title>Combo
</title>
</head>
<body>
<form method=post action="http://localhost:8080/servlets/test/radio">

<select id='cmb'>
<option>SERVLET</option>
<option>SERVLET1</option>
<option>SERVLET2</option>
</select>
<input type=submit value="OK">
</form>
</body>
</html>
---------------------------------------------------------------
Servlet
-----------------------------------------
package test;
import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
import java.util.*;
public class radio extends HttpServlet{
public void init(ServletConfig sc) throws ServletException//, IOException
{
System.out.println("Into Init of test1");
super.init(sc);
}
public void doPost(HttpServletRequest req,HttpServletResponse res) throws ServletException,IOException{
res.setContentType("text/html");
PrintWriter out=res.getWriter();
String str=req.getParameter("cmb");
if(str!=null){
out.println("You have selected "+str);
}
else{
out.println("You have not selected any thing");
}
}
public void doGet(HttpServletRequest req,HttpServletResponse res) throws ServletException,IOException{
doPost(req,res);
System.out.println(req.getMethod());
}
}
-------------------------------------------------------------------------------
Thanks in Advance,
Narasimha.


Narasimha
Faisal Khan
Ranch Hand

Joined: Jun 29, 2003
Posts: 285
You need request.getParameterValues("cmb") in your servlet (which returns a String[]) and you need to give values to the options on your html page.


The secret to creativity is knowing how to hide your sources.
Jeroen Wenting
Ranch Hand

Joined: Oct 12, 2000
Posts: 5093
this board isn't entirely threadsafe it seems.
I posted much the same at what must have been the same moment but it got lost...


42
Narasimha Rao B.
Ranch Hand

Joined: Aug 26, 2002
Posts: 205
Hi,
When i use the below code in JSP,
---------------
<select id='cmb'>
<option value="SERVLET">SERVLET</option>
<option value="SERVLET">SERVLET1</option>
<option value="SERVLET">SERVLET2</option>
-----------------------------------
and the below code in servlet
--------------------------
String str=(String)req.getParameter("cmb");
-----------------------------------
After this statement in the servlet, 'str' value is null. Means, JSP is not passing the selected values to Servlet.
Anand Loganathan
Greenhorn

Joined: Sep 13, 2003
Posts: 12
try this code for your combo box instead of your code.
<select id='cmb'>
<option value="servlet">SERVLET</option>
<option value="servlet1">SERVLET1</option>
<option value="servlet2">SERVLET2</option>
</select>.

The above code will work fine.
[ September 19, 2003: Message edited by: Anand Loganathan ]
Anand Loganathan
Greenhorn

Joined: Sep 13, 2003
Posts: 12
hey,
instead of using ID use name
<select id="cmb"> // Incorrect
ID is not used to capture values manipulated by user in HTML
Instead of that use "Name" property
<select name="cmb"> // Correct

Try this one, and this one will work fine.
Narasimha Rao B.
Ranch Hand

Joined: Aug 26, 2002
Posts: 205
Hi Anand,
Thnaks a lot, it is working fine now. After implementing your logic.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: How pass the selected values of the Combo box to the servlet....
 
Similar Threads
How pass the selected values of the Combo box to the servlet....
Strange problem - request forwarding
Requested Dispatcher
Error 92
HTTP method GET is not supported by this URL