2014-03-26 61 views
0

我想在使用servlet的下拉菜單中填充數據庫GroupNames。 如何做到這一點?請幫助使用數據庫條目填充下拉菜單

在我的JSP文件時遇到:

<% 
ArrayList<String> list1 = (ArrayList<String>)request.getAttribute("res1"); 
%> 
<select name="GName" > 
<% 
    for(String list:list1) 
{ %> 
<option><%=list%></option> 

<% } 
%> 

在我的servlet我在這樣一個ArrayList獲取所有的數據庫名稱:

ArrayList<String> list = new ArrayList<String>(); 
      out.println("HI GROUP MEMBER"); 
      String query="Select GNAME from tbGroup"; 
      ps = con.prepareStatement(query); 
      rs=ps.executeQuery(); 
      while(rs.next()){ 
       list.add(rs.getString("GNAME")); 


      } 
      String[] arr1 = list.toArray(new String[list.size()]); 
      request.setAttribute("res1",arr1); 

      int i; 
      for(i=0;i<list.size();i++){ 
       out.println(arr1[i]); 
      } 

現在我得到空在它的指針異常。請幫助可能是什麼原因呢?

下面是完整的錯誤:

Stacktrace:] with root cause 
java.lang.NullPointerException 
at org.apache.jsp.GroupLoginScreen_jsp._jspService(GroupLoginScreen_jsp.java:91) 
at org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70) 
at javax.servlet.http.HttpServlet.service(HttpServlet.java:722) 
at org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:419) 
at org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:390) 
at org.apache.jasper.servlet.JspServlet.service(JspServlet.java:333) 
at javax.servlet.http.HttpServlet.service(HttpServlet.java:722) 
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:304) 
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210) 
at org.netbeans.modules.web.monitor.server.MonitorFilter.doFilter(MonitorFilter.java:393) 
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:243) 
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210) 
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:240) 
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:164) 
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:462) 
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:164) 
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:100) 
at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:563) 
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118) 
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:399) 
at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:317) 
at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:204) 
at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:311) 
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1110) 
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:603) 
at java.lang.Thread.run(Thread.java:722) 
+0

更容易學習如何使用MVC框架如struts,stripes或Spring MVC –

+0

@Java1我想在html本身做這件事。不是在jsp中。 – user3462609

+2

HTML是靜態文檔。你應該使用JSP。 –

回答

0
<% 
    ArrayList<String> list1 = (ArrayList<String>)request.getAttribute("res1"); 
%> 
<select name="GName" > 
    <% 
     for(String list:list1) 
    { %> 
    <option><%=list%></option> 

    <% } 
    %> 
</select> 

使用這在你的JSP。 servlet代碼

  ArrayList<String> list = new ArrayList<String>(); 
      out.println("HI GROUP MEMBER"); 
      String query="Select GNAME from tbGroup"; 
      ps = con.prepareStatement(query); 
      rs=ps.executeQuery(); 
      while(rs.next()){ 
       list.add(rs.getString("GNAME")); 
      } 
      request.setAttribute("res1",list); 
      RequestDispatcher rd= context.getRequestDispatcher("GroupLoginScreen.jsp"); 
      rd.forward(request, response); 
+0

仍然是例外。 – user3462609

+0

@ user3462609在您的問題中發佈異常。 –

+0

我用ti發表我的問題 – user3462609

0

填入使用jstl

在JSP的頂部添加

<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %> 

然後,填充喜歡選擇框:

<select> 
    <option value="NONE">--None--</option> 
    <c:forEach items="${res1}" var="itm"> 
    <option value="${itm}">${itm}</option> 
    </c:forEach> 
</select> 

,並在你的servlet:

後retrived元素在請求加入列表:

request.setAttribute("res1",list); 

注:添加jstl.jar在你的類路徑使用c:標籤,而avoid scriptlets in jsp

+0

我試過了,它給出了下面的異常:org.apache.jasper.JasperException:絕對的uri:http://java.sun.com/jsp/jstl/core無法在任何web中解析。xml或與此應用程序一起部署的jar文件 – user3462609

+0

@ user3462609您尚未在您的類路徑中添加jstl,請將其添加到頂部的鏈接添加到答案中。 – Rembo

+0

它只顯示NONE在下拉菜單 – user3462609