• Post Reply Bookmark Topic Watch Topic
  • New Topic

path to servlet  RSS feed

 
liliya woland
Ranch Hand
Posts: 134
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have a servlet SendMail working fine. I have it on Tomcat in WEB-INF/classes and the only way I can call it in my jsp pages (located also on Tomcat)is by using full path "http://myserver:8080/myJSPfolder/SendMail".
I know this is not a java related issue, but could someone please tell me what why can't I use a relative path?
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 65833
134
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Becasue the servlet's path has nothing to do with the file system. You can use a relative path, but it must be relative to the server, not the current URL.

E.g.

/contextPath/whatever/thisorthat.jsp
 
liliya woland
Ranch Hand
Posts: 134
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So, why then I can refer to a jsp page using "/myjspsInWebapps/somefolder/file.jsp", but can't refer to a servlet in a file.jsp by "/servlet/MyServlet"?
I'd like to be able to use a path "/servlet/MyServlet" and not
"http://myserver:8080/myjspsInWebapps/MyServlet".
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 65833
134
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can refer to any resource, be it a JSP or a servlet, using server-relative addressing as shown.

The exception is when doing an internal reference, such as a forward, you would need to drop the context path (making it a context-relative reference).
 
Ben Souther
Sheriff
Posts: 13411
Firefox Browser Redhat VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The physical location of a servlet class file has nothing to do with any of the URL mappings.
The container automatically generates mappings for JSP pages to make them appear to behave more like static HTML pages.

Further, webapps can be deployed as either exploded directory systems or from a war file directly. If you're container is deploying from an exploded file system, you can use java.lang.String)" target="_blank" rel="nofollow">ServletContext.getRealPath(path to resource relative to context root) to find the file system path to your resource. If you are deploying from a packed war file, you can't even do that.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!