tags:

views:

71

answers:

1

I have made small web-app in jsp with a start page with login and some restricted pages with logout, -everything controlled with filters and servlets. I started making it because I wanted to learn how to use filters and servlets, and think I have succeded in that very well.
Everything works fine when I just ensure everything is happening in the 'root' url-pattern, but the problem is I want the restricted pages to be in its own url-pattern directory...

On my restricted pages I have included (<@ include...>) a Logout page which contains this form:

<form method=POST action=LC>
<table align="right">

 <tr>
  <td>Navn:</td>
  <td><b><%=login.getName() %></b></td>
 </tr>
 <tr>
  <td>Aktør:</td>
  <td><b><%= login.getAktoer() %></b></td>
 </tr>
 <tr>
  <td><input type="submit" value="Log ud"></td>
 </tr>
 <tr>
  <td></td>
  <td><b><%= login.getMeddelelse() %></b></td>
 </tr>
</table>
</form>

The submit on this page will send the user to a control servlet that just clears a LoginBean, sets a boolean isLoggedin value to false on the same bean and finally 'sendRedirect' the user to my start page. This works well when all pages are in the 'root' url-pattern directory.

One of my restricted pages is Yellow.jsp. As it is now, it just have following servlet mapping:

<servlet>
    <servlet-name>ptYellow</servlet-name>
    <jsp-file>/Yellow.jsp</jsp-file>
  </servlet>
  <servlet-mapping>
    <servlet-name>ptYellow</servlet-name>
    <url-pattern>/Yellow.jsp</url-pattern>
  </servlet-mapping>

what I want it to be is this:

<servlet-mapping>
    <servlet-name>ptYellow</servlet-name>
    <url-pattern>/RestrictedPages/YellowZone/Yellow.jsp</url-pattern>
  </servlet-mapping>

But when I try to map it like this and I attempt a logout from the Yellow.jsp page, it just spits out a 404 error because it is attempting to access my logout control servlet in the '/RestrictedPages/YellowZone/' directory.

it tries to access:
http://localhost:8080/myapp/RestrictedPages/YellowZone/LC
(LC is my Logout Control servlet)
when it should just go for:
http://localhost:8080/myapp/LC

I want to include my logout on many different restricted pages in different url-patterns, so it should not be mapped to the same url-pattern (that doesn't seem to solve the problem anyways).
And I definitely don't want to hard code the logout form on all restricted pages, when I know it should be possible to just 'include' it...

I'm guessing I have to write something special in my logout forms action attribute, but can not figure out what to write. I have tried stuff like:
action=*/LC
and
action=/../LC

Stripped from most irrelevant code, my xml file looks like this:

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
xmlns="http://java.sun.com/xml/ns/javaee" 
xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" 
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 
http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" 
version="3.0">
  <welcome-file-list>
    <welcome-file>StartSide.jsp</welcome-file>
  </welcome-file-list>
  <error-page>
    <error-code>404</error-code>
    <location>/404.jsp</location>
  </error-page>
  <filter>
    <filter-name>LoginFilter</filter-name>
    <filter-class>control.LoginFilter</filter-class>
  </filter>
  <filter-mapping>
    <filter-name>LoginFilter</filter-name>
    <servlet-name>AC</servlet-name>
  </filter-mapping>
  <filter>
    <filter-name>YellowFilter</filter-name>
    <filter-class>control.YellowFilter</filter-class>
  </filter>
  <filter-mapping>
    <filter-name>YellowFilter</filter-name>
    <url-pattern>/RestrictedPages/YellowZone/*</url-pattern>
  </filter-mapping>

  <servlet>
    <servlet-name>ptYellow</servlet-name>
    <jsp-file>/Yellow.jsp</jsp-file>
  </servlet>
  <servlet-mapping>
    <servlet-name>ptYellow</servlet-name>
    <url-pattern>/RestrictedPages/YellowZone/Yellow.jsp</url-pattern>
  </servlet-mapping>

  <servlet>
    <servlet-name>start</servlet-name>
    <jsp-file>/StartSide.jsp</jsp-file>
    <load-on-startup>1</load-on-startup>
  </servlet>
  <servlet-mapping>
    <servlet-name>start</servlet-name>
    <url-pattern>/start</url-pattern>
  </servlet-mapping>
  <servlet>
    <servlet-name>AC</servlet-name>
    <servlet-class>control.ActorControl</servlet-class>
  </servlet>
  <servlet-mapping>
    <servlet-name>AC</servlet-name>
    <url-pattern>/AC</url-pattern>
  </servlet-mapping>
  <servlet>
    <servlet-name>LC</servlet-name>
    <servlet-class>control.LogoutControl</servlet-class>
  </servlet>
  <servlet-mapping>
    <servlet-name>LC</servlet-name>
    <url-pattern>/LC</url-pattern>
  </servlet-mapping>

</web-app>

Would be awesome if someone can point out what I'm doing wrong... I can see a lot of potential in using filters on url-patterns, but it is no use to me when I can't do simple stuff like moving back in a url-pattern....
I am using Eclipse Helios and apache-tomcat 7.0, if that has any relevance...
(I have used hours trying to search for an answer, but I don't think I know exactly what to search for)

EDIT: typos and clarification

EDIT2: I have tried to do some mapping on my Logout.jsp page (which is the log out page that I 'include' on my restricted pages), but that doesn't seem to solve the problem either...

EDIT3:I have poked some more around with this problem tonight.
I tried to add this block of code to my YellowFilter.java (and added the name=Logout to the submit button in Logout.jsp):

try{
        if(httpReq.getParameter("logout").equals("Log ud"))
            httpResp.sendRedirect("LC");
        } catch(NullPointerException e)
        {
        }

it didn't work either, it still just tries to access
http://localhost:8080/myapp/RestrictedPages/YellowZone/LC
I also tried removing the action parameter from my logout form, so it would just POST to the same page. That didn't help either...

Furthermore I have tried to edit the servlet-mapping for my Logout control servlet in the xml file:

<servlet-mapping>
    <servlet-name>LC</servlet-name>
    <url-pattern>/*/*/LC</url-pattern>
  </servlet-mapping>

didn't work, then I tried:

<servlet-mapping>
    <servlet-name>LC</servlet-name>
    <url-pattern>/*/LC</url-pattern>
  </servlet-mapping>

didn't work either, so then I tried:

<servlet-mapping>
    <servlet-name>LC</servlet-name>
    <url-pattern>/*LC</url-pattern>
  </servlet-mapping>

and that didn't work either...


/sigh, very annoying problem, and can't let it go...

A: 

I found a very simple solution to the problem. I just change the form method in Logout.jsp to method=GET instead of POST, and then write action=/'myappname'/'name-of-servlet':

<form method=GET action=/Prototype/LC>
<table align="right">

    <tr>
        <td>Navn:</td>
        <td><b><%=login.getName() %></b></td>
    </tr>
    <tr>
        <td>Aktør:</td>
        <td><b><%= login.getAktoer() %></b></td>
    </tr>
    <tr>
        <td><input type="submit" value="Log ud"></td>
    </tr>
    <tr>
        <td></td>
        <td><b><%= login.getMeddelelse() %></b></td>
    </tr>
</table>
</form>

The solution is pretty obvious to me now, and I fell stupid about overlooking this, -so I really hope someone else is having the same problem xD...
Will change the title of my question to 'Go back in url-pattern (invoking servlet from a jsp page)'

Mizh