我想在使用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)
更容易學習如何使用MVC框架如struts,stripes或Spring MVC –
@Java1我想在html本身做這件事。不是在jsp中。 – user3462609
HTML是靜態文檔。你應該使用JSP。 –