1. TTfile
  2. JSP File
  3. How to reference a CSS file from JSP?

How to reference a CSS file from JSP?

One of the benefits of Java Server Pages (JSP) is that you can let your Web server do some of the work traditionally left to your visitor's Web browser. In traditional static HTML, CSS (cascading) stylesheets are referenced using the "link" tag, requiring your visitor's Web browser to fetch the stylesheet before rendering your Web page. Using JSP, you can include the CSS directly, making faster Web pages for your visitors.

0

Instructions

Step 1

Open your JSP file in your favorite editor.

Step 2

Locate the header section of the document (between the and tags near the top of the file).

Step 3

Start a style block on a new line by adding

8.

Hit the Enter key to add a new line below the style tag.

Step 4

Add a reference to your CSS stylesheet using the JSP include directive by adding

<%@include file="style.css" %>.

Use the correct path and filename of your stylesheet in place of style.css. Hit the Enter key to add a new line below the JSP tag.

Step 5

Finish the style block by adding

9.

Step 6

Save the file and test it. When your JSP file is executed, it will copy the contents of your CSS stylesheet into the style block of the HTML generated by the JSP.

Tips & Warnings

If your JSP executes without errors, but the page is not styled according to your CSS stylesheet, make sure the directory path and file name are correct. The directory path is relative to the JSP file. On some Web servers, the file name may be case-sensitive. Make certain the path and file name in the JSP are spelled exactly as they appear on the server.