javaservletspicocontainer

redirect to custom not found jsp page


I've got HttpServlet. It redirects user to different jsp pages, depends on action user want to do. For example http://localhost:8080/collections/index.do redirects to index.jsp. Different action I keep in picocontainer like this

<component-instance key="index">
    <com.epam.collections.web.IndexAction/>
</component-instance>

When user write previous url in browser - 1) I get action name - index

String name = getActionName(req);

2) Get action from picocontainer

Action action = (Action) pico.getComponentInstance(name);

3) Perform action - return string which represents jsp page name

String view = action.exec(req, resp);

where exec method is

public String exec(HttpServletRequest req, HttpServletResponse resp) {
    return "index";
}

4) Forward user to index.jsp page

getServletContext().getRequestDispatcher(
            "/WEB-INFO/pages/" + view + ".jsp").forward(req, resp);

I want to forward user to page notfound.jsp when there is not action in picocontainer. For example some blabla.do should return notfound.jsp page. But when I do like this

if (action == null) {
        getServletContext().getRequestDispatcher(
                "/WEB-INF/jsp/notfound.jsp").forward(req, resp);
        return;
    }

because getComponentInstance returns null when action does not exist in xml file - I've got error 500

Also I want to redirect to this page when I write sth without .do at all. For example ddd.dd, plain and etc. But I've got 404 error.


Solution

  • 1) to handle 404 errors you need to setup 404 handler in web.xml 2) you got 500 err, cause probably something is wrong about your jsp page, you need to check jsp code carefully 3) the way you use pico is an antipattern, check pico docs