This week's book giveaway is in the JavaScript forum.
We're giving away four copies of Cross-Platform Desktop Applications: Using Node, Electron, and NW.js and have Paul Jensen on-line!
See this thread for details.
Win a copy of Cross-Platform Desktop Applications: Using Node, Electron, and NW.js this week in the JavaScript forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

ROOT mapping causes static files to not be served  RSS feed

 
Mark Frasertoo
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
When I map my servlet to the ROOT of the site, the javascript, CSS and image files are not served. The conversation between the server and browser shows the files are being sent, but they are not rendered in the browser. This happens in both Firefox and Chrome.

If I change the mapping to anything other than the root, such as /x/, everything works as it should. Any suggestions?

Here's my web.xml file:

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
version="2.5">

<servlet>
<servlet-name>Homepage</servlet-name>
<servlet-class>com.efgi.home.HomepageServlet</servlet-class>
<load-on-startup>1</load-on-startup>
<display-name>Homepage</display-name>
<description>The EFG Homepage Servlet</description>
<init-param>
<param-name>jspRoot</param-name>
<param-value>/WEB-INF/jsp/home/</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>Homepage</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>

</web-app>
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 66142
141
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yeah, don't do that. Use another mapping for your servlet.
 
Mark Frasertoo
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Could I use a jsp:include of the output from the servlet to add the content to the homepage?
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 66142
141
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm not sure what that has to do with the issue. If you are asking if you can use a JSP include in place of resource link like script and image tags, then, no.

Why are you trying to map "/" to your servlet? That is never a good idea.
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 66142
141
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you are trying to capture a request that a user might make to the root url, then that's what a welcome-file declaration is for. You don't need to futz with the mappings for that.
 
Tim Holloway
Bartender
Posts: 18662
71
Android Eclipse IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If the files are being sent, then that, by definition means that they were served. Whether they were served in the form that the client needs them to be in is another matter.

The way you're talking makes it sound like you think you can just plop files down in your server's webapps directory (TOMCAT_HOME/webapps, for example) and do so anywhere you like.

J2EE doesn't work that way. You have to compose a WAR to contain your webapp and , and the WAR must be compliant with the J2EE specs. In Tomcat's case. TOMCAT_HOME/webapps/ROOT is the exploded (unzipped) root-context webapp. The apps in Tomcat's webapps directory have default context paths made from their WAR names, but since "/" isn't a valid filename, the name "ROOT" substitutes for it.

It's valid - and not uncommon - in Tomcat to replace the ROOT webapp that comes with Tomcat with some other webapp, but it still has to conform to WAR standards.

The above is also true in general for other appserver than Tomcat, but I'm willing to bet based on the terms you used that you're using Tomcat. If not, well, they're still true anyway.
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 66142
141
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Tim brings up a good point -- are you talking about the ROOT web context? Or about the "/" mapping within your web context? Not the same thing. (Though, of course, both related to what URLs you are trying to use for the web app.)
 
Mark Frasertoo
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm trying to include data from a database on the homepage of the website, without using scriptlets. I have other servlets in the site using the front controller pattern and want to use the same pattern on the homepage. A CMS has no problem with dynamic info on a homepage and I'm wondering what it takes to do that using JSP.
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 66142
141
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm still not grokking what all that has to do with mapping the "/"

Unless, again, if you are wanting to define that happens when the user doesn't include a path, you'd use the welcome-file declaration in the deployment descriptor to channel that request to a path of your choice -- your front controller, for example.
 
Mark Frasertoo
Greenhorn
Posts: 16
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for the help!

For those looking for what works, here's what I used:

<servlet-mapping>
<servlet-name>WelcomeServlet</servlet-name>
<url-pattern>/welcome</url-pattern>
</servlet-mapping>

<welcome-file-list>
<welcome-file>welcome</welcome-file>
</welcome-file-list>
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 66142
141
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for posting back with your solution.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!