带有参数String和Integer的JSP表单提交

Java学习者

我正在制作一个包含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
肯·德·古兹曼

我发现您的循环条件不正确。

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, 

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

从jQuery中的链接提交带有参数的表单

codeigniter 提交带有文本和图像的表单

Angular:提交带有数据和图像的表单

使用Ajax和MVC提交带有照片数据的表单

使用JQuery和AJAX提交带有文件的表单

带有提交和重置按钮的AngularJS表单验证

带有取消和提交按钮的Bootstrap模式弹出表单,无论单击哪个按钮提交表单

提交带有href的表单

提交JSP表单后如何重定向回html(带有成功/错误消息)

ASP.NET MVC Ajax 提交带有 AntiforgeryToken 的表单和带有验证的序列化表单数据

带有额外参数的JavaScript表单提交/自定义重定向

使用带有g-recaptcha-response参数的POST提交表单

Grails 2.x提交带有其他参数的表单

提交带有包含哈希的操作的表单

带有多个重定向的表单提交

如何提交带有标题的HTML表单

提交带有按钮的表单(HTML)

带有POST方法的表单提交GET

带有angularjs的Materializecss表单未提交

调用带有onChange事件的表单提交

如何正确验证和提交表单是否与带有typescript的ant design表单有效

如何在Flutter中显示带有提交按钮和隐藏输入字段的HTML表单

通过Ajax将带有文本和图像的表单提交给Laravel Controller

如何在表单提交时使用带有setTimeout和preventDefault的blockUI?

以编程方式提交带有“提交”事件的表单

带有脆皮的Django表单集-提交按钮不提交

使用JavaScript处理带有多个提交按钮的表单提交

带有JAX-RX的REST API从jsp页面中的表单发送和接收解析的JSON

带有表单验证(PHP)的表单提交预览