1

I need to know if any way is there to call spring controller from web.xml from following location:

<error-page>
    <exception-type>org.demo.exceptions.InvalidSessionException</exception-type>
    <location>/login.jsp</location>
</error-page>

I have created a custom exception class named as InvaliSessionException, typically we call some jsp page from <location> tag, but what I need to do is to call login controller such as auth/login i.e. <location>/auth/login</location>.

is there any way to achieve this ?

InvaliSessionException.java

public class InvalidSessionException extends NullPointerException 
{
    private static final long serialVersionUID = 1L;

public String getMessage()
{
    return "No user logged in.";
}

public int getExceptionType()
{
    return -1;
}
}
2
  • From that error jsp , can you redirect to the URL mapped to the Login Controller ? Commented Apr 19, 2013 at 9:10
  • @Noob Even, if we can call some jsp under WebContent directory, that will also do, because my custom exception is thrown properly from controller but it is not being captured by web.xml as above mentioned. I have one jsp in which I am redirecting to my login controller using meta tag as below: <meta http-equiv="REFRESH" content="0;url=${applicationScope.contextPath}/auth/login"></HEAD> Commented Apr 19, 2013 at 9:11

1 Answer 1

1

The preferred way to do this would be to add a "Global Exception Handler" within in the Spring context, instead of trying to re-enter the Spring context from the web.xml.

You can achieve this by configuring a org.springframework.web.servlet.handler.SimpleMappingExceptionResolver bean in your context. Set the exceptionMappings property with list of exceptions that this resolver should act on, and for each exception, mention the view that should be shown to the user.

This keeps the design clean and maintainable.

Sign up to request clarification or add additional context in comments.

1 Comment

thanks, seems like good approach to handle exceptions via context.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.