SitePoint Sponsor

User Tag List

Results 1 to 1 of 1
  1. #1
    SitePoint Member
    Join Date
    Sep 2008
    Posts
    1
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    3 dimensional array java script in JSP getting the details from JavaBeans

    Hi,

    I have a JSP which gets the information from DB and populate the information on three combo boxes...

    Assume stateList is list of State Beans populated with some values.. I am giving you the list of java beans as well here and the code to populate the list of state beans ... (servlet is uses RequestDispatcher and forward the list(the statebeans list) to the JSP ...

    the servelt,three java beans and jsp file are given below...

    I want to select the list of districts for the selected state filed .. and need to get the list of colleges for the selected district field ...

    I am succeeded to get the list of districts when onchange of state field happens ... I want to get the list of colleges when onchange of district happens ... Please find my code below to take the initiation .. I m confused to get the three dimensional array for doing that stuff .. please try to execute the code... it is clean and working fine for two combo boxes .. please help me to get that for three comboboxes...

    servlet to populate the list of statebeans
    ---------------------------------------





    import java.io.IOException;

    import java.sql.Connection;

    import java.sql.ResultSet;

    import java.sql.SQLException;

    import java.sql.Statement;

    import java.util.ArrayList;

    import java.util.List;



    import javax.servlet.RequestDispatcher;

    import javax.servlet.ServletContext;

    import javax.servlet.ServletException;

    import javax.servlet.http.HttpServlet;

    import javax.servlet.http.HttpServletRequest;

    import javax.servlet.http.HttpServletResponse;

    import javax.servlet.http.HttpSession;



    public class CountryServlet extends HttpServlet {

    public void doGet(HttpServletRequest request,HttpServletResponse response) throws IOException, ServletException

    {

    doPost(request, response);

    }



    public void doPost(HttpServletRequest request,HttpServletResponse response) throws IOException {

    System.out.println("********* Inside the doPost method of College Servlet *********");

    response.setContentType("text/html");

    java.io.PrintWriter out = response.getWriter();



    List stateList = new ArrayList();

    List distList;

    List collList;



    StateBean stateBean = null;

    DistrictBean distBean = null;

    ColleageBean colleageBean = null;

    HttpSession session = request.getSession(true);



    try

    {

    // Retrieve state Data

    // call setters to initialize bean

    int i=1;



    while(i<=3)

    {

    stateBean = new StateBean();

    stateBean.setStateID(i);

    stateBean.setStateName("statename"+i);

    stateBean.setStateLatitude("STATELATITUDE"+i);

    stateBean.setStateLongitude("STATELONGITUDE"+i);

    distList = new ArrayList();



    for(int j=0;j<i*2;j++){



    distBean = new DistrictBean();

    distBean.setDistrictID(j);

    distBean.setDistrictName("DN"+j+"-"+stateBean.getStateID());

    distBean.setStateID(i);

    distBean.setDistrictLatitude("DistLATITUDE"+i);

    distBean.setDistrictLongitude("DistLONGITUDE"+i);

    collList = new ArrayList();



    for(int k=0;k<j*2;k++){



    colleageBean = new ColleageBean();

    colleageBean.setCollegeid(k);

    colleageBean.setCollegeName("ColN"+k+"-dist"+j+"-state-"+k);

    colleageBean.setStateID(i);

    colleageBean.setStateID(j);

    collList.add(colleageBean);

    }



    distBean.setColleageBean(collList);

    collList = null;

    distList.add(distBean);

    }



    stateBean.setDistList(distList);

    distList = null;



    stateList.add(stateBean);

    i++;

    }



    ServletContext application = getServletConfig().getServletContext();

    i=0;



    System.out.println("No Exception ");



    application.setAttribute("stateList", stateList);

    RequestDispatcher dispatcher = application.getRequestDispatcher("/GMAPSearch.jsp");



    System.out.println("No Exception 2");

    try {

    dispatcher.forward(request,response);

    }catch (ServletException e) {

    System.out.println("Exception in catch");

    e.printStackTrace();

    }

    }



    catch(Exception e){

    e.getMessage();

    }

    finally {

    //close database conncection

    try{



    }

    catch (Exception ignored) {



    }

    }

    out.close();

    }



    }


    -----------------------------------------------------------------------

    Please generate the getter and setter methods for the three beans with the given fiields ...

    CollegeBean
    ------------
    int collegeid;

    int districtID;

    int stateID;

    String collegeName;

    String collegeLatitude;

    String collegeLongitude;

    String collegeAddress;

    String collegeURL;

    -------------------------------------------

    StateBean
    ----------

    int stateID;

    String StateName;

    String StateLatitude;

    String StateLongitude;

    java.util.List<DistrictBean> distList;

    -------------------------------------------
    DistrictBean
    -----------

    int districtID;

    String districtName;

    String districtLatitude;

    String districtLongitude;

    int stateID;


    java.util.List<ColleageBean> colleageBeanList;
    --------------------------------------------------------------------------------------


    and the JSP which was forwarded from servlet
    ----------------------------------------------------------------------------------------------------------



    <&#37;@ page import="java.util.*" %>


    <META HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=us-ascii">

    <html>

    <head>

    <script type="text/javascript">

    /* Use this to store the values for states in JavaScript */

    var district2DArray;

    /* Javascript function to set up values for the states and countries */

    function initSelects() {

    <%/* This is JSP scriptlet code to get the country list that we need... */

    List states = (List)application.getAttribute("stateList");%>

    stateSelect = document.form.state;

    districtSelect = document.form.district;


    <%/* This is JSP scriptlet code to loop through the countries and assign

    * values as needed. We will break out of scriptlets to print out

    * the javascript needed to assign values to the stateSelect and to

    * the state2DArray

    */

    int stateCount = states.size();

    System.out.println("state count"+stateCount);

    %>



    district2DArray=new Array(<%=stateCount+1%>);

    district2DArray[0] = new Array(1);

    district2DArray[0][0]="Choose a District";



    <%for (int st = 1; st <= stateCount; st++) {

    StateBean state = (StateBean)states.get(st-1);
    List<DistrictBean> districtList = state.getDistList();
    int districtCount = districtList.size();

    System.out.println("ditrict coutnt for state -- "+state.getStateID()+"is "+districtCount);
    %>

    stateSelect.options[<%=st%>] = new Option("<%=state.getStateName()%>","<%=state.getStateID()%>");
    district2DArray[<%=st%>] = new Array(<%=districtCount+1%>);
    district2DArray[<%=st%>][0] = "Choose a District";

    <%
    for (int dis = 1; dis <= districtCount; dis++) {
    DistrictBean districtBean = (DistrictBean)districtList.get(dis-1);
    %>

    district2DArray[<%=st%>][<%=dis%>] = "<%=districtBean.getDistrictName()%>";

    <%

    }
    }
    %>
    }

    function fillInStates(stateSelect) {
    selectedState = stateSelect.selectedIndex;
    districtCount = district2DArray[selectedState].length;
    stateSelect = document.form.district;
    stateSelect.options.length = 0;
    for (district = 0; district < districtCount; district++) {
    stateSelect.options[district] = new Option(district2DArray[selectedState][district]);
    }
    }
    </script>
    </head>
    <body bgcolor=#998888 onload="initSelects()">
    <p align="left">
    <FORM name="form" id="form" action="#" method="get">
    <table border="1" width="50%" >
    <table border="0" color="" width="100%" id="table1" height="150">
    <tr>
    <td width="100%" height="30">
    <p align="left"><font color="#800000"></font></p>
    <p align="left">
    <tr>
    <td>Select State/td>
    <td>
    <select name="state" id="state" style="width:100pt" onchange="fillInStates(this);">
    <OPTION value="">Choose a State
    </select>
    </td>
    </tr>
    <p align="left">
    <tr>
    <td>Select District/td>
    <td>
    <select name="district" id="district" style="width:100pt" >
    <OPTION value="">Choose a District
    </select>
    </td>
    </tr>
    <p align="center">
    <tr>

    <td></td>

    <td></td>

    <td></td>

    <td></td>

    </tr>

    <tr>

    <td>

    <INPUT type="button" name="search" value="search" >

    </td>

    </tr>

    </table>

    </table>

    </FORM>

    </body>

    </html>

    ---------------------------------------------------------------------------

    Please modify my jsp / java script to make it work for three combo boxes ....

    Big Thanks in Advance,
    Vidhya...
    Last edited by vidhya517; Sep 25, 2008 at 11:04.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •