0

In the past we've used a SSO solution for authentication. A servlet filter is checking for login etc. For cost reduction we want to replace it with a simple authentication against our database.

The plan here is to use the old login/password change jsp's (which were located on the sso server) and call it directly in our application in that way (if the filter is detecting a not logged in user):

req.getRequestDispatcher("/auth/login.jsp").include(req, response);

The page gets displayed, but only the text. Images and Stylesheet are not beeing displayed. (Can not be found).

<img src="/auth/platform.jpg" alt="platform" class="left" />

I've tried several other ways like, but all of these versions, didn't work. I know, a relativ path isn't working!

<img src="auth/platform.jpg" />
<img src=".auth/platform.jpg" />
<img src="<%=request.getContextPath()%>/auth/platform.jpg" />

I've output the following, everything is empty.

ServletPath: <%request.getServletPath(); %> <br />
ServletContext: <%request.getServletContext();%> <br />
ContextPath: <%request.getContextPath(); %> <br />
LocalAddr: <%request.getLocalAddr(); %> <br />
PathInfo: <%request.getPathInfo(); %> <br />
RemoteAddr: <%request.getRemoteAddr(); %> <br />
RemoteHost: <%request.getRemoteHost(); %> <br />

In the original projects (projects where login page was located) the jsp only look like:

<img src="images/platform.jpg" />

And it works, also with relative referenced ...

The stylesheet could also not be found by referencing it in this way (or any other ways like above):

<link href="/auth/style.css" rel="stylesheet" type="text/css" />

But if I am using this way, it works!

<style>
    <%@ include file="/auth/style.css"%>
</style> 

This is my structure:

enter image description here

Do I need anything else in my web.xml e.g.?

I know, in nowadays JSF will be used, but this is an old project and should amended as few as possible.

This is the http error message:

ERROR] 500 - GET /auth/platform.jpg (127.0.0.1) 2720 bytes
   Request headers
      Host: localhost:8888
      User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64; rv:26.0) Gecko/20100101 Firefox/26.0
      Accept: image/png,image/*;q=0.8,*/*;q=0.5
      Accept-Language: en-gb,en;q=0.5
      Accept-Encoding: gzip, deflate
      Referer: http://localhost:8888/Application.html?gwt.codesvr=127.0.0.1:9997&groupId=xxx
      Cookie: JSESSIONID=b56c92nn81g51d28ys2l65dg2
      Connection: keep-alive
   Response headers
      Accept-Ranges: bytes
      Content-Type: text/html
      Content-Length: 2720
      Last-Modified: Thu, 30 Apr 2015 11:43:38 GMT
NiceGuy
  • 133
  • 1
  • 14

3 Answers3

0

The paths to the image and CSS in the JSP should be relative to the URL of your servlet, not the JSP file itself.

James
  • 195
  • 3
  • 11
  • I am a bit confused. What is the correct URL relative to my servlet? – NiceGuy May 13 '15 at 12:26
  • @NiceGuy Can you give your servlet URL? – James May 13 '15 at 12:39
  • This one? http://localhost:8888/Application.html?gwt.codesvr=127.0.0.1:9997&... – NiceGuy May 13 '15 at 12:49
  • The one that is defined in `web.xml` to load the servlet whose class has this code: `req.getRequestDispatcher("/auth/login.jsp").include(req, response);` – James May 13 '15 at 13:02
  • I think it's a bit more complicated here, and that is what my confusion caused. In the web.xml only a Servlet for the RequestFactory is configured. (com.google.web.bindery.requestfactory.server.RequestFactoryServlet). The rest is configured via guice. – NiceGuy May 13 '15 at 13:09
  • Then maybe you need to configure access to the image and CSS too. If you can't access them in a browser yourself then the JSP won't be able to either. And once you can, you'll have found your paths. – James May 13 '15 at 13:29
  • Hm ok. And do you know, why is it possible to load the css? One way works, on not (mentioned in the question) – NiceGuy May 13 '15 at 14:03
  • `<%@ include file="/auth/style.css"%>` this way is using JSP server side code which has access to whole application so can find that file from the root of the application. ` – James May 13 '15 at 14:37
0

You can set the HTML tag to the context root of your application (e.g. http://locahost:8080/myapp) and all relative URLs in your document will be resolved relative to this:

http://www.w3schools.com/tags/tag_base.asp

So in your JSP you can do the following

<%
    String path = request.getContextPath();
    String basePath = request.getScheme()
        + "://"
        + request.getServerName()
        + ":"
        + (request.getServerPort() != 80
            ? request.getServerPort()
            : "") + path + "/";
%>

    <head>
        <base href="<%=basePath%>" />

        <!-- relative link to a folder 'css' in the webapp root -->
        <link href="css/my-css.css" rel="stylesheet" media="screen" />

    </head>
</html>

You can probably avoid the scriptlet by using or adapting the approach here

how to get the base url from jsp request object?

and if you are not using a templating library then you can create a tag file to avoid repeating this in all pages.

Community
  • 1
  • 1
Alan Hay
  • 22,665
  • 4
  • 56
  • 110
0

After searching for hours, I've found the cause for "not displaying images and loading stylesheets".

With the following HTML code, I've get a HTTP 200 (straight forward ...)

<img src="images/xxx.jpg" />

The cause, that the images aren't displayed is an authentication (servlet) filter, which is checking for an existing session (user logged in etc.). As this is a login page, there is no valid session and ALL other requests (loading stylesheets and images) get blocked.

My Filter look like:

Guice.createInjector(..., new ServletModule() {

   @Override
   protected void configureServlets() {
      filter("/*").through(MyFilter.class);
   }
}

The simplest way of allowing images (jpgs) is to add the following to my doFilter():

if (httpServletRequest.getRequestURI().endsWith(".jpg")) {
    chain.doFilter(request, response);
}

The problem was to find out, that is was the filter that causes the issue ...

Thanks guys.

NiceGuy
  • 133
  • 1
  • 14