如何使用Servlet在其Response中显示一个JSP页面? [英] How to show a JSP page using Servlet which has image in its Response?
问题描述
Servlet doGet()代码,用于从数据库获取图像并在响应中存储图像
Servlet doGet() code for getting an Image from Database and to store image in Response
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();
}
}
但是,当这个servlet显示JSP页面显示仅图像
而不是JSP页面。
However, When this servlet shows the JSP page it shows only image
and not the JSP page.
JSP代码:
... JSP code
<img src="Servlet url">
... JSP code cont...
我得到了什么输出:
- 我只在JSP中获取Image而不是图像
- 当我使用RequestDispatcher / sendRedirect时( )我得到以下异常
java.lang.IllegalStateException:响应提交后无法转发
- 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中获取图像而不仅仅是浏览器中的图像
- 如何避免上述异常?
编辑:我的Web.xml看起来像这样
My Web.xml looks like this
<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>
推荐答案
如何在JSP中获取Image而不仅仅是在浏览器中使用Image
输入包含<$ c的JSP文件的URL浏览器地址栏中的$ c>< img> 元素。
http:// localhost:8080 / contextname / webplugin / jsp / profile / photos.jsp
如何避免上述异常?
从servlet代码中删除以下行。
//Redirect it to profile page
RequestDispatcher rd = request.getRequestDispatcher
("/webplugin/jsp/profile/photos.jsp");
rd.forward(request, response);
servlet应该只返回图像。而已。 Web浏览器本身应该下载并显示图像,而不是网络服务器。
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.
- How to retrieve image from database and display in JSP via Servlet?
- How to retrieve and display images from a database in a JSP page?
这篇关于如何使用Servlet在其Response中显示一个JSP页面?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!