我想访问我的JSP页面:
<%@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>
使用参数&#34; test&#34;调用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>
}
但在行#34; out = response.getOutputStream();&#34;抛出异常:
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 :(得分:0)
text
将test
在那里。
String text = request.getParameter("test")== null ? "no text" : request.getParameter("test");