1) jsp file
<%@page import="java.sql.Statement"%> <%@page import="java.sql.ResultSet"%> <%@page import="controller.ConnectionString"%> <%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"> <script type="text/javascript"></script> <title>Country</title> <script type="text/javascript"> var htmlres; function showState(str) { var cou=document.getElementById("cname").value; alert("In showState function:"+cou); var url="State_drop?cname=" +cou; if(window.ActiveXObject) { htmlres=new ActiveXObject("Microsoft.XMLHTTP"); } else if(window.XMLHttpRequest) { htmlres=new XMLHttpRequest(); } htmlres.onreadystatechange=changeState; htmlres.open("post", url, true); htmlres.send(); } function changeState() { alert("In changeState function"); if(htmlres.readyState == 4 && htmlres.status == 200) { alert("In readystate==4.."); var res=htmlres.responseText; document.getElementById("state").innerHTML=res.text; } } </script> </head> <body> Select Country: <select name="cname" id="cname" onchange="showState(this.value)"> <option value="none">--Select Country--</option> <% ConnectionString cs=new ConnectionString(); cs.getConn(); Statement s=cs.conn.createStatement(); ResultSet rs=s.executeQuery("select * from country"); while(rs.next()) { //String cid=rs.getString(1); //String cname=rs.getString(2); %> <option value="<%= rs.getString(2) %>"><%= rs.getString(2) %></option> <% } s.close(); rs.close(); %> </select> <br> <br> <div id="stateid"> Select State: <select name="state" id="state"> <option value='-1'> --Select One--</option> </select> </div> </body> </html>
2) java file
package controller; import java.io.IOException; import java.io.PrintWriter; import java.sql.ResultSet; import java.sql.Statement; import javax.servlet.ServletException; import javax.servlet.annotation.WebServlet; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; @WebServlet("/State_drop") public class State_drop extends HttpServlet { private static final long serialVersionUID = 1L; public State_drop() { super(); } protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { } protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { System.out.println("In state.jsp"); PrintWriter out1=response.getWriter(); response.setContentType("text/xml"); String country=request.getParameter("cname"); System.out.println("country name is:"+ country); String result="<select name='state'><option value='-1'>Select</option>"; try { ConnectionString cs1=new ConnectionString(); cs1.getConn(); int val=0; Statement st=cs1.conn.createStatement(); Statement st1=cs1.conn.createStatement(); ResultSet rs1=st.executeQuery("select coun_id from country where coun_name= '"+country+"'"); while(rs1.next()) { val=((Number)rs1.getObject(1)).intValue(); } System.out.println("val is:"+ val); ResultSet rs2=st1.executeQuery("select * from state where coun_id='"+val+"'"); while(rs2.next()) { result=result + "<option value='"+ rs2.getString(3)+"'>"+rs2.getString(3)+"</option>"; result= result + "</select>"; System.out.println("Final result is:"+ result); out1.println("result"); } st.close(); st1.close(); rs1.close(); rs2.close(); } catch(Exception e) { out1.println(e); } } }
Im not able to get values retrived from servlet into dropdownlist of jsp page...kindly check the code attached and tell me changes in the same.
Ads