0

Hey guys i want to link my css file to my jsp . my css file located in WebContent in a folder named css. the path of my css file: /PFE/WebContent/css/styleLogin.css

<head>

<title>Login Page</title>
<link rel="stylesheet" href="css/styleLogin.css" />

</head>

i tried differents paths like :

<link rel="stylesheet" href="../css/styleLogin.css" />
<link rel="stylesheet" href="WebContent/css/styleLogin.css" />
                         /PFE/WebContent/css/styleLogin.css

i don't want to use Internal css even if it's working . when i created my css file i got an error: could not find node.js. this will result in editors missing key features.

UPDATE: i tried

href="${pageContext.request.contextPath}/css/styleLogin.css" /> 
<link rel="stylesheet" href="/PFE/css/styleLogin.css" />  <--% PFE=nameOfProject  --%>

but it's not working i tested the path with :

<style type="text/css">
     <%@include file="/css/styleLogin.css" %>
</style>

i'm sure of the path now cuz i got no error with this syntax but still i can't see my css working . The only solution for now ** i can only use internal css with no include and that's it's working fine but still not optimal . **Update 2 : i created a simple test Web Dynamic app: in WEB-INF:i have index.jsp && web.xml in WebContent:i have a folder named css inside it css file named styleIndex the path

"css/styleIndex.css"

in src : a package 'pack.servlet' a servlet named Index.java

source code of web.xml:

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
  <display-name>test</display-name>
  <welcome-file-list>
    <welcome-file>index.html</welcome-file>
    <welcome-file>index.htm</welcome-file>
    <welcome-file>index.jsp</welcome-file>
    <welcome-file>default.html</welcome-file>
    <welcome-file>default.htm</welcome-file>
    <welcome-file>default.jsp</welcome-file>
  </welcome-file-list>
<servlet>
      <servlet-name>index</servlet-name>
      <servlet-class>pack.servlet.Index</servlet-class>
</servlet>

<servlet-mapping>
    <servlet-name>index</servlet-name>
      <url-pattern>/index</url-pattern>
</servlet-mapping>
</web-app> 

source code of index.java: package pack.servlet;

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;


@WebServlet("/Index")
public class Index extends HttpServlet {
    private static final long serialVersionUID = 1L;


    public Index() {
        super();

    }


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


    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

    }

}

code source of index.jsp :

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Index page </title>
<link rel="stylesheet" href="/test/css/styleIndex.css" />
</head>
<body>
<p>Hello World ... </p>
</body>
</html>

code source of styleIndex.css :

body {
  background-color: lightblue;
}
p{
    color:bleu;

}

when i execute it on my server Apache Tomcatv7.0 i got an error msg : Staring Tomcat v7.0 Server at localhost has encountered a problem . in Details: Server Tomcat v7.0 Server at localhost failed to start. my other project running fine with the same Server .

BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
  • i think im linking it correctly and now i have another problem with my
    ,in my servlet i forward to my jsp but i got 404 error , i can't link with <%@include %> . to be honest i don't know if i'm doing it wrong or i have some config problelmes i'm gonna build another project with new server config TomCat another version and test .
    – f1llTheV0id Jun 16 '20 at 18:12
  • I suggest you start with a HelloWorld application with bare minimum code for the Servlet, JSP/HTML and CSS. It will take less than 10 minutes to do all of it if you have tomcat configured in Eclipse. If you do not succeed, do let me know and I'll post the complete working code with screenshot. – Arvind Kumar Avinash Jun 16 '20 at 18:31
  • you can check my update i'm gonna change to a another Tomcat version and test again . thank you for your help – f1llTheV0id Jun 16 '20 at 19:38

3 Answers3

0

Try using:

<link rel="stylesheet" type="text/css" href="/YOUR_APP_NAME/css/styleLogin.css" />
k92
  • 375
  • 3
  • 15
0

Guys i just reinstalled Eclipse using jdk 8 then i placed my css in a folder named css inside WebContent . and finally linked my jsp with :

<link rel="stylesheet" href="<c:url value ="/css/loginStyle.css"/>" /> 
0

You keep the css file in WebContent folder and refer it simply by adding below line in jsp.

<link rel="stylesheet" href="style.css">

Samar
  • 1
  • 3