how to load CSS file into jsp - java

I created a jsp page as follows:
<%# page contentType="text/css" %>
<html>
<head>
<title>Login page</title>
<link href="/css/loginstyle.css" rel="stylesheet" type="text/css">
</head>
<body>
<h1> India welfare</h1>
<p> welcome </p>
</body>
</html>
and named it as login.jsp
and i also created a css file called loginstyle.css and the code of the .css file is as follows:
body
{
background-color:#d0e4fe;
}
h1
{
color:orange;
text-align:center;
}
p
{
font-family:"Times New Roman";
font-size:20px;
}
the directory structure for css and jsp's are as follows:
webcontent/welfare_web/css for .css files and
webcontent/welfare_web/login for jsp files
the programming editor is eclipse and the server i am using is tomcat 7.0. when i am trying to run the login.jsp file using tomcat server. The css file is not showing any effect. i mean the output is normal text and is not as per the CSS file.
please help me how to make the .css file to effect the jsp file.

css href link is incorrect. Use relative path instead:
<link href="../css/loginstyle.css" rel="stylesheet" type="text/css">

You can write like that. This is for whenever you change context path you don't need to modify your jsp file.
<link rel="stylesheet" href="${pageContext.request.contextPath}/css/styles.css" />

I use this version
<style><%#include file="/WEB-INF/css/style.css"%></style>

I had the same problem too. Then i realized that in the MainPageServlet the urlPatterns parameter in #WebServlet annotation contained "/", because i wanted to forward to the MainPage if the user entered the section www.site.com/ . When i tried to open the css file from the browser, the url was www.site.com/css/desktop.css, but the page content was THE PAGE MainPage.jsp. So, i removed the "/" urlPattern and now i can use CSS files in my jsp file using one of the most common solutions (${pageContext.request.contextPath}/css/desktop.css).
Make sure your servlet doesn't contain the "/" urlPattern.
I hope this worked for u too,
- Axel Montini

if everything seems correct, and despite it still does not work I invite you to load the statics files in the web.xml like this
<servlet-mapping>
<servlet-name>default</servlet-name>
<url-pattern>/includes/*</url-pattern>
</servlet-mapping>
after
<!-- bootstrap css -->
<link rel="stylesheet" type="text/css" href="/includes/asserts/css/bootstrap.min.css"/>

use this it worked for me
<style><%#include file="/WEB-INF/view/style/style.css"%></style>

For CSS :
<link href="css/bootstrap.css" rel="stylesheet" type="text/css">
For JS :
<script type="text/javascript" src="js/bootstrap.js"></script>
Files location:
└───src
└───main
├───java
└───webapp
├───css/bootstrap.css
├───js/bootstrap.js
├───META-INF
└───WEB-INF/index.jsp
└───lib

Related

How to write the path so style.css can reload on my localhost page?

I m trying to create a sidebar where I have a style.css for customize it. When I inspect my page, it seems it never loads and get 404 not found.
<head>
<h1>sidebar</h1>
<title>Responsive Sidebar Menu</title>
<link rel="stylesheet" type="text/css" href="style.css"></head>
My project is structured like in the following picture:
I can't figure out how to write the path so it can load properly.
Move your template folder right under resources:
src/main/resource/static/css (for CSS files);
src/main/resource/templates (for HTML templates).
Then correct the link tag as follows:
<link href="../static/css/style.css" th:href="#{/css/style.css}" rel="stylesheet" />

How do I include my css file, while using Java and Spring with thymeleaf?

I have tried different apporaches. For me, most logical would be the standard way, as I have it now in my "Item.html":
<html xmlns="http://www.w3.org/1999/xhtml" xmlns:th="http://www.thymeleaf.org">
<head>
<title th:text="${item.name}"></title>
<link href="styles.css" rel="stylesheet" type="text/css"/>
</head>
This is the form what "drag and drop" in netbeans gives me as a path.
My file structure is shown here:
Any ideas?
Create a directory static in your src/main/resources/ folder.
Create a directory css in created static folder to store css files.
Put the css files to the css folder.
In html you can include css like <link rel="stylesheet" type="text/css" th:href="#{/css/style.css}"/>
Here the static folder to serve static contents like css, js files.

Where should I put my css file in Intellij IDEA

I am writing a very simple web application project by "IntelliJ IDEA" and "Tomcat". I downloaded and HTML file and pasted it into my index.jsp, this HTML also has a css file, but I do not know where to put it.
you can put css anywhere in project . All you need to select correct path in href .
<head>
<link rel="stylesheet" type="text/css" href="path_to_css/xyz.css">
</head>
you can put css and html in same directory. then use
<head>
<link rel="stylesheet" type="text/css" href="xyz.css">
</head>

CSS and JS are not rendered when index.jsp is set as welcome file

I am facing a problem in jsp-servlet, when I am setting up my index.jsp as welcome-file (default page to run first as soon as the project gets run) at that time the CSS and JS files are not rendered.
My project Hierarchy is like this
-ProjectName
-User
-css
-js
-index.jsp
And also when I run my project the URL in the browser will be localhost:8080/ProjectName so here index.jsp is called but without CSS and JS files.
Edit: Finally, I had solved this issue by copying the CSS and JS to the root directory also. i.e. I have CSS and JS # "User" as well as "ProjectName".
Try with the context path,
<link href="${pageContext.request.contextPath}/css/style.css" rel="stylesheet" type="text/css">
will get you the path of the css file in the css folder. or with the scriptlet,
<link rel="stylesheet" type="text/css" href="<%=request.getContextPath() %>/css/style.css">
However, it is not recommended to you use the scriptlets in the jsp.

Include css in struts2 jsp page

My struts2 project structure as follows
Webcontent
css
abc.css
jsp
login.jsp
META-INF
WEB-INF
indeed to include the functions of abc.css in login page.How can i do that ? How can i give the path of css file in jsp page ?
Include your css with the following statement.
<head>
<link rel="stylesheet" type="text/css" href="../css/abc.css">
</head>
"../css/abc.css" is a relative path.
"../" represents one directory up. i.e., Login.jsp is in "jsp" directory "../" will make the directory as "Webcontent"
"../css" will traverse you upto "css" directory in the "Webcontent".
"../css/abc.css" will give the abc.css file.
you can include your css file like this.
<head>
<link rel="stylesheet" type="text/css" href="/Webcontent/css/abc.css">
</head>

Categories

Resources