将int作为参数从JSP传递给servlet

时间:2012-03-05 17:17:31

标签: jsp servlets

我正在尝试将数据类型 int 的studentId传递给servlet,但它不接受它并强制将数据类型更改为String

    int studentId;
    String studentName;

    studentId = request.getParameter("StudentId");   // (cannot convert from int to String).

我无法将其更改为String,因为数据库中studentId是一个整数(主键),我的java类也将int作为参数。

我该如何解决这个问题?

3 个答案:

答案 0 :(得分:18)

您需要将参数解析为int。在请求中,参数在一天结束时只是字符串(它们被输入到浏览器中,或者毕竟存储在网页的html中),因此您需要在服务器上将其解释为您期望的类型。 / p>

studentId = Integer.parseInt(request.getParameter("StudentId"));

答案 1 :(得分:0)

Mobileno怎么样?

int amobile;
amobile = Integer.parseInt(request.getParameter("amobile"));
//raising an exception

答案 2 :(得分:0)

<package name="userManagement" namespace="/user" extends="struts-default">      
        <action name="login" class="com.rm.airlines.action.Login">
            <result name="success">/user.jsp</result>
            <result name="login">/login.jsp</result>
        </action>       
</package>

此处<s:form action="/user/login"> <s:textfield label="Email" key="email" /> <s:password label="Password" key="password" /> <s:submit /> </s:form> <button onclick="openNewWidow(500,100)" > OPen </button> <script language="javascript"> function openNewWidow(width, height) { var top = parseInt((screen.availHeight) - height - 100); var left = parseInt((screen.availWidth) - (width / 2)); var features = "location=1, status=1, scrollbars=1, width=" + width + ", height=" + height + ", top=" + top + ", left=" + left; window.open("test.html", "kad", features); window.close(); } </script> 的对象,因此它会private int roll; int roll = Integer.parseInt(request.getParameter("roll")); ,我们的值为request