2016-03-20 51 views
0

我想我的JSP頁面:由JSP頁面調用servlet的返回IllegalStateException異常

<%@page contentType="text/html" pageEncoding="windows-1250"%> 
<%@page import="java.util.Random"%> 
<%@page import = "java.util.*" session="true"%> 

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" 
    "http://www.w3.org/TR/html4/loose.dtd"> 
<html> 
    <head> 
     <meta http-equiv="Content-Type" content="text/html; charset=windows- 
       1250"> 
     <title>JSP Page</title> 
    </head> 
    <body> 
     <% 
      String text = "text"; 
     %> 

     <jsp:include page="header.jsp"> 
      <jsp:param name="test" value="<%=text%>"/> 
     </jsp:include> 
    </body> 
</html> 

調用header.jsp中與參數 「測試」 引起了:

<%@page contentType="text/html" pageEncoding="windows-1250"%> 
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" 
    "http://www.w3.org/TR/html4/loose.dtd"> 
<table bgcolor="#aaaaaa"> 
    <tr> 
     <td> 
      <h1>Image: 
       <br> 
       <jsp:include page="/ServletExample"/> 
      </h1> 
     </td> 
    </tr> 
</table> 

這調用一個servlet:

@WebServlet(name = "ServletExample", urlPatterns = {"/ServletExample"}) 
public class ServletExample extends HttpServlet { 

    /** 
    * Processes requests for both HTTP <code>GET</code> and <code>POST</code> 
    * methods. 
    * 
    * @param request servlet request 
    * @param response servlet response 
    * @throws ServletException if a servlet-specific error occurs 
    * @throws IOException if an I/O error occurs 
    */ 
    protected void processRequest(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { 

     request.setCharacterEncoding("UTF-8"); 

     BufferedImage graph = new BufferedImage(500, 500, BufferedImage.TYPE_INT_ARGB); 
     Graphics2D g2d = (Graphics2D) graph.getGraphics(); 
     g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); 
     g2d.setColor(Color.ORANGE); 
     String text = request.getParameter("test")== null ? "no text" : request.getParameter("text"); 
     g2d.drawString(text, 100, 100); 

     OutputStream out; 
     response.setContentType("image/png"); 
     try { 
      ByteArrayOutputStream buffer = new ByteArrayOutputStream(); 
      ImageIO.write(graph, "PNG", buffer); 
      response.setContentLength(buffer.size()); 
      out = response.getOutputStream(); 
      out.write(buffer.toByteArray()); 
     } catch (Exception ex) { 
      throw new ServletException(ex); 
     } 
     out.close(); 
    } 

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code."> 
    /** 
    * Handles the HTTP <code>GET</code> method. 
    * 
    * @param request servlet request 
    * @param response servlet response 
    * @throws ServletException if a servlet-specific error occurs 
    * @throws IOException if an I/O error occurs 
    */ 
    @Override 
    protected void doGet(HttpServletRequest request, HttpServletResponse response) 
      throws ServletException, IOException { 
     processRequest(request, response); 
    } 

    /** 
    * Handles the HTTP <code>POST</code> method. 
    * 
    * @param request servlet request 
    * @param response servlet response 
    * @throws ServletException if a servlet-specific error occurs 
    * @throws IOException if an I/O error occurs 
    */ 
    @Override 
    protected void doPost(HttpServletRequest request, HttpServletResponse response) 
      throws ServletException, IOException { 
     processRequest(request, response); 
    } 

    /** 
    * Returns a short description of the servlet. 
    * 
    * @return a String containing servlet description 
    */ 
    @Override 
    public String getServletInfo() { 
     return "Short description"; 
    }// </editor-fold> 

} 

但在行「out = response.getOutputStream();」拋出異常:

Warning: Servlet.service() for servlet jsp threw exception 
java.lang.IllegalStateException 
    at org.apache.jasper.runtime.ServletResponseWrapperInclude.getOutputStream(ServletResponseWrapperInclude.java:124) 
    at example.ServletExample.processRequest(ServletExample.java:59) 
    at example.ServletExample.doGet(ServletExample.java:79) 
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:687) 
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:790) 
    at org.apache.catalina.core.StandardWrapper.service(StandardWrapper.java:1682) 
    at org.apache.catalina.core.ApplicationDispatcher.doInvoke(ApplicationDispatcher.java:875) 
    at org.apache.catalina.core.ApplicationDispatcher.invoke(ApplicationDispatcher.java:739) 
    at org.apache.catalina.core.ApplicationDispatcher.doInclude(ApplicationDispatcher.java:695) 

我試圖設置flush(),但它不起作用。你遇到過類似的問題?

+0

我要補充的是,當我直接調用servlet(無副作用JSP)的servlet,返回我與文字爲png「無文本」 –

+1

使用''顯示圖像,而不是''。重複內容解釋了確切的過程(用「Java2D」替換「數據庫」)。 – BalusC

回答

0

text將在那裏test

String text = request.getParameter("test")== null ? "no text" : request.getParameter("test");