File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Servlets and the fly likes servlet mapping issue Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Servlets
Bookmark "servlet mapping issue" Watch "servlet mapping issue" New topic
Author

servlet mapping issue

Leijun Zheng
Greenhorn

Joined: Feb 22, 2010
Posts: 2
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

Joined: Jan 10, 2002
Posts: 60059
    
  65

You didn't include the context path.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Mark E Hansen
Ranch Hand

Joined: Apr 01, 2009
Posts: 642
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

Joined: Oct 04, 2009
Posts: 143
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

Joined: Feb 22, 2010
Posts: 2
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

Joined: Apr 01, 2009
Posts: 642
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

    Joined: Jan 10, 2002
    Posts: 60059
        
      65

    <c:out> will do no such thing. Did you mean <c:url>?
    Mark E Hansen
    Ranch Hand

    Joined: Apr 01, 2009
    Posts: 642
    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

    Joined: Oct 04, 2009
    Posts: 143
    Thanks , Mark Hansen things are clear now
     
    I agree. Here's the link: http://aspose.com/file-tools
     
    subject: servlet mapping issue
     
    Similar Threads
    war on servlets 4
    Using Vaadin and Struts 1 together
    Http status 500 Error instantiating servlet class peigov.businessportal.servlets.LoginServlet
    "HTTP Status 404" error when starting tomcat
    Error JSF 2.1: java.io.FileNotFoundException:*/*.xhtml Not Found in ExternalContext