【问题标题】:Multiple Dependent Dropdown list in JspJsp中的多个依赖下拉列表
【发布时间】:2014-03-30 12:06:38
【问题描述】:

我在 Jsp 中有三个下拉菜单。 Branch、Semester 和 Subject 以及值来自 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>

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

【问题讨论】:

  • 为什么不使用 servlet 进行数据库事务和业务逻辑?一切都在 jsp 中混在一起了。

标签: java mysql jsp


【解决方案1】:

提示:尝试将业务逻辑与 JSP 文件分开。 有不同的选项可以做到这一点,一种是使用Servlets,另一种方法是使用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 
   } 
}

您的数据库只有 有限 资源,例如 ConnectionsCursors、...

第三个​​技巧:使用准备好的语句。否则有人可能会使用SQL Injection 来攻击您的系统。

你的问题:只是猜测

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

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-04-27
    • 1970-01-01
    • 2023-04-05
    • 2021-04-07
    • 1970-01-01
    相关资源
    最近更新 更多