pageContext.request.contextPath not working - java

I'm using Tomcat 6.0.32 and I'm trying to include a JSP file into another, but somehow the file cannot be found (yes, the file exists). Here is the file structure and code:
<jsp:include page="${pageContext.request.contextPath}/templates/header.jsp">
</jsp:include>
WebContent
- folder/caller.jsp
- templates/header.jsp
However, if I use ${pageContext.request.contextPath} outside of that to check if it outputs correctly, it works, does anyone know why this is happening?

I'm not even sure if you can use runtime variables inside a <jsp:include>, but the other problem is that the contextPath refers to the context of the URL being requested from the server, and does not necessarily correspond to your filesystem layout.
Why wouldn't you just use <jsp:include page="templates/header.jsp">?

i completely agree with the above answer however if u have to use it this way then the below code shd work
<c:set var="myContext" value="${pageContext.request.contextPath}"/>
<jsp:include page="${myContext}/templates/header.jsp"> </jsp:include>

Related

JSP Template in other file - Spring Boot

I'm making a web application in spring boot. I have a lot of views created in JSP, but every view has the same footer and the same menu. Of course I can just copy all menu and footer code and paste it to 30 JSP but I would like to do somethink like this: I have file in which I have something like that menu="HTML CODE WITH MENU", and then in JSP in body section I just put {menu}, and all HTML code is put inside JSP. When I want to change my menu I will just change its code in "menu" variable/string or whatever it will be, and menu in all JSP will change. I tried a few solutions for example with include but it doesn't work. I search scritly the solution I have mentioned. Has anybody got any ideas how I can do this ?
Since you are using JSP you can use the standard JSTL <c:import> tag as explained here. The other options would be to use <%# include #> or <jsp:include>, either of them will work for local files:
<jsp:include page="footer.jsp" />
<%# include file="footer.jsp" %>
<c:import url="footer.jsp" />

jsp:include gives 404 when running

My directory tree is like this,
- /index.jsp
- /template/topHeader.jsp
- /accounts/signin.jsp
In my index.jsp I have included the topHeader.jsp like this,
<jsp:include page="template/topHeader.jsp" />
This works fine without any problem. But in the topHeader.jsp I have some <a> tags which I have given paths,
<div class="pull-left">
Hello! Sign in.
</div>
After running, this link gives a 404 which is understandable because the index page is already in the root folder. Now in the future I may have to include this topHeader.jsp almost everywhere.
Browser changes the url like this,
localhost:8084/project/index.jsp
after clicking on the sign in (404)
localhost:8084/accounts/signin.jsp
but the path should be
localhost:8084/project/accounts/signin.jsp
I have also tried including the topHeader.jsp in the following way, but got the same results.
<%# include file="template/topHeader.jsp" %>
So what is the easiest way to give the paths to <a> tags. I'm sorry the question may seem a bit unclear but I don't know how to explain it better.
Use ${pageContext.request.contextPath} to give relative paths to the links,
Sign in

How can I get the server filepath of the JSP from a servlet?

This is my second shot at asking the question - I'll provide some more details this time to help get an informed answer.
What I'm trying to really do here is populate a drop-down menu with data that is dynamically generated from an executable that's supposed to run on a page load. The catch is, the executable must be in a directory local to the JSP on the server. My company has 3 or 4 clones of this website and I cannot place the executable in some absolute filepath - it must be relative (because it will be shipped with the JSP page to other processors all at once by some other team).
I know that JSP has the ability to run executables with relative paths because I could run this:
<form action="./my_executable_that_generates_a_dynamic_page">
However, I need this to run from a servlet to populate the dropbox using javascript.
<%= new File("./exec_produce_dropdown_list").getPath() %>
The above snippet gives me a different directory.
I've also tried:
<%= new File(new File(request.getServletContext().getRealPath(request.getRequestURI())).getParent().replace('\\', '/'), "exec_produce_dropdown_list"); %>
And this is pretty close except if there's a virtual path in the URI that doesn't correspond to the filesystem (which is what we have), it won't point to the right directory.
Is there a simple way for me to run the executable??
I also would like to add that there is no web.xml for me to work with.
Found my answer:
request.getRealPath(request.getServletPath())
Should give me the /path/in/filesystem/to/file.jsp

How to read from file on page load using jsp?

I have one webpage and a file on a server. How to read from file on every page load. I m using jsp. Is there any function available to check page load?
Every page load means that you come to server every time (cache is another story).
So, jsp is loaded from the server every time and here is simple directive to include file to jsp:
<%# include file="foo.html" %>
Keep in mind that server knows only about jsp changes but not about foo.html changes. So, if you change only foo.html server doesn't know about it. That's the reason why this approach is not common. It is used mostly for common templates and parts of all pages (like common footer) even there are other better modern techniques to do so (like CSS).
However, if you still want to use external file which constantly changes just remember that JSP is Java too and you can use whatever you do in Java (except it is not recommended - JSP should be simple viewer in MVC).
So, something like this will work:
<% out.write(Files.readAllBytes("foo.html")); %>
You can use any techniques to read file and write it to the response output.
Addition for your comment:
Text field is regular html. Input to it would be like this:
<input name=abc value="<% out.write(Files.readAllBytes("foo.txt")); %>">
but, again, please consider more modern techniques like DHTML, AJAX, CSS or simple JavaScript.

include directive and <jsp:include> attribute name problem

Why in JSP we write attribute name as file in include directive, but as page standard action?
<% include file="target.jsp" %> will inline the source of target.jsp into your page, and then the whole thing will then be evaluated as a single JSP. This is done at JSP compile time. This can be highly optimized by the container, and can have side-effects. For example, if you change the content of target.jsp, the container generally will not recompile the JSPs that included it.
<jsp:include page="target.jsp"/> will execute target.jsp as a seperate JSP, and then include the output of that execution into your page. This is done at JSP execution time. Note that this can refer to any path inside the container, not just a JSP (e.g. you can include the output of a servlet).

Categories