• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

servlet mapping issue

 
Leijun Zheng
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am working on a web app that is deployed on Tomcat 5.0, and I have the following entry in web.xml

<servlet>
<servlet-name>ViewSVG</servlet-name>
<display-name>ViewSVG</display-name>
<servlet-class>com.company.ViewSVG</servlet-class>
</servlet>


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


and in my jsp page, I have

<EMBED NAME="inter1_01" WIDTH="<%=adSquareWidth%>" HEIGHT="<%=adSquareHeight%>" SRC="/servlet/ViewSVG?adId=<%=id%>" TYPE="image/svg+xml" PLUGINSPAGE="http://www.adobe.com/svg/viewer/install/"/>


Now the problem is that the app doesn't goto that servlet class, as if it doesn't understand the servlet mapping at all.
If I put SRC="http://localhost:8080/appName/servlet/ViewSVG?adId=<%=id%>" , it works fine.

did I miss something about url path?
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64959
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You didn't include the context path.
 
Mark E Hansen
Ranch Hand
Posts: 650
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, you missed something And welcome to Java Ranch!

The path you have specified in your JSP page will be considered relative to your web server container (your web server), not your web application. The URL to your servlet is actually /appName/servlet/ViewSVG - as you noted at the end of your post.

One nice way to deal with this is to use JSTL to output the tag. With JSTL, you can output the app-relative URI (the /servlet/ViewSVG part) and JSTL will add the context path for your application (the /appName part) automatically.

You can get the same effect using JSP tags as well, but this is a little harder and considered bad form any more. You really should be using JSTL for this kind of thing.

If you need help getting up and running with JSTL, just have a look at the FAQs on this site, and ask if you need anything more. It's really pretty easy to get up and running.

Best Regards,
 
Sony Agrawal
Ranch Hand
Posts: 143
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Mark E Hansen wrote:
The path you have specified in your JSP page will be considered relative to your web server container (your web server), not your web application.


I just wanted to confirm one thing, let me know if iam wrong...
relative to your web server container - means the current dir is WEB-Apps/
and
relative to your web application - means the current dir is WEB-Apps/ProjectName/

please let me know...
 
Leijun Zheng
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for everyone's help, I figured out the problem.

I was re-working this existing app, and originally they had the context root as ROOT, which means http://servername/*.jsp to access the page, and that's why /servlet/ViewSVG works, now that I have added a context root for this app, I have to use /contextRoot/servlet/ViewSVG in order to access the same servlet.
 
Mark E Hansen
Ranch Hand
Posts: 650
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sony Agrawal wrote:
Mark E Hansen wrote:
The path you have specified in your JSP page will be considered relative to your web server container (your web server), not your web application.


I just wanted to confirm one thing, let me know if iam wrong...
relative to your web server container - means the current dir is WEB-Apps/
and
relative to your web application - means the current dir is WEB-Apps/ProjectName/

please let me know...


No. By web server, I meant the HTTP server, under which all the web application contexts go. Of course, the actual URL depends on what web server you're using, etc. As a simple example, consider the following web application URLs:

  • http://localhost:8080/myApp1/kings/hello.jsp
  • http://localhost:8080/myApp2/queens/hello.jsp

  • In this case, the web server is located at http://localhost:8080/.
    Also, /myApp1 and /myApp2 are the two web application contexts.

    The point I was making is that a URI specified in the JSP as /kings/hello.jsp would not be enough to locate the resource, as it is missing the web application context and would evaluate to http://localhost:8080/kings/hello.jsp

    However, if you use JSTL tags to output the URI (and the URI begins with a slash character), the tag will automatically add the web application context. For example:
    Which would result in a URI of /myApp1/kings/hello.jsp, which would be correct.

     
    Bear Bibeault
    Author and ninkuma
    Marshal
    Pie
    Posts: 64959
    86
    IntelliJ IDE Java jQuery Mac Mac OS X
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    <c:out> will do no such thing. Did you mean <c:url>?
     
    Mark E Hansen
    Ranch Hand
    Posts: 650
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Bear Bibeault wrote:<c:out> will do no such thing. Did you mean <c:url>?

    ... maybe :)

    Yes, sorry. It was a brain-fart class of typo. Of course, <c:url...> does this mapping.

    Sorry about the confusion.
     
    Sony Agrawal
    Ranch Hand
    Posts: 143
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Thanks , Mark Hansen things are clear now
     
    • Post Reply
    • Bookmark Topic Watch Topic
    • New Topic