struts中response问题

04-11-03 songqian

在Struts下用response出现下列提示:说是不能forward

StandardWrapperValve[action]: Servlet.service() for servlet action threw exception

java.lang.IllegalStateException: Cannot forward after response has been committed

at org.apache.catalina.core.ApplicationDispatcher.doForward(ApplicationDispatcher.java:367)

at org.apache.catalina.core.ApplicationDispatcher.forward(ApplicationDispatcher.java:355)

at org.apache.struts.action.ActionServlet.processActionForward(ActionServlet.java:1759)

at org.apache.struts.action.ActionServlet.process(ActionServlet.java:1596)

at org.apache.struts.action.ActionServlet.doGet(ActionServlet.java:492)

at javax.servlet.http.HttpServlet.service(HttpServlet.java:740)

。。。。。。。。。。。。。。。。。。。。。。。。。。。。。

我把相关代码贴出来,请大家帮我分析分析原因。程序中唯一用到response是在printmap方法中,作用是打出图片,我单步运行看过程序能走到return mapping.findForward("success")处,并且也没进入catch中。不知道是什么原因出现上述错误,急盼答复,多谢

 public ActionForward perform(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response)throws java.io.IOException,javax.servlet.ServletException{

    try{
             String rqutype = request.getParameter("rqutype");
              if(rqutype != null && rqutype.equals("initmap"))
               {
                   mymap = initmap(request);
                   printmap(mymap, response);//打印地图
               } else。。。。。 
     return mapping.findForward("success");
   }catch(Exception e){
   //  e.printStackTrace();
     request.setAttribute("returnedErrors",e.getMessage());
   }
   return new ActionForward(mapping.getInput());
  }


    private void printmap(MapJ mymap, HttpServletResponse response)
    {
        ServletOutputStream sout;
        sout = null;
        try{
        mymap.setDeviceBounds(new DoubleRect(0.0D, 0.0D, 480,360));
        response.setContentType("image/jpeg");
        sout = response.getOutputStream();//********************用到response***********
        ImageRequestComposer irc = ImageRequestComposer.create(mymap, 65535, Color.white, "image/jpeg");
        MapXtremeImageRenderer renderer = new MapXtremeImageRenderer("http://localhost:8088/mapxtreme47/servlet/mapxtreme");
        renderer.render(irc);
        renderer.toStream(sout);//***************打印图片*****************
      }catch( Exception e){
        e.printStackTrace();}
        try
        {
            if(sout != null)
                sout.close();
        }
        catch(Exception exception) { }
    }

}

<p>

wildfox
2004-11-03 15:58

"Cannot forward after response has been committed "

你的printmap方法已经在response中输出了数据到browser,当然不能再forward 了。

songqian
2004-11-05 13:47

这位大侠,请指点一下,那应该怎么改?

songqian
2004-11-05 13:49

我既需要在browser中打印图片,又想能转发,有办法解决嘛?