Convert String to Integer JSP

carteruk picture carteruk · Dec 1, 2014 · Viewed 73.3k times · Source

I am a beginner using JSP. I want to display a list of incrementing integers using a maximum range of the users choice.

Entering: 6 should display the following:

  • number 1
  • number 2
  • number 3
  • number 4
  • number 5
  • number 6

input.jsp

<body>
<input type="number" name="numberMax" required>
<input type="submit" value="submit">
</body>

jspResult.jsp

<body>
<% 
int numberMax = request.getParameter("numberMax");  // Cannot convert from String to int
%>
for (int i = 1; i <= numberMax; i++) 
{ %>
<ul>
<li><%= i %></li>
</ul>
<% } %>
</body>

How can I convert the input to an integer in order for the jsp scriptlet to print.

Answer

Megha Sharma picture Megha Sharma · Dec 1, 2014

Try using this:

<%int no = Integer.parseInt(request.getParameter("numberMax"));%>

Its working for me.