0

Internet Explorer doesn't support HTML <base> tag and even other browsers do, there are some problems when redirect takes place inservletsto some.jsppages for examplerequest dispatching.`

It's feasible to add ${pageContext.request.contextPath} with each URL nor request.getServletPath()

JSP relative links for CSS and images with servlets forwarding may change things a lot. This link : Browser can't access/find relative resources like CSS, images and links when calling a Servlet which forwards to a JSP

Is there a better approach with JSP / servlets or it's just an IE issue?
Link : HTML <base> TAG and local folder path with Internet Explorer

And if it is an IE issue:
1. how to fix the IE issue as the above post is unable to give a valid answer?
2. how to solve it with JSP / servlets?


My website is now showing CSS and images.
E.g. HTML output is:

<base href="http://localhost:8080/Alpinema/" /> is not working for 
<link media="all" rel="stylesheet" type="text/css" href="css/all.css">

It works in other browsers like Firefox and Chrome.

My JSP code portion:

<head>
    <base href="${fn:substring(url, 0, fn:length(url) - fn:length(uri))}${req.contextPath}/" />
    <meta charset="utf-8">
    <title>Alpinema.com</title>
    <link media="all" rel="stylesheet" type="text/css" href="css/all.css">
   /css?family=Merriweather|PT+Sans:700|Nobile:400italic' rel='stylesheet' type='text/css'>
</head>
Community
  • 1
  • 1
kevin
  • 328
  • 4
  • 15
  • 33

1 Answers1

4

Use <c:url> tag from JSTL to reference CSS/JavaScript resources inside my JSP files. By doing so you can be sure that the CSS/JavaScript resources are referenced always relative to the application context (context path).


Example

index.jsp:

<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<!DOCTYPE html>
<html>
<head>
  <title>Some Title</title>
  <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
  <link type="text/css" rel="stylesheet" href="<c:url value="/css/main.css" />" />
  <script type="text/javascript" src="<c:url value="/js/utils.js" />"></script>
  <script type="text/javascript" src="<c:url value="/js/jquery-1.8.3.js" />"></script>
</head>
<body>
...
</body>
</html>

For even more solutions see my answer here:
Adding external resources (CSS/JavaScript/images etc) in JSP.

Community
  • 1
  • 1
informatik01
  • 16,038
  • 10
  • 74
  • 104
  • nice! its off now. give me some time to test it. and when using web templates or some site made by some other developer. its hard to change all links to c:url – kevin Dec 28 '12 at 01:04
  • @Masood I use JSTL tags, as I code all JSP files myself. No problems so far. For instance, in the example above, the "css" and "js" folder are both located in the root folder. And the path in the value attribute MUST start with forward slash ("/some_path") in order to be relative to the context path. Sorry if it doesn't suit your needs. – informatik01 Dec 28 '12 at 01:25
  • @Masood And of course the right JAR libraries also must be present in your WEB-INF/lib folder. See here for the details:http://stackoverflow.com/tags/jstl/info – informatik01 Dec 28 '12 at 01:37
  • 1
    it works great! Thanks but for template sites (thats my work) , I am looking for a proper solution. I want to rate this answer but my reputaion is < 15 – kevin Dec 28 '12 at 01:42
  • @Masood It's OK, no problem )) I upvoted your question to get rid of -1. Hope someone will help you. – informatik01 Dec 28 '12 at 01:49
  • Maybe this will help (from the Microsoft MSDN page related to Internet Explorer): "When used, the **base element must appear within the head of the document, before any elements that refer to an external source**." See "Remarks" and "Example" here (scroll down): http://msdn.microsoft.com/en-us/library/ms535191%28v=vs.85%29.aspx – informatik01 Dec 28 '12 at 02:33
  • added 3 ignore some urls in html output are /Alpinema/path/file and some are path/file . I added ${pageContext.request.contextPath}/ to each, so not consistent. and thats the only way I found as solution (not preferred nor for template sites( thats what I do)) – kevin Dec 28 '12 at 13:58