• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • paul wheaton
  • Jeanne Boyarsky
  • Ron McLeod
Sheriffs:
  • Paul Clapham
  • Liutauras Vilda
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
Bartenders:

Servlet getting values from a combo box

 
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ?
 
Ranch Hand
Posts: 171
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
HTH
 
Ranch Hand
Posts: 73
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Well,
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.
Aleks
 
SJ Rao
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 EditPhonebookEntry.java
package Phonebook_Project;
import java.io.*;
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
{
res.setContentType("text/html");
PrintWriter sysout = res.getWriter();
sysout.println("<html>");
sysout.println("<head>");
sysout.println("<Title> EDIT PHONE BOOK ENTRY PAGE </title>");
sysout.println("</head>");
sysout.println("<hr><h1 align=center> PHONEBOOK.COM </h1></hr>");
sysout.println("<body>");
sysout.println("<form name=EditPhonebookEntry method=post>");
sysout.println("<table border=0 cellspacing=1>");
sysout.println("<tr>");
sysout.println("<td> First Name:   </td> ");
sysout.println("<script language=\"javascript\" src=\"Phonebook_Project/fFirstname.js\">");
sysout.println("</script>");
sysout.println("<td> <select id=cmbFirstname name=cmbFirstname onChange=\"fFirstname()\">");
connectiondb();
try
{
rs = stmt.executeQuery(strSql);
iCombocount = 1;
while (rs.next())
{
strFirstName = rs.getString("FirstName");
sysout.println("<option value= " + strFirstName + " > " + strFirstName + " </option>");
iCombocount ++;
}
rs.close();
}
catch (Exception e)
{
System.out.println("The error is :" + e.getMessage());
}

sysout.println("</select>");
sysout.println("</td>");

sysout.println("<td><input type=hidden name=hdnFirstname></td>");
strHdnfirstname = req.getParameter("hdnFirstname");
System.out.println(strHdnfirstname);
sysout.println("<td> Last Name:   </td>");
sysout.println("<td> <input type=text name=txtLastName value=*****></td>");
sysout.println("</tr>");
sysout.println("<tr>");
sysout.println("<td> Residence Phone:   </td>");
sysout.println("<td> <input type=text name=txtResphone value=*****></td>");
sysout.println("</tr>");
sysout.println("<tr>");
sysout.println("<td> Office Phone:   </td>");
sysout.println("<td> <input type=text name=txtOffphone value=*****></td>");
sysout.println("</tr>");
sysout.println("<tr>");
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>");
sysout.println("</tr>");
sysout.println("</table>");
sysout.println("</form>");
sysout.println("</body>");
sysout.println("</html>");
};
public void connectiondb()
{
try
{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
strUrl = "jdbc dbc honebook";
con=DriverManager.getConnection(strUrl,"sa","");
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;
//document.EditPhonebookEntry.action="http://localhost:8001/EditPhonebookEntry.java";
//Location="http://localhost:8001/EditPhonebookEntry.java"
//document.EditPhonebookEntry.method="post";
//document.EditPhonebookEntry.submit();
}
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.
 
Ranch Hand
Posts: 72
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Could u pl post the errors its giving when u try to submit ur page?
 
Ranch Hand
Posts: 251
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Nothing up my sleeve ... and ... presto! A tiny ad:
Smokeless wood heat with a rocket mass heater
https://woodheat.net
reply
    Bookmark Topic Watch Topic
  • New Topic