0

I tried to map image and other URLs seperately but calling without slashes in ServletPath, it shows images.When ServletPath contains slashes it doesn't allow images to be shown.

JSP :

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

Servlet :

String servletPath = request.getServletPath();
request.getRequestDispatcher("/jspPage.jsp").forward(request, response);

Web.xml

<servlet-mapping>
    <servlet-name>default</servlet-name>
    <url-pattern>*.jpg</url-pattern>
</servlet-mapping>

<servlet>
    <servlet-name>ControllerServlet</servlet-name>
    <servlet-class>tut.controller.ControllerServlet</servlet-class>
</servlet>

<servlet-mapping>
    <servlet-name>ControllerServlet</servlet-name>
    <url-pattern>/</url-pattern>
</servlet-mapping>

Results :

http://localhost:8888/ServletFilter/url1     ---------------- image visible
http://localhost:8888/ServletFilter/url1/    ---------------- image not visible
http://localhost:8888/ServletFilter/url1/ex  ---------------- image not visible
http://localhost:8888/ServletFilter/url1/ex/ ---------------- image not visible
sunleo
  • 10,589
  • 35
  • 116
  • 196

2 Answers2

1

When a browser sees the following HTML

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

it sends a request to a URL that is built by taking the path in the src attribute and resolving it relative to the current URL. So if you previously sent a request to

http://localhost:8888/ServletFilter/url1   

(which rendered the HTML containing the img tag), then the URL for the image would be

http://localhost:8888/ServletFilter/images/tiger.png

Similarly, if you send your request to

http://localhost:8888/ServletFilter/url1/ex

the URL for the image will be

http://localhost:8888/ServletFilter/url1/images/tiger.png

When you prefix the src path with a /, the URL is constructed relative to the host name. So for

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

the request would be sent to

http://localhost:8888/images/tiger.png

This is not what you want but you can use it to your advantage. JSTL and EL both provide a way to build an absolute (relative to host) URL using the context path of your web application.

<c:url value="/images/tiger.png" var="path" />
<img src="${path}"/>

Or

<img src="${pageContext.request.contextPath}/images/tiger.png" />

This will be resolved to

/ServletFilter/tiger.png

assuming ServletFilter is the value of your web application's context path.

Here's some extra reading:

Community
  • 1
  • 1
Sotirios Delimanolis
  • 274,122
  • 60
  • 696
  • 724
0

The simplest solution would be to change a mapping for your servlet:

 <servlet-name>ControllerServlet</servlet-name>
 <url-pattern>/myservlet</url-pattern>

That would allow you to get your servlet out of business of static context handling.

It looks like what you're trying to do is to configure Tomcat to serve a static content. While in general this is not a good idea, because of performance impact, in a development environment you still might want to do that. If this is the case, here is a good blog that provides details. It will also allow getting your servlet out of business of static content handling.

http://th1rty7.blogspot.com/2009/05/tomcat-is-often-considered-to-be-too.html

Oleg Gryb
  • 5,122
  • 1
  • 28
  • 40
  • Have you tried it first? will it work for `http://localhost:8888/ServletFilter/url1/` – Braj May 26 '14 at 17:32
  • Tried what? Mapping a servlet to a unique path? I do it all the time. After this is done, your servlet will be available at: //myservlet location. Your static context will be available at //.../tiger.jpg location. Most importantly - your servlet will not need to be in a business of static context handler and it normally should not. – Oleg Gryb May 26 '14 at 17:38