Doubts about using jsp include - java

I'm starting to develop systems more precisely in java web applications using jsp'm more a problem that still can not find the solution. I have a file called [header.jsp] that is the links for CSS and other things that are important to my system that file is in the root directory. I have a folder that has other jsp files more when I try to do an include file [header.jsp] does not work, does anyone have experienced the same problem that could help me?
Thank you in advance.

TestFile.jsp
<%# include file="header.jsp" %>
<center>
<p>Thanks for visiting my page.</p>
</center>
<%# include file="footer.jsp" %>
header.jsp
enter code here
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
Hello from Header
</body>
</html>
Same for footer.jsp
The include directive is used to includes a file during the translation phase.
This directive tells the container to merge the content of other external files with the current JSP during the translation phase.

Related

Eclipse IDE 2022-06, how to change template when create new JSP file?

I am using Eclipse IDE 2022-06 , Java/JDK 1.6, Windows 10 pro x64, Spring 2.5.6 .
I need encoding UTF-8 by default, not ISO-8859-1. I don't want edit many files, many time manually. How to change template when create new JSP file?
In the New JSP File dialog click Next
to choose a template or
click the JSP Templates link to go to the preferences Web > JSP Files > Editor > Templates where you can change the template for JSP.
By default, in the JSP templates the variable ${encoding} is used which can be configured in the preferences Web > JSP Files.
sample result
<%# page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
</body>
</html>

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.

Image cannot be displayed by a simple JSP

I have a JSP which simply shows and image as header. I will change this into a .tag file for custom tag development. I am doing this in eclipse and my project structure is -
The jsp i am trying to run on the server is Header.jsp under jsp folder. The problem is that the image is not displayed, even when I use the fully qualified path of the image. Insted, i see a red cross. How do I fix this ?
When I use this file as a .tag file referenced by another jsp, the contents of tag do not appear in that jsp.
JSP code -
<%# 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">
<%# taglib prefix="myTags" tagdir="/WEB-INF/tags"%>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<img src="/images/java_logo.gif"><br>
</body>
</html>
The URL you specify here is absolute for your site, not the web app.
<img src="/images/java_logo.gif">
So try to use (with WEBAPPROOT replaced by the correct name)
<img src="/WEBAPPROOT/images/java_logo.gif">
or make it relative:
<img src="../images/java_logo.gif">
If you're in JSP or Facelets, it is way better to use HttpServletRequest#getContextPath:
<img src="${request.contextPath}/images/java_logo.gif" />
If you happen to use JSTL:
<img src="<c:url value="images/java_logo.gif" />" />
In this way, you avoid using relative paths and/or guessing what would be your current web application path (this is in case you change the name to display the app or something similar). For example, if you happen to have this structure:
- WebContent
- images
+ java_logo.gif
- jsp
+ Header.jsp
- anotherFolder
+ Another.jsp
If you want to add java_logo.gif in Another.jsp, you just need to do this:
<img src="${request.contextPath}/images/java_logo.gif" />
Unlike the relative path:
<img src="../../images/java_logo.gif" />

How can I reference a javascript library from a JSP in a java web application?

I am trying to create a table with sortable columns using a javascript library provided by this website. The web application is being created in Java and the table is created but not with the CSS style. I am 100% new to html and javascript and just started trying to learn things today.
I have placed the library in the same source folder as the JSP and also at the root of the project but both times the table is created in standard html format without the script formatting. How should I reference a Javascript library from a JSP in Java Web Application?
I have made some progress but I still cannot get it to work properly. This is my WelcomePage.jsp file:
<%#page contentType="text/html" pageEncoding="UTF-8"%>
<%response.setHeader("Refresh", "5");%>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>title</title>
<script type="text/javascript" src="sorttable.js"></script>
</head>
<body>
<p>Start</p>
<table class="sorttable">
<tr>
<td>text </td>
<td>text </td>
<td>text </td>
<td>text </td>
<td>text </td>
</tr>
</table>
</body>
</html>
I have tried placing it into the META-INF, WEB-INF, and the root folders none of them seem to work correctly. I have also attempted to add it to the web.xml file but nothing seems to work.
Some things I have already tried
Can not include javascript file from WEB-INF directory in JSP.
how to reference a java .class file from a JSP page?
Put it at the root of your application, or under a /scripts folder there. WEB-INF and META-INF are not accessible from outside.
If it is not accessible, then check the logs, try opening the js file manually in the browser, and check with Firebug (or similar tool) if there aren't javascript errors.
Then, if everything is ok, I suppose you need to invoke some function and pass the table id.
Since you are going to create web pages and you will probably need more js libraries, why don't you make a new folder inside the Web Pages one, solely for js?

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.

Categories