my dog learned polymorphism*
The moose likes Servlets and the fly likes Servlet getting values from a combo box 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 » Servlets
Bookmark "Servlet getting values from a combo box" Watch "Servlet getting values from a combo box" New topic

Servlet getting values from a combo box

SJ Rao

Joined: Mar 05, 2003
Posts: 25
Need following clarification.
1) I've created a servlet which has a combo box and text boxes. The combo box values are populated from the database.
2) On selection of the combo box value, I want to search the database and display the details in the text boxes.
eg:- Combo box consists of users. On selection of a particular user, I want to display the details of that user.
Can you let me know how do i go about doing it ?
Andy Bowes
Ranch Hand

Joined: Jan 14, 2003
Posts: 171
There is no way to perform a direct query from the client browser to the database that exists on the server. The solution is basically to refresh the HTML page whenever the user selects an entry in the combo-box.
To do this you will need to do something like the following:
Add a piece of JavaScript to the combo-box that submits the form that the combo-box exists. This will need to be defined in the 'onChange' property of the combo-box.
The servlet can then identify the value of the selected id by getting the parameter from the request and use this to populate the text fields with information from the database.

Andy Bowes<br />SCJP, SCWCD<br />I like deadlines, I love the whoosing noise they make as they go flying past - Douglas Adams
Aleks V. Pascoal
Ranch Hand

Joined: Apr 21, 2002
Posts: 73
in fact the only way you can do this is 'refreshing' your page. But, i don't recomend to use this aproach, the correct would be to use a jsp and taglibs to display de combobox, and depending on the amount of information of each user, you may bring this data together with the list of users to avoid refreshing the page every time.
SJ Rao

Joined: Mar 05, 2003
Posts: 25
Thanks for the response.
I still haven't found the complete solution.
Onchange of the combo box, How do i submit the combo box data from javascript back to the servlet.
Function of this servlet is to display the data of the user selected in the combo box. Hence i need to pass the name of the user back to the servlet to browse the database for this selected user's details like residence phone, office phone, lastname etc.
I am using bea-weblogic server.
I've pasted the following code below :-
1) Below is the servlet named
package Phonebook_Project;
import java.sql.*;
import java.util.*;
import javax.servlet.*;
import javax.servlet.http.*;
public class EditPhonebookEntry extends HttpServlet
Connection con = null;
Statement stmt = null;
ResultSet rs = null;
String strSql = "";
String strUrl = "";
String strFirstName = "";
String strLastName = "";
String strHdnfirstname="";
int iCombocount = 0;
public void doGet (HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException
PrintWriter sysout = res.getWriter();
sysout.println("<Title> EDIT PHONE BOOK ENTRY PAGE </title>");
sysout.println("<hr><h1 align=center> PHONEBOOK.COM </h1></hr>");
sysout.println("<form name=EditPhonebookEntry method=post>");
sysout.println("<table border=0 cellspacing=1>");
sysout.println("<td> First Name:   </td> ");
sysout.println("<script language=\"javascript\" src=\"Phonebook_Project/fFirstname.js\">");
sysout.println("<td> <select id=cmbFirstname name=cmbFirstname onChange=\"fFirstname()\">");
rs = stmt.executeQuery(strSql);
iCombocount = 1;
while (
strFirstName = rs.getString("FirstName");
sysout.println("<option value= " + strFirstName + " > " + strFirstName + " </option>");
iCombocount ++;
catch (Exception e)
System.out.println("The error is :" + e.getMessage());


sysout.println("<td><input type=hidden name=hdnFirstname></td>");
strHdnfirstname = req.getParameter("hdnFirstname");
sysout.println("<td> Last Name:   </td>");
sysout.println("<td> <input type=text name=txtLastName value=*****></td>");
sysout.println("<td> Residence Phone:   </td>");
sysout.println("<td> <input type=text name=txtResphone value=*****></td>");
sysout.println("<td> Office Phone:   </td>");
sysout.println("<td> <input type=text name=txtOffphone value=*****></td>");
sysout.println("<td> <br><input type=submit name=butBack value=BACK> </td>");
sysout.println("<td> <br><input type=submit name=butSave value=SAVE> </td>");
sysout.println("<td> <br><input type=submit name=butDelete value=DELETE> </td>");
public void connectiondb()
strUrl = "jdbc dbc honebook";
stmt = con.createStatement();
strSql = "SELECT * FROM UserMaster";
catch (Exception e)
System.out.println("The error is :" + e.getMessage());
2) Below is the code for javascript named fFirstname.js
Note:- I've commented the lines which gives an error.
function fFirstname()
document.EditPhonebookEntry.hdnFirstname.value = document.EditPhonebookEntry.cmbFirstname.value;
Note :-
These lines work perfectly in a jsp page but not in servlet.
If I convert this into a jsp page it works perfectly well as the form is submitted and accessed within the jsp page itself. But I want to do this pgm using servlets.
Thanks in advance.
Geeta Ramasami
Ranch Hand

Joined: Mar 05, 2003
Posts: 72
Could u pl post the errors its giving when u try to submit ur page?
Phil Chuang
Ranch Hand

Joined: Feb 15, 2003
Posts: 251
As stated before, the simplest solution would probably be to send a vector of all pertinent user information to the jsp, then have some fancy javascript to display the information. However, if you're not big on javascript, this can be a pain to develop and debug.
The easiest way would probably be to have 2 separate pages, a selection page and a details page. The details page could even be a popup. Basically, the selection page would have a vector or an array of user names that is passed back to the servlet. The way to get it out of the servlet is simply "request.getParameter(paramName);" where paramName is the ID or NAME value in the combo box INPUT HTML tag. Then you would fetch the information from the DB, and pass it to the details page.
I agree. Here's the link:
subject: Servlet getting values from a combo box
Similar Threads
How to iterate using struts 2 ?
in dropdown problem
Populating the Combo Box using AJAX
Problems in Form reset button