Manage the insertion of null values in a form of a Servlet
I have a Servlet that gives me this error:HTTP Status 500 – Internal Server Error
Type Exception Report
Message empty String
Description The server encountered an unexpected condition that prevented it from fulfilling the request.
Exception
java.lang.NumberFormatException: empty String
java.base/jdk.internal.math.FloatingDecimal.readJavaFormatString(FloatingDecimal.java:1842)
java.base/jdk.internal.math.FloatingDecimal.parseDouble(FloatingDecimal.java:110)
java.base/java.lang.Double.parseDouble(Double.java:543)
web1.GestioneCoordinate.doPost(GestioneCoordinate.java:28)
javax.servlet.http.HttpServlet.service(HttpServlet.java:660)
javax.servlet.http.HttpServlet.service(HttpServlet.java:741)
org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:53)
Note The full stack trace of the root cause is available in the server logs.
Apache Tomcat/9.0.10
The problem occurs when I leave an empty field of an input tag inside a form and press enter.
Why if I write this I will not solve?public class GestioneCoordinate extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// Verifico l’esistenza di un utente autenticato.
HttpSession session = request.getSession();
if (session.getAttribute("UserID")!=null){
String UserID = request.getParameter("UserID");
Double Latitudine = null;
try {
Latitudine = Double.parseDouble(request.getParameter("Latitudine"));
}catch (Exception e){
response.sendRedirect("route.jsp");
}
route.jsp is obviously the page where the form is present. In practice I would like the Servlet to verify the existence of an acceptable data in the input tag and if not, send the user back to the calling page from which the request was sent.
I can leave the tag empty or write a comma number like 23,567 instead of 23.567 but in each of the circumstances this response.sendRedirect ("route.jsp") does not start and does not return the browser to route.jsp.
Why?
Please sign in to leave a comment.
https://stackoverflow.com/ is the proper place for such questions since it's not IDE specific.