views:

542

answers:

1

My simple project structure is shown in this link. I am using Eclipse and Tomcat 6.

There is login.jsp which submits its form to login_servlet. The login_servlet sets a session variable and then redirects to home.jsp. The home.jsp file has links to the 4 JSP files under a directory called /sam. In web.xml I have given the url-pattern as /sam/* for the LogFiler filter.

The LogFilter just reads the session variable and does doChain(request,resposne) if valid, else it redirects to /login.jsp.

RequestDispatcher rd = request.getRequestDispatcher("/login.jsp");
rd.forward(request,response);

Basically I don't want anyone to access files inside /sam directory directly. Now let's say, I try to directly access a file inside /sam directory, the filter kicks in and the redirection to login.jsp works and even the broswers contents are that of login.jsp, but the url in the browser doesn't change. When I enter details and press submit, instead of sending the data to login_servlet, it sends it to sam/login_servlet and then tomcat tells me there is no such servlet here! Obviously there isn't. My doubt is why is it sending it so sam/login_servlet instead of /login_servlet which is usually what it does when I start running the login.jsp on my own.

One more thing, is there a way I can apply the servlet to ONLY .jsp files inside /sam diectory? I tried giving the url-pattern like /sam/*.jsp, but Tomcat was refusing to accept that url-pattern.

A: 

You're actually forwarding the request, not redirecting it. You normally use RequestDispatcher#forward() to forward the request and response to resources which aren't directly accessible by URL and thus you want to hide their location from the browser address bar. You normally use HttpServletResponse#sendRedirect() to redirect the response to a new request, which thus get reflected in the browser's address bar.

Replace the forward in the doFilter() as follows:

HttpServletResponse httpResponse = (HttpServletResponse) response;
httpResponse.sendRedirect("/login.jsp");

As to your second question, unfortunately that's not possible. You'll either provide a more specific and compatible url-pattern like /sam/pages/* or to let the servlet check the request URL.

BalusC
wow thank you very much!it worked. i just changed it a little...httpResponse.sendRedirect("/<project-name>/login.jsp");thanks!
gnomeguru