This can apply to many error handling scenarios, but I'm curious about how to do it in a login scenario.Given this Spring controller method:@RequestMapping(value = /account/login, method = RequestMethod.POST)public String login(@RequestParam(username) String aUsername,        @RequestParam(password) String aPassword,        Model model) {        try {            accountService.login(aUsername, aPassword);         } catch (LoginException e) {            // log e            // add an error to request scope            model.addAttribute(error, e.getMessage);            return login; // return to login page        }        // success, show profile        return redirect:profile;    }And a login.html form (I'm using thymeleaf, but just follow logic, similar to jsp):<!-- This is saying, if ${error} is set, use it instead of the content of p, `No error` --><p th:if=${error} th:text=${error}>No error</p><form method=post action=# th:action=@{/account/login}>    Username: <input name=username type=text />    Password: <input name=password type=text />    <input name=login type=submit value=Login /> </form>Is this how error handling is usually done in java web applications? Anywhere an error can happen, you branch out with different ifs? Is there a cleaner, or more generic, way to do this?
Handling error message for a Spring MVC login form
java;mvc;spring;jsp
Error Handling and Displaying to users can be unique depending on your UI design and on how you want to view it by the user. Displaying it on mobile can be different from displaying on a desktop.What I know is it is important as well to categorize these errors. There are validation errors, security access errors, critical error(i.e database exceptions, any unexpected exceptions). And for each categories, you could design different approach such as for critical error, you could use a dedicated page that will catch all critical errors. For validation errors, you must display it on the same page where the user enters invalid data and make sure that your message is very obvious that they will recognize immediately which fields/inputs to correct, etc.Displaying of errors must always be user friendly, direct and appropriate.