使用数据库条目填充下拉列表

时间:2014-03-26 04:44:35

标签: java html jsp servlets jdbc

我正在尝试使用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)

2 个答案:

答案 0 :(得分: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);

答案 1 :(得分:0)

使用jstl填充

添加

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

在jsp的顶部。

然后,填充选择框,如:

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

并在你的servlet中:

在重新审核后,元素在请求中添加列表:

request.setAttribute("res1",list);

注意:在类路径中添加jstl.jar以使用c:标记和avoid scriptlets in jsp