Jsp中的多个从属下拉列表 [英] Multiple Dependent Dropdown list in Jsp

查看:62
本文介绍了Jsp中的多个从属下拉列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在Jsp中有三个下拉菜单.分支,学期和主题,值来自MySQL.分支机构和学期不相互依赖.但是学科的价值应该取决于分支机构和学期. 我已经完成了以下代码. 出现的问题是,尽管选择了branch和semester下拉列表中的两个值,但主题"下拉列表中的值未获得表示空白的下拉字段.

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.

FirstTwoDropdown.jsp

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

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);
 }
 %>

推荐答案

仅作为提示:尝试将业务逻辑与JSP文件分开. 有多种方法可以执行此操作,一种是使用Servlets,另一种方法是使用

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.

第二个提示:关闭您的资源.

// 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 
   } 
}

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

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

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

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

针对您的问题:只是一个猜测

您将从FirstTwoDropdown.jsp中检索的学期值是"学期值 on",即,将"on"附加到从数据库中检索到的值上.

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使用了页面中未定义的javascript函数showsemester-因此它将永远无法使用.然后showsubject函数发送一个查询参数count,该参数未在subject.jsp上使用.

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.

这篇关于Jsp中的多个从属下拉列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆