Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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" /> 

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?

share|improve this question
I can't decide if this belongs here or StackOverflow. Considering you're talking about proper logic flow, I'm leaning to the fact that it does in fact belong here. However, you could feel free to remove the Spring/Java specific comments, as that will likely cause this question to be deemed a poor fit for this site. – Craige Feb 13 '13 at 19:24
Change it to what? error, logic flow? – Sotirios Delimanolis Feb 13 '13 at 19:27
Yes. Try to rephrase the question to be about HTTP form submit success/error logic flow. Talking about libraries and specific code here is frowned upon, and may cause this question to be moved to StackOverflow. I think this is a valid question for this site though. I suppose you could wait to see if anybody else agrees with me on this before you actually make the changes. – Craige Feb 13 '13 at 19:30
up vote 1 down vote accepted

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.

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.