I have strange situation with my image servlet. It works well with Eclipse internal web browser, but won't to work with FireFox, InternetExplorer, Chrome, Opera....
Servlet code (below)
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
File fl=new File("C:\\Documents and Settings\\Administrator\\My Documents\\Eclipse\\zadatak\\WebContent\\WEB-INF\\upload\\"); PrintWriter pw=response.getWriter(); response.setContentType("text/html"); pw.println("<html>"); pw.println("<head>"); pw.println("<style type=\"text/css\">"); pw.println("img{display:block-inline;width:100px;height:100px;"); pw.println("</style>"); pw.println("</head>"); pw.println("<body>"); pw.println("<table border=\"1\">"); pw.println("<tr>"); for(File f:fl.listFiles()){ pw.println("<td>"); pw.println("<img src=\"" + f.getPath() + "\">"); pw.println("</td>"); } pw.println("</tr>"); pw.println("</table>"); pw.println("</body>"); pw.println("</html>");
}
In Eclipse internal web browser looks like this (below)
PrintScreen of Eclipse internal web browser
Finally I found a solution for my problem. Thanks you all for helping me!
Now, this code works!
ServletContext context=request.getServletContext(); String path=context.getRealPath("upload"); File fl=new File(path); PrintWriter pw=response.getWriter(); response.setContentType("text/html"); pw.println("<html>"); pw.println("<head>"); pw.println("<style type=\"text/css\">"); pw.println("img{display:block-inline;width:100px;height:100px;"); pw.println("</style>"); pw.println("</head>"); pw.println("<body>"); pw.println("<table border=\"1\">"); pw.println("<tr>"); LinkedHashMap<Integer, String> hm=new LinkedHashMap<Integer,String>(); int imageIndex=-1; for(File f:fl.listFiles()) { if(f.getName()!=null){ if(f.getName().endsWith(".bmp")) hm.put(++imageIndex, f.getName()); } } Iterator<String> pic=hm.values().iterator(); while(pic.hasNext()){ pw.println("<img src=\"upload/" + pic.next() + "\"/>"); } pw.println("</tr>"); pw.println("</table>"); pw.println("</body>"); pw.println("</html>");
class hierarchy in my project looks like
MyWebProject
|-- src
| :
|
|-- web
| |-- META-INF
| | `-- MANIFEST.MF
| |-- WEB-INF
| | `-- web.xml
|-- upload (the place where I keep pictures)
:
Firefox now can correctly to show pictures
src
attribute. – BalusC