JSP中的arraylist.get(int i)方法出现问题

时间:2019-04-26 14:14:54

标签: java jsp servlets arraylist

我使用TomCat,但收到错误消息“ HTTP状态500-内部服务器错误org.apache.jasper.JasperException:在第[25]行处理[/login.jsp]时发生异常。” 我不懂为什么。除了arryalist的get(i)方法之外,其他所有方法都可以正常运行。 这是代码:

<html>

  <head>
    <title>
      Registrazione
    </title>
  </head>

  <body>
    <%@ page import="java.util.ArrayList" %>
    <%! ArrayList<String> utenti = new ArrayList<String>(); %>
    <%!String u; %>

    <%
      ServletContext us = getServletConfig().getServletContext();
      ServletContext pw = getServletConfig().getServletContext();
      ServletContext cont = getServletConfig().getServletContext();
      //utenti.add(request.getParameter("usern"));
      //in base al username e password inseriti controlla se presenti dentro l'arraylist ed in caso positivo fa login, altrimenti no
      String use = request.getParameter("ulog");
      String pass = request.getParameter("plog");
      int conta=(int) cont.getAttribute("Conta");
      u = (String) us.getAttribute("Username"+conta);
      utenti.add(u);
      for(int i=0;i<=conta;i++){
        out.println(utenti.get(i));
      }
      out.println("USE " + use + "<br>");
      out.println("ARRAY " + utenti + "<br>");
      out.println("CONTA: " + conta);
      //out.println(cont.getAttribute("    Conta") + " " + us.getAttribute("Username"+conta));
    %>
      <a href="index.html"> CLICCA </a>

  </body>

</html>

1 个答案:

答案 0 :(得分:1)

最有可能 ArrayIndexOutofBounceException 确保conta始终小于utenti

的大小

或简单地使用

for(int i=0;i<Math.min(conta, utenti.size());i++){
        out.println(utenti.get(i));
      }

而不是避免ArrayIndexOutofBounceException