Developing a web project (Java EE + HTML + CSS). Everything works somehow, but there are jambs. Question on one of them. The user of the web application has the ability to register. He enters the site, enters the data and they are entered into the database (MySQL). But, in case of incorrect data entry, I want to display an error. How to make mandatory to fill every field? If you send an empty form, a 500 error will pop up, and this is terrible. Help me please. There is no time at all ... Form code:

<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %> <%@taglib prefix="ftm" uri="http://java.sun.com/jsp/jstl/fmt" %> <ftm:setLocale value="${sessionScope.locale}"/> <ftm:setBundle basename="messages" var="i18n"/> <center><h5><ftm:message bundle="${i18n}" key="register.namePage"/></h5></center> <div class="error">${errorMsg}</div> <br/> <form id="form_input" action="frontController?command=registration" method="post"> <ftm:setLocale value="${sessionScope.locale}"/> <ftm:setBundle basename="messages" var="i18n"/> <ftm:message bundle="${i18n}" key="register.name"/><br/> <input type="text" placeholder="name" name="name" id="name"/><br/> <ftm:message bundle="${i18n}" key="register.email"/><br/> <input type="text" placeholder="email" name="email" id="email"/><br/> <ftm:message bundle="${i18n}" key="register.password"/><br/> <input type="password" placeholder="password" name="password" id="password"/><br/> <ftm:message bundle="${i18n}" key="register.address"/><br/> <input type="text" placeholder="address" name="address" id="address"/><br/> <input type="submit" values=" <p><ftm:message bundle="${i18n}" key="register.submit"/>"></p> </form> 

Server code:

 public class RegistrationUserController implements Controller { private UserService userService = UserServiceIplm.getInstance(); @Override public void execute(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { String name = req.getParameter("name"); String email = req.getParameter("email"); String password = req.getParameter("password"); String address = req.getParameter("address"); if (email == null || password == null || name == null || address == null) { resp.setHeader("errorMsg", "Invalid data"); RequestDispatcher dispatcher = req.getRequestDispatcher(MAIN_PAGE); dispatcher.forward(req, resp); return; } User user = new User(name, email, password, address); userService.save(user); if (user != null) { String contextPath = req.getContextPath(); resp.sendRedirect(contextPath + "/frontController?command=email"); return; } else { resp.setHeader("errorMsg", "Invalid"); req.setAttribute("errorMsg", "Invalid"); RequestDispatcher dispatcher = req.getRequestDispatcher(MAIN_PAGE); dispatcher.forward(req, resp); return; } } } 
  • HTML 5 has the attribute required - Oleksiy Morenets
  • <Epam> courses? ))) - Oleksiy Morenets
  • thanks) yes, only from HTP) - Andrey Yuraga
  • What is pvt ??? - Oleksiy Morenets

1 answer 1

Solved the problem by using the "required" tag attribute on the client side)

  • 2
    Welcome to Stack Overflow in Russian! Please try to leave a little more detailed answers. You can add an answer by clicking edit - aleksandr barakin