This question already has answers here:
Browser can't access/find relative resources like CSS, images and links when calling a Servlet which forwards to a JSP
(9 answers)
Closed 6 years ago.
I'm having trouble accessing some images and a css file from my JSP stored in WEB-INF. My servlet calls up the JSP and that all loads fine but the page is unable to find the images and css file. I'm very new to this sort of thing and would appreciate the help. I am aware that there have been some questions on this in the past and I've tried the suggestions posed by those but I can't seem to get my head around it. Here's the contents of the JSP (I tried a bunch of different methods):
<%# page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!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=ISO-8859-1">
<title>Title</title>
<link rel="stylesheet" type="text/css" href="resources/grid.css">
</head>
<body>
<!-- Main container -->
<div class="container_12"></div>
<!-- Foreground container -->
<div class="grid_10 prefix_1 suffix_1">
<img src = "/resources/images/Foreground.png">
</div>
<!-- Header container -->
<div class = "grid 8 prefix_2 suffix_2">
<img src = "${pagecontext.request.contextPath}/resources/images/Header.png">
<p>Test</p>
</div>
<div class = "clear"></div>
<!-- Navigation bar container -->
<div class = "grid 8 prefix_2 suffix_2">
<img src = "${pagecontext.request.contextPath}/resources/images/Navbar.png">
</div>
</body>
</html>
And here's my directory structure:
http://gyazo.com/0a7aea0a44e51e1789c263767e14a40b
You can use the below to find the real path of an image.
<!-- Header container -->
<div class = "grid 8 prefix_2 suffix_2">
<img src = "<%= application.getRealPath("/resources/images/Header.png") %>">
<p>Test</p>
</div>
Put all the files or css-images outside the WEB-INF folder. It is good practice that in WEB-INF you put only classes and web.xml.
Related
Hi Can anyone help me out to resolve the error mentioned above.
I used a index.jsp file
<%# page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<%# taglib prefix="s" uri="/struts-tags"%>
<!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=ISO-8859-1">
<title>Hello World</title>
</head>
<body>
<h1>Hello World From Struts2</h1>
<form action="hello">
<label for="name">Please enter your name</label><br/>
<input type="text" name="name"/>
<input type="submit" value="Say Hello"/>
</form>
</body>
</html>
After writing all the classes and xml files,I have run the classes by tomcat server but got an error as follows
Can anyone help me out as I am new to Struts
The taglib is included in struts-core.jar, and the container will discover it automatically.
for some reason, a taglib configuration is needed within web.xml, extract the TLD file from the struts-core.jar META-INF folder, and add a taglib element to the web.xml.
<!-- ... -->
</welcome-file-list>
<taglib>
<taglib-uri>/s</taglib-uri>
<taglib-location>/WEB-INF/struts-tags.tld</taglib-location>
</taglib>
</web-app>
Try these:
Seems like you copied the libraries manually and directly into the folder WEB-INF/lib of the project and eclipse doesn't sense them, I suggest that you copy any necessary libraries into WEB-INF/lib only through eclipse. I had this issue previously and only a refresh of the project solved the problem.
add struts2-core.jar to the project
download it here.
Select Project and right click, Click on the properties, Click on
libraries tab, Click on 'Add Jars', Add relevant jar for you.
I am learning Jsp and working on a basic Jsp project which is using a HTML file in it.
When I run the HTML file, it gives page not found error and while running the Jsp, it does not load the form and button in it.
Kindly help me out.
Below are my HTML and Jsp files:
Index.html
<html>
<head><title>New</title></head>
<body>
<form action="myFirstJsp.jsp">
<input type="text" name="uname">
<input type="submit" name="submit">
</form>
</body>
</html>
myFirstJsp.jsp
<%#page import="java.util.Calendar"%>
<%# page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!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=ISO-8859-1">
<title>My First JSP</title>
</head>
<body>
<%String name= request.getParameter("uname");
out.println("Welcome " +name);%>
</body>
</html>
First check if your server is up & your app is well deployed on it. Check the request url being fired from the browser.
Also
It's a good practice to use jstl tag to provide urls in your application. It computes the context path averting 404 page not found errors due to incorrect urls.
<form action="<c:url value="/myFirstJsp.jsp">
Add the jstl library in your jsp
<%#taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"/>
I'm developing a web page, in that page at bottom I have to upload a file. I have code for upload, but that is in JSP. How I can include that JSP in my HTML page?
index.jsp
<%# page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!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>File Upload Example in JSP and Servlet - Java web application</title>
</head>
<body>
<div>
<h3> Choose File to Upload in Server </h3>
<form action="UploadServlet" method="post" enctype="multipart/form-data">
<input type="file" name="file" />
<input type="submit" value="upload" />
</form>
</div>
</body>
</html>
You can include an html file in a jsp file but cannot include a jsp file in a html file because html files are not parsed on the server-side (so how will the server know to do the include?) and jsp are (assuming you are using a servlet-container).
Make the main file a JSP, put the snippet in an html file, then include in your DIV by using
<div>
<jsp:include page="snippet.html" />
</div>
i have just started learning jsp, i came across running Applet from jsp, the code below doesn't seems to work.
<%# page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!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=ISO-8859-1">
<title>InfoVisual</title>
</head>
<body>
<jsp:plugin type="applet" codebase="WEB-INF/classes" code="DemoApplet.class" width="400" height="400">
<jsp:fallback>
<p>Unable to load applet</p>
</jsp:fallback>
</jsp:plugin>
<input name="btnShow" value="Show" type="submit">
</body>
</html>
My eclipse project structure is like below
I don't know why i can't see the applet running.
Have i placed files in right folders???
Classes in WEB-INF/classes are only visible to server-side code such as servlets. Instead create a JAR file containing the compiled classes required to run the applet are place them in the same location as the JSP file. The plugin tag will then look like
<jsp:plugin type="applet" code="DemoApplet.class" archive="MyAppletJar.jar" width="400" height="400">
<jsp:fallback>
<p>Unable to load applet</p>
</jsp:fallback>
</jsp:plugin>
this code worked perfectly:
<applet code="DemoApplet.class" name="DemoApplet" archive="DemoApplet.jar" width=300 height=300>
</applet>
and here is my project structure:
Thanx for the help :))
Internet Explorer doesn't support HTML <base> tag and even other browsers do, there are some problems when redirect takes place inservletsto some.jsppages for examplerequest dispatching.`
It's feasible to add ${pageContext.request.contextPath} with each URL
nor request.getServletPath()
JSP relative links for CSS and images with servlets forwarding may change things a lot. This link : Browser can't access/find relative resources like CSS, images and links when calling a Servlet which forwards to a JSP
Is there a better approach with JSP / servlets or it's just an IE issue?
Link : HTML <base> TAG and local folder path with Internet Explorer
And if it is an IE issue:
1. how to fix the IE issue as the above post is unable to give a valid answer?
2. how to solve it with JSP / servlets?
My website is now showing CSS and images.
E.g. HTML output is:
<base href="http://localhost:8080/Alpinema/" /> is not working for
<link media="all" rel="stylesheet" type="text/css" href="css/all.css">
It works in other browsers like Firefox and Chrome.
My JSP code portion:
<head>
<base href="${fn:substring(url, 0, fn:length(url) - fn:length(uri))}${req.contextPath}/" />
<meta charset="utf-8">
<title>Alpinema.com</title>
<link media="all" rel="stylesheet" type="text/css" href="css/all.css">
/css?family=Merriweather|PT+Sans:700|Nobile:400italic' rel='stylesheet' type='text/css'>
</head>
Use <c:url> tag from JSTL to reference CSS/JavaScript resources inside my JSP files. By doing so you can be sure that the CSS/JavaScript resources are referenced always relative to the application context (context path).
Example
index.jsp:
<%# page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%# taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<!DOCTYPE html>
<html>
<head>
<title>Some Title</title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<link type="text/css" rel="stylesheet" href="<c:url value="/css/main.css" />" />
<script type="text/javascript" src="<c:url value="/js/utils.js" />"></script>
<script type="text/javascript" src="<c:url value="/js/jquery-1.8.3.js" />"></script>
</head>
<body>
...
</body>
</html>
For even more solutions see my answer here:
Adding external resources (CSS/JavaScript/images etc) in JSP.