1

I put index.jsp inside WEB-INF. Then I have a servlet which dispatch request to that file.

@WebServlet(name="Home", urlPatterns={"/"})
public class Home extends HttpServlet {

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        request.getRequestDispatcher("/WEB-INF/index.jsp").forward(request, response);
    }
}

I have a css folder which is outside the WEB-INF folder. It contains the css.css file.

The This is the content of index.jsp file:

<%@page contentType="text/html" pageEncoding="UTF-8"%>
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <link type="text/css" href="<c:url value="/css/css.css" />" rel="stylesheet">
        <title>JSP Page</title>
    </head>
    <body>
        <h1>Hello World!</h1>
    </body>
</html>

The content of css file:

body {
    color: red;
}

Question: Why doesn't the sentence "Hello World" turn red? Why can't the index.jsp file access the css file?

2 Answers2

1

It looks like you are mapping your servlet "Home" for all incoming requests. So when the browser makes a request for the css url, it is intercepted by the servlet and it is unable to find it. You can change the servlet mapping for the home servlet so that it does not intercept all requests

@WebServlet(name="Home", urlPatterns={"/home"})
Nucleo
  • 26
  • 1
  • nice, I tried and everything works fine. So this is the reason why people use a JSP to redirect to another JSP (in case the URL is the root context) instead of using a servlet. –  Apr 30 '13 at 18:05
  • 2
    +1. As an additional information for @LongHDi: what you did was you redefined the so called **default servlet** in your `urlPatterns={"/"}`. The default servlet is the servlet which serves static resources as well as serves the directory listings (if directory listings are enabled). Read the official Apache Tomcat documentation for more: [Default Servlet Reference](http://tomcat.apache.org/tomcat-7.0-doc/default-servlet.html) – informatik01 Apr 30 '13 at 19:11
  • 1
    @informatik01 that information is very new to me, thanks! I think it is not mentioned in most JSP Servlet books. –  May 01 '13 at 03:13
  • 1
    @LongHDi You're welcome. You can also read the following post about defining *url patterns*, might be helpful as well: http://stackoverflow.com/a/14225540/814702 – informatik01 May 01 '13 at 12:44
  • @LongHDi i do not understand what you mean by your comment about about jsp redirection but i am assuming that you are talking about whether you need to have the context root in the url or not. As a general rule, if the browser is going to make the request, then the context root needs to be present i.e.( in case you are redirecting from a servlet or the url is in the jsp page). In case you are forwarding in the servlet, then you should not put the context root. In your code the c:url tag is going to generate the context root for you so that the generated html code will include the context root. – Nucleo May 03 '13 at 06:12
  • No, I didn't mean that. I was implementing Restful style. When user access the root (homepage), I made a front controller and prepared all the objects, then I save all those objects in request attributes. I put the JSP in WEB-INF folder so people can not access it directly. The only way to access the JSP is to go through the controller. Because the controller had prepared all objects, the JSP only needed to use EL expression to access them. It also separated the url from the view so I could forward to something else later. I couldn't use servlet so I use index.jsp as controller (welcome file). –  May 03 '13 at 09:16
  • what I meant is using a welcome file (JSP) as a front controller because you can not use a servlet as a controller if the URL is "/". I saw people do that in some Spring MVC projects. –  May 03 '13 at 09:35
0

Add following in your jsp

   <link rel="stylesheet" type="text/css" href="/project-context-root-name/css/myfile.css"/>

If it did not work further then do below step.

In your web.xml file add following

  <servlet-mapping>
  <servlet-name>servlet-name</servlet-name>
  <url-pattern>/css/*</url-pattern>
  </servlet-mapping>
Ruju
  • 961
  • 1
  • 12
  • 24