Muhammad
Muhammad

Reputation: 631

passing int as parameter from JSP to servlet

I am trying to pass studentId of datatype int to servlet but it does not accept it and forces to change the datatype to String

    int studentId;
    String studentName;

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

I can't change it to String as in the database studentId is an integer (primary key) and my java class also takes an int as a parameter.

How can I solve this?

Upvotes: 10

Views: 102298

Answers (3)

user1364078
user1364078

Reputation: 21

private int roll;

int roll = Integer.parseInt(request.getParameter("roll"));

Here request is the object of HttpServletRequest and hence it will the getParameter() and we are getting the value as string.

Upvotes: 2

SaitejaKolloju
SaitejaKolloju

Reputation: 1

What about Mobileno?

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

Upvotes: 0

Sam Holder
Sam Holder

Reputation: 32936

you need to parse the parameter as an int. In a request the parameters are just strings at the end of the day (they were typed into a browser, or stored in the html of a webpage after all), so you need to interpret it on the server as the type you expect.

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

Upvotes: 23

Related Questions