4

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. http://i.imgur.com/CwtCQVO.png


To make it easier to find, here is everything I posted in comments or elsewhere:

RikuXan
  • 393
  • 5
  • 15
  • This is entirely dependent on how you deploy your application. Also, you should not use absolute URIs, except if you use a CDN of some sort: what if your app is not deployed at `http://localhost:8080`? – fge May 23 '13 at 17:01
  • @fge When you say dependent on how I deploy it, how am I to proceed when I just want it to work inside Eclipse? Also, the host and context are not hardcoded, I get them by `request.getContextPath()`. @ŁukaszLech As far as I understood the post you linked wants to load the static content from somewhere outside of the application server, while I want to load it from inside the server directory / WAR. – RikuXan May 23 '13 at 17:55
  • Short answer for your case is **somewhere inside WebContent**, but **not inside *WEB-INF* or *META-INF***, because the content of those folders are not accessible directly. – informatik01 May 23 '13 at 22:56
  • From what I've gathered so far, the problem seems to be, that I can't access files / folders at all, because the file / folder specification part of the URL (e.g. /images/image.png) get interpreted as a GET parameter. Is there any config setting for tomcat that could be responsible for this behaviour? – RikuXan May 24 '13 at 05:50
  • 1
    btw when replying to someone use `@username` (instead of `username` write the actual username), **so this user will be notified**. Unless the comment is for the author of the answer or the question (like I write this comment to you - the author of the question, so in this case you will be automatically notified without `@RikuXan`). Read here: [How do comment @replies work?](http://meta.stackexchange.com/questions/43019/how-do-comment-replies-work) – informatik01 May 24 '13 at 13:17

3 Answers3

3

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) + "]");
}
Ravi K Thapliyal
  • 51,095
  • 9
  • 76
  • 89
  • When I point my browser to `http://localhost:8080/Blog/test.html`, it just opens my normal page, which I'm writing the servlet for. I assume it uses the "/test.html" as a GET parameter for the servlet. Is there something I have configured terribly wrong in general, because from the sound of your post it's not normal that I can't access any files. – RikuXan May 23 '13 at 19:27
  • Post your web.xml as well as the Servlet code. – Ravi K Thapliyal May 23 '13 at 19:42
  • The web.xml is from Eclipse, I didn't do any changes here. http://pastebin.com/sTg4ugyw Concerning the servlet, completely posting it would probably be a bit overkill, structural it is a standard Eclipse servlet, I implemented the init, destroy doGet and doPost methods and basically all I do is reading textfiles, getting data from a MySQL-DB and assembling my HTTP response string. – RikuXan May 23 '13 at 19:55
  • There's no servlet configured in your web.xml so that means you server is not executing any of your servlet code for any url that you might have tried with. Which brings me to the question what did you mean when you said "test.html, it just opens my normal page"? What exactly do you see? – Ravi K Thapliyal May 23 '13 at 20:01
  • I'm fairly certain that my servlet code does get executed, since it gives me the response I instruct it to give. When browsing to the address you said, it's just the same page (e.g. `http://localhost:8080/Blog/test.html` = `http://localhost:8080/Blog/` . Maybe there is something Eclipse does specifically that a normal servlet wouldn't do. – RikuXan May 23 '13 at 20:08
  • Then the web.xml shared does not belong to this web-application. It's *impossible* for your servlet to get executed without getting listed in web.xml. That's just not how things work! And test.html cannot be the same page as /Blog because test.html would show you an image while /Blog will render an index.* (could be html, jsp etc.) page but which should again be present in WebContent. – Ravi K Thapliyal May 23 '13 at 20:13
  • Can you verify your web.xml at `/webapps/Blog/WEB-INF/web.xml`? – Ravi K Thapliyal May 23 '13 at 20:33
  • The pastebin file is definitely the web.xml in the folder you specified. There is another web.xml in the `\conf` folder, though I already searched thorugh that file and there is no mention of "Blog" either. The only mention of "Blog" is in the server.conf file, more specifically the line: `` – RikuXan May 23 '13 at 20:54
  • Hmm.. I think Eclipse WTP isn't using your Tomcat dir to deploy apps any more. Go to your `/.metadata/.plugins/org.eclipse.wst.server.core/tmp0/wtpwebapps/Blog` folder. You should find your `test.html`, your servlets at `WEB-INF/classes` and `WEB-INF/web.xml` there. Post your `web.xml` and any other helpful info that you can. – Ravi K Thapliyal May 24 '13 at 11:54
  • @RaviThapliyal You wrote: "*It's impossible for your servlet to get executed without getting listed in web.xml.*". Actually **it is possible since Servlet v.3.0**: one can use [`@WebServlet`](http://docs.oracle.com/javaee/6/api/javax/servlet/annotation/WebServlet.html) annotations – informatik01 May 24 '13 at 13:31
  • @informatik01 Well, I didn't say, "It's impossible for **a** servlet..." and so I've obviously been speaking in the context of OP's development environment. And, I've already requested OP to share the servlet code twice but we're yet to see some code. – Ravi K Thapliyal May 24 '13 at 14:14
  • @RaviThapliyal I see. I wrote it just for the sake of correctness, not to confuse newbies )) – informatik01 May 24 '13 at 14:15
  • @informatik01 Usually, I just don't like to bring in anything new (unless it helps or once the original problem has been resolved) that could potentially deviate from the problem at hand and confuse a newbie like annotations. – Ravi K Thapliyal May 24 '13 at 14:18
  • @RaviThapliyal OK, I see your point. – informatik01 May 24 '13 at 14:19
  • Sorry, I didn't think you would really want to see all of the code, but if it helps, here it is: http://pastebin.com/az97bZAY Thanks for getting so involved in my question guys, even though it seems to be a tricky situation. If you need any other information, don't hesitate to ask please. – RikuXan May 24 '13 at 15:35
  • Also, I forgot to add this, I navigated to `/.metadata/.plugins/org.eclipse.wst.server.core/tmp0/wtpweba‌​pps/Blog/WEB-INF` and the web.xml is exactly the one I posted. – RikuXan May 24 '13 at 15:43
  • @RikuXan What about test.html? Did you find one at `wtpwebapps/Blog/`? And, I updated my answer with a piece of code you can use to verify what all request parameters your servlet is receiving when you visit `/Blog/test.html`. Also, the HTML that your servlet finally generates to include images is in one of your html_templates. Please share a part of your template where you include an image. – Ravi K Thapliyal May 24 '13 at 18:08
  • @RaviThapliyal Yes, the test.html I added is found at `wtpwebapps/Blog/`. I executed your code and for a call to `http://localhost:8080/Blog/test.html` it returned an empty page, while for example the call `http://localhost:8080/Blog/?pst_id=5` lists the parameter as intended. I guess it looks like Tomcat just drops every path / file specification after the context. Concerning my HTML templates, here is the most simple one: http://pastebin.com/6KALf0Bw the all caps text in the img tag is where I tried to link the image according to all the proposals. – RikuXan May 24 '13 at 22:30
  • The test.html you deployed isn't empty right? Like you did add the image code I provided for it right? What do you see if you do a view > source on test.html especially the "src" attribute of tag? And what was here ``? – Ravi K Thapliyal May 24 '13 at 22:35
  • @RaviThapliyal The test.html is completely valid, opening the file in a browser shows the image as expected. If i try to view its source when it gets served by tomvat, it's just empty (`http://localhost:8080/Blog/test.html`). The `HERE_I_TRIED_TO_LINK_IT` I filled with different proposals from people from this thread. – RikuXan May 25 '13 at 13:17
  • Okay, so if test.html opened up fine and you saw the Blog/images/image.png getting rendered then that's progress. It means the problem lies in the way you're injecting images into your blog templates. Now, copy "src" value from test.html to `HERE_I_TRIED_TO_LINK_IT` and test if you see that image when you request your BlogServlet. – Ravi K Thapliyal May 25 '13 at 13:40
  • And, I had a look at your BlogServlet's code and I don't see you setting images anywhere. Do a view source on your BlogServlet and verify that src attributes are actually getting set. Change your browser to Firefox or Chrome if you fail to see anything. (Don't forget to scroll down. Sometimes first few html source pages are empty.) – Ravi K Thapliyal May 25 '13 at 13:48
  • The problem has nothing to do with the img-Tag, it is that when I try to open the test.html via tomcat, it returns me an empty page, the HTML is not even sent to the browser. Right now in my servlet's java code I am not setting the images, but I tried previously and it didn't work with any of the suggestions made in this thread. All of this leads me to the conclusion, that the overall problem is tomcat refusing to serve any content but my servlet. – RikuXan May 25 '13 at 14:12
  • Change @WebServlet("/") to @WebServlet("/Blog") and check again. If you see the image this time then perhaps what you're saying is correct that serving your servlet at root is somehow conflicting with other resources. – Ravi K Thapliyal May 25 '13 at 14:17
  • 1
    Wow, it worked, I could finally open the test.html in my browser and it showed the image nice and clear. I was even able to set the @WebServlet tag back to "/" and now it still works. I feel like there was a bug somewhere in the way Eclipse and tomcat go together but it seems that this simple change has fixed it. Thank you very much for your help :) Any place where I can buy you a beer maybe? – RikuXan May 25 '13 at 14:51
  • @RikuXan Happy for you mate :) Well, you haven't mentioned where you're from in your profile. For "free beer" I would definitely ping you if I'm ever on a visit there! – Ravi K Thapliyal May 25 '13 at 15:34
  • @RaviThapliyal I'm from Germany, but I rather meant via PayPal / similar donation sites, since the chances we ever meet are pretty slim ;) Hope I'm not breaking any rules by getting a little offtopic – RikuXan May 25 '13 at 15:43
  • @RikuXan Thanks for the offer. I don't have such a thing set up yet. Just contribute back to the community or maybe help out anyone you find stressed participating in this project thing at your Uni. :) – Ravi K Thapliyal May 25 '13 at 15:53
  • +1 just for the power of will to keep going until this problem is solved. – informatik01 May 25 '13 at 21:12
  • @informatik01 Thanks. OP was indeed on Servlet 3.0 :) – Ravi K Thapliyal May 25 '13 at 21:23
0

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.

Cesar Castro
  • 1,892
  • 1
  • 13
  • 19
  • I tried it th way you wrote, but it didn't work. I put the file inside the WebContent folder in Eclipse and linked my image to `"/Blog/image.png"`, still it didn't show up. – RikuXan May 23 '13 at 17:47
  • Are you, by any chance, using any kind of url rewrite filter? – Cesar Castro May 23 '13 at 19:02
  • I did nothing more than create the servlet in Eclipse, I don't think Eclipse would do that on its own. – RikuXan May 23 '13 at 22:13
0

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

Thai Tran
  • 9,815
  • 7
  • 43
  • 64
  • The host and context aren't hardcoded, I get them by `request.getRequestURL()`. Also your code doesn't seem to be pure Java to me, I'm not sure how to include this into my code. Also, there was no image included in the picture because I didn't want to confuse someone, I just deleted it for the screenshot. I also added it directly vai Eclipse. – RikuXan May 23 '13 at 18:01
  • @RikuXan The code is absolutely OK. The only thing is that it will work only if your page is [**JSP**](http://stackoverflow.com/tags/jsp/info), not pure HTML. What he uses in the above example is called [**Expression Language**](http://stackoverflow.com/tags/el/info). For JSP pages you can also use `` tag from [JSTL](http://stackoverflow.com/tags/jstl/info). See an example of that here: [Adding external resources in JSP](http://stackoverflow.com/a/14571407/814702) – informatik01 May 23 '13 at 22:48
  • Sadly, I can't use JSP, only pure Java / Servlet Code. – RikuXan May 24 '13 at 05:47
  • @RikuXan As a side note: actually JSP files get compiled into Servlets. And if you can use Servlets, then you can use JSP. Because bot the Servlet Containers (like Apache Tomcat, Jetty etc) and full [Application Servers](http://en.wikipedia.org/wiki/Application_server) (like GlassFish, JBoss, WebSphere, WebLogic etc) can handle JavaServer Pages (JSP) – informatik01 May 24 '13 at 13:11
  • 1
    The "can't" is more in a sense of "I'm not allowed to". It's a project for university and there are different teams, that are supposed to solve the problem with different languages (There are also PHP and JSP, I wish I was in one of these groups). – RikuXan May 24 '13 at 15:38