Jsp中的多个依赖下拉列表

时间:2023-01-25 14:17:50

I have Three Drop-downs in Jsp. Branch, Semester and Subject and values comes from MySQL. Branch and semester are not dependent to each other. But Subject value should be depends on branch and semester both. I had done following code. problem arise that though select the both branch and semester drop-down value, Subject drop-down's value not getting means shows blank drop-down field.

我在Jsp中有三个下拉列表。分支,学期和主题和价值来自MySQL。分支和学期不相互依赖。但主题价值应取决于分支和学期两者。我做了以下代码。问题出现了,虽然选择了分支和学期下拉值,但主题下拉列表的值不获取均值显示空白下拉字段。

FirstTwoDropdown.jsp

<%@page import="java.sql.*"%>

<%@page import="com.connection.*"%>
<%Connection con;
 ResultSet rs;
 Statement st;


 con = connectiondb.condb();
   %>
   <html>
      <head>  
      <script language="javascript" type="text/javascript">  
      var xmlHttp
          function showsubject(str){
              if (typeof XMLHttpRequest != "undefined"){
                xmlHttp= new XMLHttpRequest();
                }
              else if (window.ActiveXObject){
                xmlHttp= new ActiveXObject("Microsoft.XMLHTTP");
                }
              if (xmlHttp==null){
              alert("Browser does not support XMLHTTP Request")
              return;
              } 
              var url="subject.jsp";
              url +="?count=" +str;
              xmlHttp.onreadystatechange = stateChange1;
              xmlHttp.open("GET", url, true);
              xmlHttp.send(null);
              }
              function stateChange1(){   
              if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete"){   
              document.getElementById("subject").innerHTML=xmlHttp.responseText   
              }   
              }
      </script> 
      </head>  
      <body>  
      <select name='branch_name' onchange="showsemester(this.value)">  
       <option value="none">Select</option>  
    <% 
   st = con.createStatement();  
   rs = st.executeQuery("select distinct branch_name from subject_tbl");
  while(rs.next()){
     %>
      <option value="<%=rs.getString("branch_name")%>"><%=rs.getString("branch_name")%></option>  
      <%
 }
     %>
      </select>  
      <br>   
      <select name='semester' >
      <option value="none">Select</option>  
    <% 
st = con.createStatement();  
  rs = st.executeQuery("select distinct semester from subject_tbl");
 while(rs.next()){
     %>
      <option value="<%=rs.getString("semester")%> on"><%=rs.getString("semester")%></option>  
      <%
 }
     %>  
      </select>  

      <select name='subject_name' >  
      <option value=''></option>  
      </select> 
      </body> 
</html>

subject.jsp

<%@page import="java.sql.*"%>
<%@page import="com.connection.*"%>
<%
String semester=request.getParameter("semester");
String branch_name=request.getParameter("branch_name");
String buffer="<select name='subject_name'><option value=''>Select</option>"; 
Connection con;
ResultSet rs;
Statement st;
 con = connectiondb.condb();
 try{  
 st = con.createStatement();  
 rs = st.executeQuery("Select subject_name from subject_tbl where semester='"+semester+"' and branch_name='"+branch_name+"' ");  
   while(rs.next()){
   buffer=buffer+"<option value='"+rs.getString("subject_name")+"'>"+rs.getString("subject_name")+"</option>";  
   }  
 buffer=buffer+"</select>";  
 response.getWriter().println(buffer); 
 }
 catch(Exception e){
     System.out.println(e);
 }
 %>

1 个解决方案

#1


0  

Just as tip: Try to separate the business logic from the JSP files. There are different options to do this one is to use Servlets the other approach is to use Java Bean Components.

正如提示:尝试将业务逻辑与JSP文件分开。有不同的选项可以做到这一点,一个是使用Servlets,另一个方法是使用Java Bean Components。

Second tip: Close your resources.

第二个提示:关闭资源。

// Java 7 Try - with resources syntax 
try (
   Connection con = connectiondb.condb();
   Statement st = con.createStatement();  
) 
{
   try (ResultSet rs = st.executeQuery(...)) 
   {
      // do something with your result set 
   } 
}

Your database has only limited Resources like Connections, Cursors, ...

您的数据库只有有限的资源,如Connections,Cursors,......

Third tip: Use Prepared Statements. Otherwise someone may use SQL Injection to attack your system.

第三条提示:使用准备好的陈述。否则,有人可能会使用SQL注入攻击您的系统。

For your Problem: Just a guess

对于你的问题:只是一个猜测

The semester value you'll retrieve from your FirstTwoDropdown.jsp is "semester-value on" i.e. you append " on" to the value retrieved from the database.

您将从FirstTwoDropdown.jsp中检索的学期值是“semester-value on”,即您将“on”追加到从数据库中检索的值。

The other thing is that FirstTwoDropdown.jsp uses the javascript function showsemester which is not defined within the page - so this will never work. And the showsubject function sends a query parameter count which is not used on the subject.jsp.

另一件事是FirstTwoDropdown.jsp使用javascript函数showsemester,它没有在页面中定义 - 所以这将永远不会工作。并且showsubject函数发送一个查询参数count,该参数不在subject.jsp上使用。

#1


0  

Just as tip: Try to separate the business logic from the JSP files. There are different options to do this one is to use Servlets the other approach is to use Java Bean Components.

正如提示:尝试将业务逻辑与JSP文件分开。有不同的选项可以做到这一点,一个是使用Servlets,另一个方法是使用Java Bean Components。

Second tip: Close your resources.

第二个提示:关闭资源。

// Java 7 Try - with resources syntax 
try (
   Connection con = connectiondb.condb();
   Statement st = con.createStatement();  
) 
{
   try (ResultSet rs = st.executeQuery(...)) 
   {
      // do something with your result set 
   } 
}

Your database has only limited Resources like Connections, Cursors, ...

您的数据库只有有限的资源,如Connections,Cursors,......

Third tip: Use Prepared Statements. Otherwise someone may use SQL Injection to attack your system.

第三条提示:使用准备好的陈述。否则,有人可能会使用SQL注入攻击您的系统。

For your Problem: Just a guess

对于你的问题:只是一个猜测

The semester value you'll retrieve from your FirstTwoDropdown.jsp is "semester-value on" i.e. you append " on" to the value retrieved from the database.

您将从FirstTwoDropdown.jsp中检索的学期值是“semester-value on”,即您将“on”追加到从数据库中检索的值。

The other thing is that FirstTwoDropdown.jsp uses the javascript function showsemester which is not defined within the page - so this will never work. And the showsubject function sends a query parameter count which is not used on the subject.jsp.

另一件事是FirstTwoDropdown.jsp使用javascript函数showsemester,它没有在页面中定义 - 所以这将永远不会工作。并且showsubject函数发送一个查询参数count,该参数不在subject.jsp上使用。