0

I'm trying to create a basic java web app using servlet 3.0. Now, my application needs to run through a basic setup page/form so it knows where to store config files etc. What would be a good way to implement this?

I was thinking of a filter, but since i cant do a redirect, that seems like the wrong way.

Suggestions?

Trj
  • 657
  • 9
  • 21
  • Did you mean a wizard? Have you seen how a WAR is deployed in Websphere? You have to go through many pages. – Paul Vargas Apr 15 '13 at 21:21
  • Well, it's not really a wizard. Just a simple form with a couple of field inputs. – Trj Apr 15 '13 at 21:24
  • You can do a redirect from inside a filter. No problem with that. `response.sendRedirect(...)`, just like in a servlet. – JB Nizet Apr 15 '13 at 21:24

1 Answers1

0

Seems to me that a Filter would be a great approach to what you're trying to accomplish:

@WebFilter
public class ConfigFilter implements Filter {
    @Inject
    private SessionBean session;

    @Override
    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
        String requestURI = ((HttpServletRequest) request).getRequestURI();
        if(!session.hasConfig() && !requestURI.startsWith("/configWizard.xhtml")) {
            ((HttpServletResponse) response).sendRedirect("/configWizard.xhtml");
        }
        else {
            chain.doFilter(request, response);
        }
    }

    @Override
    public void destroy() {
    }

    @Override
    public void init(FilterConfig filterConfig) throws ServletException {
    }
}
Menno
  • 12,175
  • 14
  • 56
  • 88
  • Yes, that will work, but i will also need to check that the request does not contain "configWizard" or else it will go in a loop? – Trj Apr 15 '13 at 21:35
  • If the filter is also mapped to the URL of the config wizard, then yes, you should do this check. – JB Nizet Apr 15 '13 at 21:36