This question already has answers here:
Simplest way to serve static data from outside the application server in a Java web application
(10 answers)
Closed 9 years ago.
When writing a servlet with Eclipse, where am I to put my static content (images, CSS, etc.), so that I can make my HTML link to it (e.g. <img src="http://localhost:8080/context/image.png>). I have tried putting it into the WebContent directory, but that didn't work (or I didn't know how to link to it, I tried <img src="image.png"> and also <img src="http://localhost:8080/context/image.png">).
I attached an image of my Project Explorer, so you can maybe sort it in.
To make it easier to find, here is everything I posted in comments or elsewhere:
The project's web.xml: http://pastebin.com/sTg4ugyw
My Servlet code: http://pastebin.com/az97bZAY
One of my HTML templates: http:pastebin.com/6KALf0Bw
Create a test.html file and place it at /Blog/WebContent/test.html in your Eclipse project.
<html>
<head>
<title>Test WebContent</title>
</head>
<body>
<img src="images/test.png" />
</body>
</html>
Also place a test.png image file inside the /Blog/WebContent/images folder.
Now, point your browser to http://localhost:8080/<your-web-app-name>/test.html and check if test.png gets rendered or not. If yes, then the problem lies in the way you're writing HTML output from your servlet.
For a sample ImgServlet configured as
<servlet>
<servlet-name>ImgServlet</servlet-name>
<servlet-class>pkg.path.to.ImgServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>ImgServlet</servlet-name>
<url-pattern>/ImgServlet</url-pattern>
</servlet-mapping>
your doGet() method should ouput HTML as
response.setContentType("text/html;charset=UTF-8");
PrintWriter out = response.getWriter();
out.println("<html><head><title>Test WebContent</title></head>" +
"<body><img src=\"images/test.png\" /></body></html>");
EDIT: To print all the request parameters your servlet is receiving add the following just before your handleRequest() method call (which you can comment out also for testing)
PrintWriter out = response.getWriter();
Enumeration<String> parameterNames = request.getParameterNames();
while (parameterNames.hasMoreElements()) {
String param = (String) parameterNames.nextElement();
out.println(param + " = [" + request.getParameter(param) + "]");
}
Try
<img src="/context/image.png">
But it does depend on how you deploy your application. Anyways, files like images must be inside WebContent folder.
First of all, dont hard code your context in your link, it will make you hard to change the link later if your context path is changed. Instead, use EL to make the relative path:
<img src="${pageContext.request.contextPath}/img/abc.png" />
Secondly, I dont see any image in your WebContent, if you put the image manually into the window folder, you need to refresh eclipse project in order to let eclipse detects all the added files. Right click on your project in the Project Explorer and select Refresh
Related
This question already has answers here:
RequestDispatcher.forward() vs HttpServletResponse.sendRedirect()
(9 answers)
Closed 6 years ago.
This code snippet is from my CustomerController servlet.
#WebServlet("/CustomerController")
/*
.
.
.
*/
if(request.getParameter("operation").equalsIgnoreCase("search-customer")) {
CustomerDAO customerDAO = new CustomerDAO();
Customer customer = customerDAO.searchCustomer(Integer.parseInt(request.getParameter("customer_id")));
request.setAttribute("Customer-Result", customer);
RequestDispatcher requestDispatcher;
requestDispatcher = request.getRequestDispatcher("/search-results.jsp");
requestDispatcher.forward(request, response);
}
Instead of redirecting to search-results.jsp so as to print the result of the search, the results are printed on the servlet's (CustomerController) url itself.
Refer to the image.
search-results.jsp
<%#page import="com.servlet.Customer"%>
<!DOCTYPE html>
<html>
<body>
Customer Found :
<%
Customer customer = (Customer) request.getAttribute("Customer-Result");
out.println(customer.getCustomer_name());
%>
</body>
</html>
What is wrong ?
You do not want that!
The URL displayed in the browser is the one for the last query sent by the browser. In your case, you send a query to /CustomerController and server side, the servlet forwarded the request to a JSP to display results. That forward is an internal detail in a web application, and you have no reason to show that to the client.
More, you should not let the client know about that. Common usage is to put the JSP used internally by the servlets (via includes or forwards) under the WEB-INF folder precisely to avoid direct queries from the client. What would be the sense to ask the browser to display an URL that it cannot query? The interesting part here is that if you later change from JSP to a template engine like velocity, as that part is hidden from client, nothing will change in the visible interface
I'm not sure that this is the expected answer but you really should wonder why you want to display an internal URL in the client browser. As that will require that you put the JSP outside of the protected WEB-INF folder, it would be definitely a bad practice.
#Maven Maverick. Hope this can help you.
If you want your search-results.jsp in your URL remove that file from your WEB-INF folder and deploy it under WebContent folder(IDE:Eclipse). Because WEB-INF is protected folder its not accessed directly by user call or url. For that you have to move that jsp file under WebContent folder and instead of RequestDispatcher use response.sendRedirect("//test.jsp");return;
Comment If this works for you or not .. because works for me.
Hope it works for you :)
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.
This question already has answers here:
Browser can't access/find relative resources like CSS, images and links when calling a Servlet which forwards to a JSP
(9 answers)
Closed 5 years ago.
After a lot of looking around on various forums and such, I was not able to find an answer to my question.
I want to attach a stylesheet to my servlet instead of having to use <style> tags.
I am using Apache Tomcat 7 in Eclipse and I am manually writing the html code (via a PrintWriter).
I've tried putting the .css file in the ROOT of the WebApp. I've tried putting it in the css. Nothing is working.
Can someone point me in the right direction?
Here is some code that I tried.
Attempt 1 (css is in a folder. WebContent/css:
String cssLocation = request.getContextPath() + "/WebContent/css/styles2.css";
String cssTag = "<link rel='stylesheet' type='text/css' href='" + cssLocation + "'>";
Attempt 2 (css is in the ROOT):
String cssLocation = request.getContextPath() + "/styles2.css";
String cssTag = "<link rel='stylesheet' type='text/css' href='" + cssLocation + "'>";
Neither of those worked.
EDIT: Here is my directory structure:
PROJECT ROOT
src
testPackage
DownloadServlet.java
WebContent
css
styles2.css
files
fonts
js
META-INF
WEB-INF
index.html
To explain: I am trying to reference /WebContent/css/styles2.css in DownloadServlet.java
How I am doing that:
In the method 'doGet', I am initializing a `PrintWriter'. I'm printing out:
<html>
<head>
HERE IS WHERE THE LINK NEEDS TO GO
</head>
<body>
...
</body>
</html>
Where the text "HERE IS WHERE THE LINK NEEDS TO GO" is, that is where I need the link to the css file. I've tried the methods above, but I had no luck.
Just a guess: Try String cssTag = "<link rel='stylesheet' type='text/css' href='/css/styles.css'>";
The browser will look for the css file in the sub-folder of the root directory of the server, which is in your case the WebContent-directory. You usually don't need to call request.getContextPath() when linking resources inside HTML tags.
First you create the css file, suppose style.css in the folder name css inside the WebContent directory of your project.
Then, you must know the tomcat server path where the .css file is located.
String cssTag="<link rel='stylesheet' type='text/css' href='css/style.css'>"
PrintWriter out = res.getWriter();
out.println("<html>");
out.println("<head><title>Title Name</title>"+cssTag+"</head>");
out.println("<body>");
/*
Your code
*/
out.println("</body></html>")
I have seen several examples of using a Servlet to dynamically generate a chart using JFreeChart, and subsequently including that image in a JSP using an img tag. For example:
<img src="/MyChartServlet" width="400" height="300" border="0" alt="" />
My servlet which generates the image using JFreeChart works great, and I can see the image if I call it directly in the browser as in:
http:/myurl/MyChartServlet?id=274
The problem is that my JSP does not display the image. In fact, the JSP is not even invoking the servlet. I know this because I do not see the debug entries in the log that appear when the servlet is called.
In the Servlet I am using the JFreeChart ChartUtilities.writeChartAsJPEG() method to write the image to the output stream of the response, because I do not want to write the image to disk. As mentioned the servlet works fine when called directly.
What am I missing? Or is there a better way to do this? Maybe a plain old object can generate the chart and I can include that in the JSP? Any help would be appreciated.
You're going about it the right way. You may be having some kind of relative path issue from the context you're in. Try
<img src="http://<full path to your servlet>" ...
Also, you have a ?id=274 in your example, but not in your img src. If that's required, put that in there as well.
If you posted your servlet code, that could help, but also make sure you have your content type set properly in your servlet
response.setContentType("image/jpeg");
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.