How to give full path name in JSP programming? - java

Actually when I run a particular JSP page it run perfectly(with external style sheet) but when servlet return back the control through RequestDispatcher, the page is not displaying properly means it don't took the css file path.
In my program the stylesheet is present inside css directory and the page is present inside another directory named JSP.
The code which include the css in the program is-
<link rel="stylesheet" href="../css/stylesheet.css" type="text/css"/>

Try URL-encoding your CSS file location. If you use taglibs, this might look something like:
<link href="<c:url value="/path/to/stylesheet.css"/>" rel="stylesheet"
type="text/css" />
where you define the path to the stylesheet from your app's web root.
If you want to use multiple stylesheets, you do something like:
<c:set var="stylePath" value="/path/to/cssFolder" />
...
<link href="<c:url value="${stylePath}/stylesheet.css"/>" rel="stylesheet"
type="text/css" />

Can you access the css url in the browser? You can create the full path using context path in jsp.(You can get the context path using request.getContextPath() in JSP)

May be you have to use <base/> html tag to resolve the relative path.
<head>
<base href="/testfolder/" />
<link rel="stylesheet" type="text/css" href="../css/stylesheet.css"/>

<link rel="stylesheet" href="<%=cssFilePath%>" type="text/css" />
Try this by declaring a String cssFilePath = "YOUR_CSS_FILE_PATH" in your jsp.

Related

Folder structures eclipse spring MVC - uploading Images

I am creating uploading files application in Spring MVC application. I am using thymeleaf templates. And there's problem because i Have folder structure builded by gradle(STS).
I have such line in controller:
imageDir=new File("C:/Users/lukas/workspace/uploadingFiles/src/main/resources/images");
The problem occurs here in thymeleaf showimage html script
<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org"
xmlns:layout="http://www.ultraq.net.nz/thymeleaf/layout">
<head>
<title>Script shows specified image</title>
<link rel="stylesheet" type="text/css" href="/css/styles.css"
th:href="#{/css/styles.css}"/>
</head>
<body>
<div class="center">
<h2><a th:href="#{/uploaded}">Click here to previous page where you can choose image to show</a></h2>
</div>
<img th:src="#{${path}}"/>
<div class="center2">
<p th:text="${path}">dddd</p>
</div>
</body>
</html>
It doesn't display \images\0.jpg properly. Only one picture is displayed from other folders not exactly how specified in above line path. It gives output from other other folders for ex.: static images. Even in folder are other images the shown is only one. Even it image isn't in specified in the path.

Resources folder path - where?

I got a problem with load .css file, I put 'resources' in wrong folder probably and I dont know where I have to put it. How it should looks like? Here is a screen:
and I am loading css file like this:
<link rel="stylesheet" type="text/css" href="resources/css/flower.css" />

Cannot access CSS files defined under WEB-INF from JSP

I have created Struts2 application using Maven. In this application I have created two namespaces, first is tc and the second one is cmpui. From the JSP page, I am trying to access .css files, but it is giving me 404 error.
Location of JSP page is :
webapp\tc\layout\stylesheets.jsp
Location of CSS file is :
WEB-INF\css\default.css
Code on JSP page is
<link rel="stylesheet" type="text/css" href="../WEB-INF/css/default.css">
Any suggestion please.
You can not access a resources that are under WEB-INF folder. Move your static resources to another place accessible by Struts2 (for example, web root). And use s:url tag to build the URL.
<link rel="stylesheet" type="text/css" href="<s:url value='/css/default.css'/>">

Can't display images in JSP

I can't display images in JSP (and can't access other things like external Javascript/jQuery through SRC). The following JSP page just contains one <img> tag that can not display the image located by the SRC.
<%#page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>JSP Page</title>
</head>
<body>
<form action="Temp.htm" method="post">
<img src="/images/temp.jpg" alt="Not available">
</form>
</body>
</html>
The images are in /WEB-INF/jsp/images. I have also tried to change the image location and ensure all the times that the path given to SRC is correct still it didn't. The same is possible with the applications without the framework. It works there perfectly. What am I missing here?
First, /WEB-INF is not accessible from the outside.
Second, if you use /images/temp.jpg, the browser will load images from the root of the web server, and not from the root of your web-app (which is deployed under a context path).
Use the JSTL <c:url> tag to generate absolute paths from the root of the web-app:
<img src="<c:url value='/images/temp.jpg'/>" alt=.../>
You cannot access files that are located in /WEB-INF/ directly from the web. The folder is protected from web access.
Locate your image files somewhere else. /images/temp.jpg is a absolute path, however you likely need a relative one. Point your image path to image/temp.jpg and place the images in the /images/ folder directly located under your web root. E.g. WebContent/images or src/main/webapp/images (depending on your web root).
Try put in the webapp directory, not WEB-INF. Obv. dont nest in the jsp dir.

A very strange problem -> CSS file not detected .Java web application

If i browse my site using http://localhost:8080/abc/Login/index.jsf, everything works fine. But if browse it using simply http://localhost:8080/abc, the page is shown but all its images and css files are missing. What can be the problem?
I have this in my web.xml :-
<form-login-config>
<form-login-page>/Login/index.jsf</form-login-page>
<form-error-page>/Login/index.jsf</form-error-page>
</form-login-config>
Probably, images are not getting referenced correctly. Can anybody help me?
I have referenced css file as follows :-
<link href="./Css/MainStyleSheet.css" rel="stylesheet" type="text/css" />
where Css is folder in Login
Your problem (as you guessed) comes from how you have referenced the CSS.
Probably the easiest way to fix it is to be more specific with your reference:
<link href="/abc/Login/Css/MainStyleSheet.css" rel="stylesheet" type="text/css" />
This reference should work regardless of where the calling file is in your directory structure.

Categories