Search code examples
javahtmlhttpjspget

How to manipulate http GET request integer parameters?


I'm trying to multiply two values which are parameters which I get from a previous page. I know that I can display them separately with:

<p> val1: <%= request.getParameter( "val1" ) %> </p>

and

<p> val2: <%= request.getParameter( "val2" ) %> </p>

however when carrying out the arithmetic operation of multiplying to work out the product of these two values, the below attempt which I've tried does not work so I know this approach is incorrect

<p>product: (<%= request.getParameter( "val1" ) %> * <%= request.getParameter( "val2" ) %>)</p>

Solution

  • According to the error you get, it seems you're using Java so you should cast integer this way :

    <p>product: (<%= Integer.parseInt(request.getParameter("val1")) * Integer.parseInt(request.getParameter("val2")) %>)</p>