I am trying to load a JSP page in IFrame under webapp/WEB-INF/jsps like following:-
<iframe id="frame1" frameborder="1" src="framebody.jsp" width="100%" height="350px">
Note:- Both the pages are in same directory webapp/WEB-INF/jsps.
Getting Exception on browser console : -
Failed to load resource: the server responded with a status of 500 .
Files located within WEB-INF folder can not be accessed directly.
You need to make the src to a servlet request,and make the response of the request to the jsp file.
<iframe id="frame1" frameborder="1" src="queryBody" width="100%" height="350px">
#RequestMapping("queryBody")
public String queryBody(){
return "framebody.jsp";//make sure you have set the prefix to /WEB-INF/jsps
}
Related
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" >
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.
display.jsp
<body>
<h1>Displaying Image</h1>
<%
Authentication auth = SecurityContextHolder.getContext().getAuthentication();
session.setAttribute("username", auth.getName());
%>
<c:forEach var="imageName" items="${files}">
${username}${imageName}
<img src="../images/${username}/${imageName}"/>
</c:forEach>
</body>
Saving the images in path
(C:\workspace\fileupload\.metadata\.plugins\org.eclipse.wst.server.core\tmp0\wtpwebapps\images\user1\)
in code using .....
new File(request.getSession().getServletContext().getRealPath("../")+"/images"+"/"+auth.getName());
reaching to the deployed webfolder and then coming one step back(as using here ../) then creating a folder "images" then inside images creating a folder with the name of user (as using here +"/"+auth.getName()), saving the images successfully.But while retrieving these image, i tried the code written in jsp, but it is not working.here in jsp , i checked for ${username} and ${imageName} both are retrieving correct values from controller, means there is not any issue with controller side, but while constructing the url for retrieving image, it is getting wrong somewhere.Could someone plz help to get it right
src="../images/${username}/${imageName}"
Keep your Image folder in the place where display.jsp is stored and try again.
I have an index.jsp page which uploads an image. On submit it goes to a servlet Upload.java. In the servlet I am checking if the extension in of image("jpg","png",etc) and forwards to new jsp page else it shows an error message and includes the same index.jsp page.
My servlet is a package named "servlets".
If I select an image then it is working properly. But if I select any file other than image then it shows the error with the index.jsp page as intended. Till now it works fine but if I upload any file even image from here, the server complains.
Here is how I am including the index.jsp page in UploadServlet.java servlet.
out.println("This type of file is not allowed. Please select an image.");
RequestDispatcher dispatcher = getServletContext().getRequestDispatcher("/index.jsp");
dispatcher.include(request, response);
Here is the error from the server when I try to upload the image second time.
HTTP Status 404 - /UploadImage/servlets/servlets/UploadServlet
type Status report
message /UploadImage/servlets/servlets/UploadServlet
description The requested resource (/CropImage/servlets/servlets/UploadServlet) is not available.
Apache Tomcat/6.0.13
It is appending the servlet's package name to the url.
How to solve this problem?
Apparently you're using a relative action URL in your <form>.
<form action="servlets/UploadServlet" ...>
When you open index.jsp, the request URL is
http://localhost:8080/UploadImage/index.jsp
When you submit the form, the action URL is relative to the current folder, so request URL will be
http://localhost:8080/UploadImage/servlets/UploadServlet
When you submit the form once again, the will be still relative to current folder, so you end up in
http://localhost:8080/UploadImage/servlets/servlets/UploadServlet
You need to fix it to be a domain-relative URL, starting with a leading slash.
<form action="/UploadImage/servlets/UploadServlet" ...>
This way the URL will be resolved relative to the domain root. You can also resolve the context path dynamically by ${pageContext.request.contextPath}:
<form action="${pageContext.request.contextPath}/servlets/UploadServlet" ...>
Your url is wrong. You can open the web.xml and find the "servlet-mapping" element there you can find the mapping url.
I guess your url may be "/CropImage/servlets/UploadServlet" .you can try to delete one "servlets" in the url.
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.