jsp ${pageContext.request.contextPath} dosent get requested - java

Currently i have a .jsp project where my welcome page is a servlet
<welcome-file>frontpage</welcome-file>
The servlet sets gets two ressources, a header file containing the < nav> and a footer containing the < footer>
request.setAttribute("header1", sc.getResource("/includes/nav.jsp").toString());
request.setAttribute("footer", sc.getResource("/includes/footer.jsp").toString());
And forwards to the index.jsp page
getServletContext().getRequestDispatcher("/WEB-INF/jsp/index.jsp").forward(request, response);
My question is.
When i get the ressource (footer.jsp), how can i in the footer.jsp dynamically import / include images?
I tried the following
<img src="${pageContext.request.contextPath}/images/picture1.png" alt="picture1"/>
But the expression ${pageContext.request.contextPath} gets treated as a string instead of a command, and does not get the context path.
I suspect its because the content of the footer.jsp is fetched in this manner and their for the context path isint actually ever requested within the footer.jsp.
But how do i solve this?

add <%# page isELIgnored="false" %> in top of your JSP page, to enable expression language.
and to include a JSP page with other use <jsp:include like:
<jsp:include page="/includes/nav.jsp"/>
<jsp:include page="/includes/footer.jsp"/>

This is not the way to include stuff. Use jsp:include action to include the header/footer. If for some reason you really want to do it in the servlet, see this post. As long as you just grab a resource like you do, you're reading the file like any text, there is no JSP compilation/evaluation.

Related

Using ArrayList in a sub-template on .jsp page

I have a JSP home page with a menu. I want that menu to be on multiple pages so I created a menu.jsp template. I imported, all static source code appears but the <c:forEach /> tag is not picking it up. When I print (just put it plainly in the html) out my ${allCategories} variable I get all the data correctly.
In a controller, I set model.addAttribute("allCategories", categoryService.getPrimaryCategories());
home.jsp where the categories were before is importing <jsp:include page="menu.jsp"/>
in menu.jsp I am unable to get <c:forEach items="${allCategories}" var="category"> to work but when I just put <p>${allCategories}</p> at the beginning of the file it shows stuff like [Category(id=1281, name=Bundles, image=https://images...
How do I get the data correctly in the sub-template so it works like directly on the page?
You could try using the JSP include directive. It is used to include a file during the translation phase. For example,
<%# include file = "menu.jsp" >

Is there any difference between a JSP file and a JSP page?

I was reading the JSP Specification 2.1, the chapters about the JSP include directive and JSP include tag, a lot of places talk about the "JSP file" and "JSP page" seperately, such like:
Is there any difference between them?
Usually, there is no difference when we speak about jsp file or jsp page but a good observation from LuiggiMendoza is:
JSP file is the physical file stored in your hard drive while the JSP
page is the result of evaluating the JSP file from an application
server.
But there is a difference when using directives:
<jsp:include page="page.html" /> and also <%#include file="page.html"%>.
You can find an interesting post about it:
What is the difference between <jsp:include page = ... > and <%# include file = ... >?
And a nice explanation here:
<%# include file="filename" %> is the JSP include directive. At JSP
page translation time, the content of the file given in the include
directive is ‘pasted’ as it is, in the place where the JSP include
directive is used. Then the source JSP page is converted into a java
servlet class. The included file can be a static resource or a JSP
page. Generally JSP include directive is used to include header
banners and footers. The JSP compilation procedure is that, the source
JSP page gets compiled only if that page has changed. If there is a
change in the included JSP file, the source JSP file will not be
compiled and therefore the modification will not get reflected in the
output.
< jsp:include page="relativeURL" /> is the JSP include action element.
The jsp:include action element is like a function call. At runtime,
the included file will be ‘executed’ and the result content will be
included with the soure JSP page. When the included JSP page is
called, both the request and response objects are passed as
parameters. If there is a need to pass additional parameters, then
jsp:param element can be used. If the resource is static, its content
is inserted into the calling JSP file, since there is no processing
needed.
JSP page refers to a "top-level JSP file", which, as defined in JSP.1.1.8:
invoked directly by the client or dynamically included by another page or servlet
While JSP file may refer to a file which, for example, gets statically included.
To illustrate your quote, consider following directory structure:
dir/
file1.jsp
file2.jsp
file2.jsp
index.jsp
Let's say you have <%#include file="dir/file1.jsp" %> in your index.jsp. If you put <%#include file="file2.jsp" %> in dir/file1.jsp, it will include dir/file2.jsp, while if you put <jsp:include page="file2.jsp" />, it will include file2.jsp next to index.jsp.
Single JSP page may be built from several JSP files.
So JSP page is not the same as JSP file.
I believe following is what that means -
JSP file - the actual .jsp file, file as in the filesystem.
JSP page - the result of compilation of the .jsp file, that's currently serving the request
Please also check this answer here - https://stackoverflow.com/a/14763794/738746.
The include directive:
<%# include file="header.html" %>
Static: adds the content from the value of the file attribute to the
current page at translation time. The directive was originally
intended for static layout templates, like HTML headers.
The <jsp:include> standard action
<jsp:include page="header.jsp" />
Dynamic: adds the content from the value of the page attribute to the
current page at request time. Was intended more for dynamic content
coming from JSPs.

Something about JSP include directive and JSP include tag

In the JSP Spec 2.1, I found an example about the JSP include directive and JSP include tag:
For an example of a more complex set of inclusions, consider the following
four situations built using four JSP files: A.jsp, C.jsp, dir/B.jsp and dir/C.jsp :
I don't quite understand this, especially the first and last situation, why the C.jsp are not resolved to C.jsp in the first situation? and why the c.jsp are not resolved to dir/c.jsp in the last situation?
include directive
Use this directive to specify a resource that contains text or code to be inserted into the JSP page when it is translated.
For example:
<%# include file="/jsp/userinfopage.jsp" %>
Specify either a page-relative or context-relative path to the resource.
See Requesting a JSP Page for discussion of page-relative and context-relative paths.
Notes:
The include directive, referred to as a static include, is comparable in nature to the jsp:include action discussed later in this chapter, but jsp:include takes effect at request-time instead of translation-time. See Static Includes Versus Dynamic Includes.
The include directive can be used only between files in the same servlet context (application).
In a JSP include directive the path can be relative to the including page or absolute (then it has to start with a / and belongs to the web application root directory).
For more info have a look at another post here
Please have a look at Including Content in a JSP Page
The directive <%# include> fills in the text of an evaluation of the included JSP with the <%#> directives evaluated. As such the nested include is evaluated with respect to directory of the included JSP.
The tag <jsp:include> calls a compiled version of the included jsp.
So a single JSP is evaluated for text on its own, triggering the evaluation on includes first. Now it should be clear.
First case:
evaluate directives - A.jsp, include dir/B.jsp
evaluate directives - dir/B.jsp, ... include C.jsp = dir/C.jsp
generate java for A.jsp, with text of dir/B.jsp with text from dir/C.jsp.
<%# include file= ... > is executed while the page is translated into a servlet class. Thus, it knows the context of the file system. This is also called static inclusion, and the tag is a direction.
<jsp:include page= ...> is executed while the page is executed for a request. This is also called dynamic include, and the tag is an action. Here, the context of the file system is unknown.
Static inclusion means that a chunk of code lines is copied into the resulting final file. Dynamic inclusion means that the included page is executed, and the result is copied into the resulting final file.
Here are good sources:
http://docs.oracle.com/javaee/1.3/tutorial/doc/JSPIntro8.html
http://www.albeesonline.com/blog/2008/05/22/difference-between-static-include-and-dynamic-include-in-jsp/
http://www.coderanch.com/how-to/java/IncludesActionDirective

forward from spring jsp to spring jsp

in catalog
webapp/WEB-INF/views located my jsp pages
I want to forward from 1.jsp to 2.jsp
in 1.jsp I write
${candidate.name}
but it doesn't work.
How to fix it?
Use <c:url and then the value attribute of <c:url is set to the href link to the other JSP. So for example, it would be:
${candidate.name}
I had used it in the following manner in my webapps (where circuits is a folder under /WEB-INF/views):
Edit Circuit
A relative url should start with a / character
Change the anchor tag as below and try again
${candidate.name}
Reference Link

JSP not detecting the javascript file

From a servlet, I'm forwarding the request to a JSP page which renders a FusionChart.
But I've a problem in loading the chart. The JSP file is not detecting the JavaScript file. The folder structure is:
axis
|
WebContent
|
WEB-INF
|
classes
|_ com
|_FusionCharts.js
|_MyChartJsp.jsp
|_Line.swf
And the JSP code:
<html>
<head>
<script language="text/javascript" src="/WEB-INF/classes/FusionCharts.js"></script>
</head>
<body bgcolor="#ffffff">
<div id="chartdiv" align="left">The chart will appear within
this DIV. This text will be replaced by the chart.</div>
<script type="text/javascript">
var foo = //value fetched from DAO
var myChart = new FusionCharts("/WEB-INF/classes/Line.swf",
"myChartId", "1000", "500");
myChart
.setDataXML("<graph caption='aCaption' xAxisName='xAxis' yAxisName='yAxis' showNames='1' decimalPrecision='0' formatNumberScale='0'>"+foo+"</graph>");
myChart.render("chartdiv");
</script>
</body>
</html>
The Servlet code to forward the request:
final RequestDispatcher requestDispatcher = request.getRequestDispatcher("/WEB-INF/classes/MyChartJsp.jsp");
requestDispatcher.forward(request, response);
The request is getting forwarded to the JSP. But the chart is not getting displayed because it is unable to figure out what FusionCharts is in the line
var myChart = new FusionCharts("/WEB-INF/classes/Line.swf",
"myChartId", "1000", "500");
I tried
src="/FusionCharts.js"
src="FusionCharts.js"
but no luck.
Has it something to do with the request being forwarded??
You cannot have .js (or .swf, .jpg, etc.) files in WEB-INF - they are not publically accessible.
Move it to /js/
There is no reason to hide static resources (like scripts and css) in WEB-INF. If you insist on that, you should make a servlet that, given the name of the js/css, would read it from its location and will serve it as a response. This is what the default servlet does when you access static resources.
The flow of the page loading is as follows: the browser sends a request to the servlet; the servlet forwards internally to the JSP, and the JSP is rendered as a response; then the browser parses the <script> tag and fires another request to the script. If the script is not accessible via URL, it's not loaded.
Then, to make the script url fixed to the servlet context root, use
src="<c:url value="/js/script.js" />"
This will work regardless of what is the current url
Not the cause of your problem, but also note that your <script> element is incorrect. It should be <script type="text/javascript"....
(I tried to post this as a comment, but for some reason it wouldn't let me.)
I was facing same issue. In my case when I calling the myFile.jsp directly its reading the myFile.js;
But when calling through login-> myFile.jsp, its not reading the myFile.js;
After analyzing the path through the Developer tools :=> console, I found that its inserting the uri, so final path was incorrect.
Final Solution:
I'd used the absolute path for all .js and .css. Now its called from everywhere.
My Project Structure is:
In my servlet-context.xml
i) <context:component-scan base-package="com.SBP.SHWeb" />
ii) <resources mapping="/resources/**" location="/resources/" />
My old path for including .js was: /resources/MyJs/myfile.js ===> Its not get called sometimes.
My Absolute path, which get called from all places is like this:
/SHweb/resources/MyJs/myfile.js ==> Its get called from everywhere.
Hope it help you.

Categories

Resources