java 如何使用响应中包含图像的 Servlet 显示 JSP 页面?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6347752/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to show a JSP page using Servlet which has image in its Response?
提问by a k
Servlet doGet() code for getting an Image from Database and to store image in Response
用于从数据库获取图像并将图像存储在响应中的 Servlet doGet() 代码
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws
ServletException, IOException {
// Get userid from session
try {
// Get photos from database in (image)
// Init servlet response.
response.reset();
response.setBufferSize(DEFAULT_BUFFER_SIZE);
response.setContentType(image.getContenttype());
response.setHeader("Content-Length", String.valueOf(image.getLength()));
response.setHeader("Content-Disposition", "inline; filename=\"" + image.getTitle()
+ "\"");
// Prepare streams.
BufferedInputStream input = null;
BufferedOutputStream output = null;
try {
// Open streams.
input = new BufferedInputStream(image.getPhoto(), DEFAULT_BUFFER_SIZE);
output = new BufferedOutputStream(response.getOutputStream(),
DEFAULT_BUFFER_SIZE);
// Write file contents to response.
byte[] buffer = new byte[DEFAULT_BUFFER_SIZE];
int length;
while ((length = input.read(buffer)) > 0) {
output.write(buffer, 0, length);
}
} finally {
// Gently close streams.
output.close();
input.close();
}
//Redirect it to photo page
RequestDispatcher rd = request.getRequestDispatcher
("/webplugin/jsp/profile/photos.jsp");
rd.forward(request, response);
} catch (Exception e) {
e.printStackTrace();
}
}
However, When this servlet shows the JSP page it shows only image
and not the JSP page.
但是,当这个 servlet 显示 JSP 页面时,它显示only image
而不是 JSP 页面。
JSP code:
JSP代码:
... JSP code
<img src="Servlet url">
... JSP code cont...
What output I get:
我得到什么输出:
- I only get Image instead of image inside JSP
- When I use RequestDispatcher/sendRedirect() I get following Exception
java.lang.IllegalStateException: Cannot forward after response has been committed
- 我只得到图像而不是 JSP 中的图像
- 当我使用 RequestDispatcher/sendRedirect() 我得到以下异常
java.lang.IllegalStateException: Cannot forward after response has been committed
Question:
问题:
- How to get Image inside JSP instead of just Image in browser
- How to avoid above Exception?
- 如何在 JSP 中获取图像而不仅仅是浏览器中的图像
- 如何避免上述异常?
EDIT: My Web.xml looks like this
编辑:我的 Web.xml 看起来像这样
<servlet>
<servlet-name>Photo Module</servlet-name>
<servlet-class>app.controllers.PhotoServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>Photo Module</servlet-name>
<url-pattern>/Photos</url-pattern>
</servlet-mapping>
回答by BalusC
How to get Image inside JSP instead of just Image in browser
如何在 JSP 中获取图像而不仅仅是浏览器中的图像
Enter the URL to the JSP file containing the <img>
element in the browser address bar.
<img>
在浏览器地址栏中输入包含该元素的 JSP 文件的 URL 。
http://localhost:8080/contextname/webplugin/jsp/profile/photos.jsp
http://localhost:8080/contextname/webplugin/jsp/profile/photos.jsp
How to avoid above Exception?
如何避免上述异常?
Removethe following lines from the servlet code.
从 servlet 代码中删除以下几行。
//Redirect it to profile page
RequestDispatcher rd = request.getRequestDispatcher
("/webplugin/jsp/profile/photos.jsp");
rd.forward(request, response);
The servlet should just return the image. Nothing more. It's the webbrowser itself who is supposed to download and display the image, not the webserver.
servlet 应该只返回图像。而已。应该下载和显示图像的是网络浏览器本身,而不是网络服务器。
See also:
也可以看看:
回答by Jigar Joshi
1you are modifying response and then forwarding , it is useless. don't do it.
1你是修改响应然后转发,没用。不要这样做。