0

I would like to my JSP page:

<%@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>

invoked header.jsp with the parameter "test" aroused:

<%@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>

and this invoked a 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>

}

but in line "out = response.getOutputStream();" throw exception:

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)

I tried to set the flush () but it does not work. You met a similar problem?

LLL RRR
  • 189
  • 13
  • I will add that when I call the servlet directly (without side jsp) servlet that returns me to png with the text "no text" – LLL RRR Mar 20 '16 at 18:47
  • 1
    Use `` to display images, not ``. The duplicate explains the exact process (just substitute "Database" with "Java2D"). – BalusC Mar 20 '16 at 18:59

1 Answers1

0

text will be test there.

String text = request.getParameter("test")== null  ? "no text" : request.getParameter("test");
SkyWalker
  • 28,384
  • 14
  • 74
  • 132