【问题标题】:JSP form submission with parameters both String and IntegerJSP表单提交,参数为String和Integer
【发布时间】:2014-08-30 08:35:43
【问题描述】:

我正在制作一个包含 String 和 Integer 变量的变量的表单。

即我从 page1.jsp 页面获取的参数是 name1,place1,country1,name2,place2,country2,name3,place3,country3。

我想获取这些变量并打印在 page2.jsp 上。

这是我迄今为止尝试过的。

    for(i=1;i>=3;i++)
    {
        String name(i) = request.getParameter("name"+i);
        String place(i) = request.getParameter("place"+i);
        String country(i) = request.getParameter("country"+i);
    }
    for(i=1;i>=3;i++)
    {
        out.println(name(i));
        out.println(place(i));
        out.println(country(i));
    }

我得到的错误是:

            Syntax error, insert ";" to complete BlockStatements
    6:      int i;
    7:      for(i=1;i>=j;i++)
    8:      {
    9:          String name(i) = request.getParameter("name"+i);
    10:         String place(i) = request.getParameter("place"+i);
    11:         String country(i) = request.getParameter("country"+i);
    12:     }

        An error occurred at line: 9 in the jsp file: /page2.jsp Type mismatch: cannot convert from String to int
        An error occurred at line: 10 in the jsp file: /page2.jsp
    Syntax error, insert ";" to complete LocalVariableDeclarationStatement

【问题讨论】:

  • 为什么你的变量中有 (i) 也许你应该使用方括号 [] 而它不是一个数组
  • @Ker p pag 我已经尝试过这种方式,但我遇到了类似 An error occurred at line: 9 in the jsp file: /page2.jsp Syntax error on token "i", delete this token,An error occurred at line: 9 in the jsp file: /page2.jsp Type mismatch: cannot convert from String to String[], 的错误

标签: java jsp


【解决方案1】:

我注意到你的循环中有错误的条件。

for(i=1;i>=3;i++) // this i>=3 will return false since i is not greater than or equal to 3. 

将循环的条件更改为小于或等于 3。

   for(i=1;i<=3;i++){
        name[i]= request.getParameter("name"+i);
        place[i] = request.getParameter("place"+i);
        country[i] = request.getParameter("country"+i);
    /*
       it will access the ff.
       "name1" to "name3"
       "place1" to "place3"
       "country1" to "country3"
     */

    }

然后将您的数组大小声明更改为 4,因为您的访问高于索引 3。

String[] name = new String[4]; 
String[] place = new String[4];
String[] country = new String[4];

您还得到java.lang.ArrayIndexOutOfBoundsException:,因为您访问了非法索引。您的索引为负数或大于或等于引发 ArrayIndexOutOfBoundsException 的数组大小,请确保您访问的是合法索引。

如果你有这样的东西,它会抛出一个 ArrayIndexOutOfBoundsException,因为你访问的是大小相等的数组;

String[] name = new String[3];
out.println(name[3]); //you can only access 0 - 2 index, thats whay it throws ArrayIndexOutOfBoundsException, 

【讨论】:

  • @user23123412 '我会尽力让你知道。感谢您的宝贵时间。'
【解决方案2】:

你没有使用数组

试着这样做

String[] name = new String[3];
String[] place = new String[3];
String[] country = new String[3];


  for(int i=0;i<3;i++)
    {
        name[i]= request.getParameter("name"+i);
        place[i] = request.getParameter("place"+i);
        country[i] = request.getParameter("country"+i);
    }
    for(int i=0;i<3;i++)
    {
        out.println(name[i]);
        out.println(place[i]);
        out.println(country[i]);
    }

【讨论】:

  • 如果我需要 'n' 个变量而不是 '3' 怎么办?
  • 从你的实现我认为数据的大小对于数组是静态的。
  • @Ker p pag '我也遇到错误'java.lang.ArrayIndexOutOfBoundsException'打印变量时'
  • 错误是这个org.apache.jasper.JasperException: An exception occurred processing JSP page /page2.jsp at line 16 13: 14: for(i=1;i&gt;3;i++) 15: { 16: name[i] = request.getParameter("name"+i); 17: place[i] = request.getParameter("place"+i); root cause java.lang.ArrayIndexOutOfBoundsException: 1
  • @Ker p pag,感谢您的尝试,但我仍然收到错误java.lang.ArrayIndexOutOfBoundsException: 1,但我已经尝试手动获取参数,甚至手动打印。
【解决方案3】:

我不知道你的上一页的价值是从哪里来的..

要存入数组,还有另外一种方法

String[] name= request.getParameterValues("name");

String[] place= request.getParameterValues("place");

String[] country= request.getParameterValues("country");

for(int i=0;i<3;i++)
{
 out.println(name[i]);

  out.println(place[i]);

  out.println(country[i]);

}

【讨论】:

    猜你喜欢
    • 2015-12-28
    • 2015-02-19
    • 1970-01-01
    • 2016-12-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多