Passing int as parameter from JSP to servlet

I try to pass studentId of the int data type to the servlet, but it does not accept it and forces me to change the data type to String

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

I cannot change it to String, because in the database studentId is an integer (primary key) and my java class also accepts int as a parameter.

How can i solve this?

+6
source share
3 answers

you need to parse the parameter as int. In the request, the parameters are just strings at the end of the day (they were printed in the browser or stored in the html of the web page in the end), so you need to interpret it on the server as the type that you expect.

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

How about Mobileno?

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

Here, request is an HttpServletRequest object and therefore will getParameter() , and we get the value as a string .

0
source

Source: https://habr.com/ru/post/910014/


All Articles