carteruk carteruk - 4 months ago 50
HTML Question

Convert String to Integer JSP

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

Try using this:

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

Its working for me.