|
|
| jsp |
Expert:vasu
try { Class.forName("sun.jdbc.odbc.JdbcOdbcDriver"); Connection con=DriverManager.getConnection("Jdbc:Odbc:campus","jairam","ray"); Statement stmt=con.createStatement(); ResultSet rs=stmt.executeQuery("select CountryID,Countryname from Countrydetails");
while(rs.next()) { countryid=rs.getString("CountryID"); out.println(countryid); %> <option value=<%=countryid%>><%=rs.getString("Countryname")%></option> <script> document.university.country.options[<%=indexes%>].selected = "selected"; </script> BASING UPON THIS COUNTRYID which is in option value i have to load another combo that is state. In the state table dbase i have this countryid. pls.. help me... <% } out.println(indexes); } catch(Exception ex) { out.println("Error :"+ex.getMessage()); } %>
|
| Answers |
Hi friend,
package javacode;
import java.io.*; import javax.servlet.*; import javax.servlet.http.*; import java.sql.*; import java.util.*; import java.util.ArrayList;
public class ComboboxList extends HttpServlet{ public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException{ response.setContentType("text/html"); PrintWriter pw = response.getWriter(); Connection con = null; String url = "jdbc:mysql://192.168.10.211:3306/"; String db = "amar"; String driver = "com.mysql.jdbc.Driver"; String userName ="amar"; String password="amar123"; try{ Class.forName(driver); con = DriverManager.getConnection(url+db,userName,password); Statement st = con.createStatement(); ResultSet rs = st.executeQuery("select * from Combolist"); List ulist = new ArrayList(); List clist = new ArrayList(); while(rs.next()){ request.setAttribute("userid", rs.getString(2)); request.setAttribute("city", rs.getString(3)); pw.println("userid" + " "+"city"+"<br>"); pw.println(rs.getString(2) + " " + rs.getString(3) +"<br>"); ulist.add(rs.getString(2)); clist.add(rs.getString(3)); } request.setAttribute("useridList", ulist); request.setAttribute("cityList", clist); } catch (Exception e){ pw.println(e); } RequestDispatcher dispatcher = getServletContext().getRequestDispatcher("/jsp/Combobox.jsp"); dispatcher.forward(request, response); } } ------------------------
|
jsp page.
<%@ page language="java" import = "java.util.*;"%> <% List userList; int nouser; int i=1; List cityList; int nocity; int j=1; %> <% userList=(ArrayList)request.getAttribute("useridList"); nouser = userList.size(); %>
<% cityList=(ArrayList)request.getAttribute("cityList"); nocity=cityList.size(); %> <html> <head> <title>dynamic bombobox in servlet</title> </head> <body> <br><br> <center> <table border="1" width="300px" bgcolor="bluelight" cellspacing="0" cellpadding="0"> <tr> <td width="100%"> <form method="GET" action="/examples/jsp/ComboboxList"> <font color="red"><h3 align="center">Dynamic Combobox List</font></h3> <table border="1" width="300px" cellspacing="0" cellpadding="0"> <tr> <td width="50%"><b>User Name:</b></td> <td width="50%"><select name="userid" value=""> <option value="0">---Select Name---</option> <% Iterator userit= userList.iterator(); while(userit.hasNext()){ while(i<=nouser){%> <option value="<%=i%>"><%=userit.next()%></option> <% i++; } } %> </select> </td> </tr> <tr> <td width="50%"><b>City:</b></td> <td width="50%"> <select name="city" value=""> <option value="0">----Select city----</option> <% Iterator cityit= cityList.iterator(); while(cityit.hasNext()){ while(j<=nocity){%> <option value="<%=j%>"><%=cityit.next()%></option> <% j++; } } %> </select> </td> </tr> </table> </form> </td> </tr> </table> </center> </body>
</html>
|
Please visit for more information.
http://www.roseindia.net/jsp/comboSelect.shtml
Thanks.
|
| More Questions |
|
|
Post Answers
Ask Question
Facing Programming Problem?
|
|
|
|
|