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).
Related
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" />
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.
I have a working servlet code, which processes some command prompt commands and I have an editor code written in html, now I want to add this html file to servlet. How do I do it? tried moving the html file to WEB-INF.
I've looked into this link on How to integrate HTML design into Servlet? but everything seems damn cumbersome as I have a very large html file.
Any other fixes?
If your editor.html contains only static JS, CSS and HTML (you don't depend on the data provided by your servlet), you can simply forward your servlet into JSP and include your html page from it using
<%# include file="editor.html" %>
I have a JSP page called main.jsp and the jsp page has three iframes.
Each Iframe loads an individual JSP(page1.jsp , page2.jsp , page3.jsp) and each JSP uses a individual JS file consists of JQuery code.
Currently I included the Jquery 1.9.1.js in every JSP page ( main.jsp and it's iframe laded JSP's ) and my project works fine .
Is this the proper way , Because I included the Jquery 1.9.1.js in every JSP .
How I can load the Jquery 1.9.1.js in my main.jsp alone and make visible to all JSP's .
I don't want to include the Jquery 1.9.1.js in every JSP's . I need the Jquery 1.9.1.js to be centralized.
Is this possible ?
Hope my question is little clear and understandable.Please don't hesitate to edit or ask questions.
I dont think it is possible when you use iFrames.
Since each iframe src will load the new page and it runs in its own window context
Its not easy to use a single jQuery.js to control the behaviour for the controls inside iFrame.
So you did correct.
Still if you want, you could use different <div> and load the content into it. This way one jQuery will work, since all the divs are in same window context
While using iframes you have to add jquery in each page.Each page in iframe is considered as child.If you were using divs in place of iframe then using centralized jquery1.9.1.js would have worked.
I think you should make templates Header.jsp and Footer.jsp. Then included the Jquery 1.9.1.js on header or footer.
Then you can use header or footer on your main.jsp and on every page.
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>