如何在Servlet中使用for循环将getParameterValues(“”)存储到数组列表中?

时间:2018-07-26 06:34:39

标签: jsp servlets

有一个for循环可在“日历”上显示日期,用户可以选择多天,因为每天旁边都会有一个复选框,如下所示。

JSP:

<td class="day_cell"> <%=days[i][j]%> <input type="checkbox" value="<%=days[i][j]%>/<%=monthName%>/<%=intYear%>" name="date"> </td>

以下是我的Servlet中的代码。我从多个复选框获取日期。如何将日期保存到数组列表中?

Servlet:

Calendar cal = new Calendar();

cal.setDate(request.getParameterValues("date"));
String[] results = request.getParameterValues("date");

for (int i = 0; i < results.length; i++) {
        System.out.print(results[i]); 
    }

try {
     DBAO myDatabase = new DBAO();
     myDatabase.updateDate(cal, results);
     request.getRequestDispatcher("confirm-schedule.jsp").forward(request,response);

    }catch(Exception ex) {

    }

更新的数据库:

public void updateDate(Calendar cal, String[] results) throws Exception{
        try {
            System.out.println("entering");
            String insertStatement = "Insert into uskill.timetable (date)";
            insertStatement = insertStatement +"values (?)";
            PreparedStatement prepStmt = con.prepareStatement(insertStatement);
            for(String s:cal.getDate()){
                     prepStmt.setString(1, s);
                     prepStmt.addBatch(); 
                     System.out.println(cal.getDate());
                }

            System.out.println("Entered");

            System.out.println(prepStmt.toString());

            int status[] = prepStmt.executeBatch();;

            if(status[0] != 0) {
                System.out.println("Recorded Added");
            }
        } catch(Exception ex) {

            ex.printStackTrace();
            throw new Exception("Error:" + ex.getMessage());
       }
    }

错误:

java.lang.NullPointerException
at database.DBAO.updateDate(DBAO.java:119)
at servlet.CalendarServlet.doPost(CalendarServlet.java:91)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:660)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:741)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:231)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:53)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:199)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:96)
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:494)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:139)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:92)
at org.apache.catalina.valves.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java:651)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:87)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:343)
at org.apache.coyote.http11.Http11Processor.service(Http11Processor.java:412)
at org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:66)
at org.apache.coyote.AbstractProtocol$ConnectionHandler.process(AbstractProtocol.java:754)
at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1385)
at org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:49)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
at java.lang.Thread.run(Thread.java:748)

1 个答案:

答案 0 :(得分:0)

public void updateDate(Calendar cal, String[] results) {
        try {
            System.out.println("entering");
            String insertStatement = "Insert into uskill.timetable (date)";
            insertStatement = insertStatement +"values (?)";
            PreparedStatement prepStmt = con.prepareStatement(insertStatement);
            for(String s:cal.getDate()){
                 prepStmt.setString(1, s);
                 prepStmt.addBatch(); 
            }

            System.out.println("Entered");

            System.out.println(prepStmt.toString());

            int status[] = preparedStatement.executeBatch();;

            if(status[0] != 0) {
                System.out.println("Recorded Added");
            }
        }catch(Exception ex) {
            throw new Exception("Error:" + ex.getMessage());
        }
    }

希望这会对您有所帮助。