I wanna make a servlet where you write your name, surname, email address and you have to enter the correct sum of two numbers.
I've got one problem. When I don't enter anything in this "captcha", it gives me HTTP Status 500, Message: HTTP Status 500
Exception:
java.lang.NumberFormatException: For input string: ""
java.base/java.lang.NumberFormatException.forInputString(NumberFormatException.java:68)
java.base/java.lang.Integer.parseInt(Integer.java:662)
java.base/java.lang.Integer.parseInt(Integer.java:770)
pl.coderslab.Sess05.doPost(Sess05.java:16)
javax.servlet.http.HttpServlet.service(HttpServlet.java:652)
javax.servlet.http.HttpServlet.service(HttpServlet.java:733)
org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:53)
I'd like it to return to the form so you have to type the captcha (I'm not checking the other fields, don't pay attention to it).
Here's my code:
@WebServlet("/Sess05")
public class Sess05 extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
HttpSession sess = request.getSession();
int userSum = Integer.parseInt(request.getParameter("captchaInput")); //Here's 16 line
int correctSum = (int) sess.getAttribute("captcha");
if (userSum != correctSum || request.getParameter("captchaInput").equals("")) {
Random randGenerator = new Random();
int firstRand = randGenerator.nextInt(101);
int secRand = randGenerator.nextInt(101);
int sum = firstRand + secRand;
sess.setAttribute("captcha", sum);
response.getWriter().append("<form action='#' method='post'>");
response.getWriter().append("<label>Name: <input type='text' name='name'></label><br>");
response.getWriter().append("<label>Surname: <input type='text' name='surname'></label><br>");
response.getWriter().append("<label>Email: <input type='email' name='email'></label><br>");
response.getWriter().append("<label>Enter the sum " + firstRand + " + " + secRand + "<input type='number' name='captchaInput'></label><br>");
response.getWriter().append("<input type='submit' value='Submit'>");
response.getWriter().append("</form>");
response.getWriter().append("Enter the correct sum!");
} else {
response.getWriter().append("Sum is correct!");
}
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
Random randGenerator = new Random();
int firstRand = randGenerator.nextInt(101);
int secRand = randGenerator.nextInt(101);
int sum = firstRand + secRand;
HttpSession sess = request.getSession();
sess.setAttribute("captcha", sum);
response.getWriter().append("<form action='#' method='post'>");
response.getWriter().append("<label>Name: <input type='text' name='name'></label><br>");
response.getWriter().append("<label>Surname: <input type='text' name='surname'></label><br>");
response.getWriter().append("<label>Email: <input type='email' name='email'></label><br>");
response.getWriter().append("<label>Enter the sum " + firstRand + " + " + secRand + "<input type='number' name='captchaInput'></label><br>");
response.getWriter().append("<input type='submit' value='Submit'>");
response.getWriter().append("</form>");
}
}
request.getParameter("captchaInput")is not empty before parsing it