我有一个 java 程序,它根据发送到程序的参数生成一个有效的 BufferedImage bi。我想从一个 servlet 调用这个程序,并让 servlet 向用户发送一个 jsp,其中包含一个适合用户输入参数的图像。我在下面编写的代码会生成一条空消息,而不是请求的图像。
我应该补充一点,get_bi() 有时需要几百毫秒才能在我的开发计算机上生成 BufferedImage。不确定部分问题是否与时间延迟有关。
谁能告诉我如何更改下面的代码,以便它在用户的网络浏览器中输出实际图像而不是空消息?
这是 ImageServlet 的代码:
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import javax.imageio.ImageIO;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletConfig;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
//import org.apache.log4j.Logger;
import myownpackage.GetBI;
public class ImageServlet extends HttpServlet{
// private Logger logger = Logger.getLogger(this.getClass());
private RequestDispatcher jsp;
public void init(ServletConfig config) throws ServletException {
ServletContext context = config.getServletContext();
jsp = context.getRequestDispatcher("/WEB-INF/jsp/send-image-into-html.jsp");
}
protected void doGet(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
// logger.debug("doGet()");
String imageParams = req.getParameter("imageParams");
jsp.forward(req, resp);
}
protected void doPost(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException{
Map<String, String> errors = validate(req);
if (!errors.isEmpty()){
// logger.debug("validation errors");
jsp.forward(req, resp);
return;
}
resp.setContentType("image/gif");
BufferedImage bi = new BufferedImage(300, 300, BufferedImage.TYPE_INT_RGB);
GetBI fetchBI = new GetBI();
bi = fetchBI.get_bi(req.getParameter("imageParams"));
File imgFile = new File("imgFile");
ImageIO.write(bi, "gif", imgFile);
resp.sendRedirect("send-image-into-html");
}
public static Map<String, String> validate(HttpServletRequest req){
HashMap<String, String> errors = new HashMap<String, String>();
req.setAttribute("errors", errors);
String imageParams = req.getParameter("imageParams");
if (imageParams == null || imageParams.trim().length() == 0){
errors.put("imageParams", "imageParams required.");
}
return errors;
}
}
send-image-into-html.jsp 的代码是:
<jsp:useBean id="errors" scope="request" type="java.util.Map" class="java.util.HashMap" />
<%@ include file="top.inc" %>
<%@ include file="middle.inc" %>
<table>
<tr>
<td width=350>
<%=request.getParameter("imgFile")%>
<img src="<%=request.getParameter("imgFile")%>">
</td>
<td>
<form method="post">
<table>
<tr>
<td>Image Parameters: </td>
<td><input type="text" name="imageParams" value="some parameters" size="50" />
<% if (errors.containsKey("imageParams")) {
out.println("<span class=\"error\">" + errors.get("imageParams") + "</span>");
}%>
</td>
</tr>
<tr>
<td>
<input type="submit" name="submit-button" value="Click Here" />
</td>
</tr>
</table>
</form>
</td>
</tr>
</table>
<%@ include file="bottom.inc" %>
编辑:
我已经根据我对 Joop 建议的理解编辑了我的代码,但它仍然不起作用。你能告诉我如何进一步编辑它以使其工作吗?
这是 FirstServlet 的代码:
public class FirstServlet extends HttpServlet{
// private Logger logger = Logger.getLogger(this.getClass());
private RequestDispatcher jsp;
public void init(ServletConfig config) throws ServletException {
ServletContext context = config.getServletContext();
jsp = context.getRequestDispatcher("/WEB-INF/jsp/send-image-into-html.jsp");
}
protected void doGet(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {// logger.debug("doGet()");
jsp.forward(req, resp);
}
protected void doPost(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException{
Map<String, String> errors = validate(req);
if (!errors.isEmpty()){// logger.debug("validation errors");
jsp.forward(req, resp);
return;
}
resp.sendRedirect("/send-image-into-html");
}
public static Map<String, String> validate(HttpServletRequest req){
HashMap<String, String> errors = new HashMap<String, String>();
req.setAttribute("errors", errors);
String imageParam1 = req.getParameter("imageParam1");
if (imageParam1 == null || imageParam1.trim().length() == 0){
errors.put("imageParam1", "imageParam1 required.");
}
return errors;
}
}
这是 SecondServlet 的代码:
public class GetGraphServlet extends HttpServlet{
// private Logger logger = Logger.getLogger(this.getClass());
private RequestDispatcher jsp;
public void init(ServletConfig config) throws ServletException {
ServletContext context = config.getServletContext();
jsp = context.getRequestDispatcher("/WEB-INF/jsp/send-image-into-html.jsp");
}
protected void doGet(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {// logger.debug("doGet()");
// Create the BufferedImage, using any parameters and
// possibly the exact request URI:
String requestURI = req.getRequestURI();
String imageParam1 = req.getParameter("imageParam1");
resp.setContentType("image/gif");//256 colors
BufferedImage bi = new BufferedImage(300, 300, BufferedImage.TYPE_INT_RGB);
GetBI fetchBI = new GetBI();
bi = fetchBI.get_bi(req.getParameter(imageParam1));
ImageIO.write(bi,"gif",resp.getOutputStream());
resp.setContentType("image/gif"); // 256 colors.
ImageIO.write(bi, "gif", resp.getOutputStream());
jsp.forward(req, resp);
}
protected void doPost(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException{}
}
这是 send-image-into-html.jsp 的代码
<jsp:useBean id="errors" scope="request" type="java.util.Map" class="java.util.HashMap" />
<%@ include file="top.inc" %>
<%@ include file="middle.inc" %>
<form method="post">
<table>
<tr>
<td width=350>
<img src="<%="/get-graph.gif?imageParam1="%>">
</td>
<td>
<table>
<tr>
<td>imageParam1: </td>
<td><input type="text" name="imageParam1" value="samplevalue" size="50" />
<%if (errors.containsKey("imageParam1")) {
out.println("<span class=\"error\">" + errors.get("imageParam1") + "</span>");}%>
</td>
</tr>
<tr>
<td>
<input type="submit" name="submit-button" value="CreateImage"/>
</td>
</tr>
</table>
</td>
</tr>
</table>
</form>
<%@ include file="bottom.inc" %>
以下是 web.xml 中的相关标签:
<servlet>
<servlet-name>FirstServlet</servlet-name>
<servlet-class>mypackage.FirstServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>FirstServlet</servlet-name>
<url-pattern>/html-page-url</url-pattern>
</servlet-mapping>
<servlet>
<servlet-name>SecondServlet</servlet-name>
<servlet-class>mypackage.SecondServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>SecondServlet</servlet-name>
<url-pattern>/get-graph</url-pattern>
</servlet-mapping>