Access servlets web application context URL from inside CSS/JS file - java

How can we access web application context url from inside the CSS/JS file on java web server?
We can map URL's (for background image for example) relatively with url('../img/bg.gif') etc. but this will not work with web application with mappings like:
/shop/
/shop/show/3/
/shop/payment/
because browser will search for this file relative to current "virtual" directory.
Also, we can't universally map image URL global like url('/images/bg.gif'), because we forces deployment in top-level directory like example.com/ (not example.com/myproject/).
How to avoid changing CSS/JS(ajax) URL's when changing application context URL?
It is possible to access aplication context in CSS file in easy way like accessing contextPath in default servlet wich serves those static files?

You could do something like this (assuming you're using Tomcat, if not, look up the doc to find the correct servlet name to add the mapping):
<servlet>
<servlet-name>jsp</servlet-name>
<servlet-class>org.apache.jasper.servlet.JspServlet</servlet-class>
<init-param>
<param-name>fork</param-name>
<param-value>false</param-value>
</init-param>
<init-param>
<param-name>xpoweredBy</param-name>
<param-value>false</param-value>
</init-param>
<load-on-startup>3</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>jsp</servlet-name>
<url-pattern>*.css</url-pattern>
</servlet-mapping>
Then you can add jsp el code in your css:
url('${pageContext.request.contextPath}/images/bg.gif')
This will make your CSS files serve MUCH slower, so take into account if you can do it with absolute pathing at the root. We've used this approach in an app that allowed skinning of the CSS, so that we could output custom color schemes.

Related

Tomcat is serving JNLP Files with the content type "text/html" instead of the JNLP content type

I've setup a Tomcat for a java web start application. Now I'm offering the .JNLP files as download on a static html page (The server is just for internal purposes). But when I click on a file instead of downloading it it opens up and shows the xml code in the browser.
With fiddler I found out that the file will be server with the content type text/htmlinstead of application/x-java-jnlp-file.
I've already configured the following stuff in my tomcat web.xml:
<servlet>
<servlet-name>default</servlet-name>
<servlet-class>org.apache.catalina.servlets.DefaultServlet</servlet-class>
<init-param>
<param-name>debug</param-name>
<param-value>0</param-value>
</init-param>
<init-param>
<param-name>listings</param-name>
<param-value>false</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<mime-mapping>
<extension>jnlp</extension>
<mime-type>application/x-java-jnlp-file</mime-type>
</mime-mapping>
Why is Tomcat serving the file with the wrong Content-type?
I've found a solution for my case.
I pasted the mime-mapping tags in my global web.xml of my tomcat. After restarting the server and clearing the browser cache it was working as expected.
What I've learned: There's no need for an extra servlet to download JNLP files.

Root "/" mades server load index.html before pass through the servlet

I have a Servlet that is mapped to the root directory "/":
<servlet>
<servlet-name>Main</servlet-name>
<servlet-class>com.motorola.triage.MainServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>Main</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
In this servet there is a couple of small things that are done there, like authentication and retrieve of Google Plus information. After that, I'm doing a forward to a JSP file called "index.jsp"
req.getRequestDispatcher("index.jsp").forward(req,resp);
When I'm accessing "localhost:8080/" the static file "index.jsp" is loaded without passing through the servlet. For architecture reasons I can not change the name of index.jsp. I would like to ask if is there any way to change this behavior of the server and make it look to the servlet before look the index.jsp file.
This is occurring specifically because you used the name index.jsp.
This has been covered elsewhere, such as here and here and here.

SpringMVC Map Servlet to Root without Removing Content Servlet

Currently, we have "root" (/) mapped to a static index.html page, but we want to upgrade to a jsp to have dynamic content. Trying to figure out how to do this. We have content that is mapped to the default content server (e.g. /css), so we don't want to change too much.
We tried:
Changing the .html to .jsp. This resulted in a blank page.
Changing the .html to .jsp and then moving the file into the WEB-INF directory. This resulted in a 404.
Trying to subclass the DefaultServlet class that content servlet is currently mapped to. This through a 500, with a class assertion error (it checked to see if it was the same class).
Adding another servlet to that url, but it overwrote the current one.
I've searched StackOverflow, but still haven't found an answer that works.
Thanks!
If I understand your question correctly, this is trivial using Spring MVC:
<mvc:default-servlet-handler/>
And in web.xml:
<servlet>
<servlet-name>spring-mvc</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<async-supported>true</async-supported>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:/META-INF/spring/your-applicationContext-web.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>spring-mvc</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
Is this what you have tried already?
Just set up a controller method mapped to / that returns a view name, which is your jsp file. And make sure your view resolver is set up correctly. Any of the spring mac tutorial hellos world programs out there will show how.

Tomcat not serving static files

I'm at the end of my rope on this one. I'm try to get a super simple webapp up and I can't seem to get tomcat to not 404 static files.
I'm using the gradle tomcat plugin with tomcat version 7.0.39
My html file is at hey-world/src/main/webapp/index.html
My web.xml looks like this:
<servlet>
<servlet-name>Resteasy</servlet-name>
<servlet-class>
org.jboss.resteasy.plugins.server.servlet.HttpServletDispatcher
</servlet-class>
<init-param>
<param-name>javax.ws.rs.Application</param-name>
<param-value>HeyWorldApplication</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>default</servlet-name>
<url-pattern>/static/*</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>Resteasy</servlet-name>
<url-pattern>/*</url-pattern>
</servlet-mapping>
So I thought this setup would map localhost:8080/hey-world/static/index.html to the file, but it 404's everytime. Is this a problem with some convention in the gradle tomcat plugin?
The URL-patterns used in web.xml/servlet-mapping is often a little simplistic. I think in your case, the /* pattern for Resteasy will work as a catch-all, so that no other mapping will really matter.
For debugging, I suggest you remove the Resteasy-servlet altogether, and see if you can serve static files from a custom URL with your mapping.
If that works, re-enable Resteasy, but on a different URL-pattern (eg. /rest/*).
If that works, well, then everything really works fine, it's just that the URL-mapping for /* blocks anything else from working.
The easiest solution would probably be to server static files as per default (no mapping), and serve rest-stuff from another URL.
Alternatively use two web apps. One with context root /static, one with context root /.

Tomcat 6.x web.xml default and custom servlet routing

I have two servlets defined in the web.xml file, namely the default2 and myservlet. The default2 servlet is used to map the static files like the javascript and css. The myservlet is used for getting dynamic content.
<servlet>
<servlet-name>default2</servlet-name>
<servlet-class>org.apache.catalina.servlets.DefaultServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet>
<servlet-name>myservlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>
classpath:my-servlet.xml
</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
The servlet mapping is defined as follows
<servlet-mapping>
<servlet-name>myservlet</servlet-name>
<url-pattern>/*</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>default2</servlet-name>
<url-pattern>/resources/*</url-pattern>
</servlet-mapping>
When i try to access any files under /resources, i get a 404. Any ideas why this config is not working or change this config to make it work.
Tomcat's default servlet before 6.0.30 actually serves a static resource identified by HttpServletRequest.getPathInfo(), so that /style.css will be returned when /resources/style.css is requested.
Tomcat's behavior has changed from version 6.0.30 onwards. So the original configuration from the question works in newer versions of Tomcat. See "50026: Add support for mapping the default servlet to URLs other than /. (timw)" in the changelog.
Jetty's default servlet uses a full path.
It should work fine. Are those files in real also located in the /resources folder?
Your web.xml looks correct (except I would change your <load-on-startup> constants).
Make sure that your /resources exists and is a publicly visible folder in your project path and not under /WEB-INF folder.
Try changing your url-pattern for myservlet to /, and optionally adding <mvc:default-servlet-handler /> (see here) to your Spring configuration.
Removed wrong portion of the answer as per #BalusC comment.
Set a break point in your servlet and perform a debug session. Look for the path that your servlet is picking up these files at. Make sure they match the location

Categories