【发布时间】:2011-08-10 09:55:09
【问题描述】:
public class DemoServlet extends HttpServlet {
public void service(HttpServletRequest req, HttpServletResponse resp)
throws IOException, ServletException {
//prints out my string
resp.getOutputStream().write("Hello from servlet\n".getBytes());
String variable ="VAR";
//trying to print out variable by this way but doesn't work
resp.getOutputStream().write("%s\n".getBytes(),variable);
//doesn't work this way either
resp.getOutputStream().write("variable is:"+ variable +"something else\n".getBytes());
}
}
首先,我使用PageWriter out= resp.getWriter();,但后来我切换到ServletOutputStream,因为我想打印图像。其他一切都很好,但是:
public void makedbconnection() {
try {
Class.forName("com.mysql.jdbc.Driver").newInstance();
Dbcon = DriverManager.getConnection("jdbc:mysql://localhost/test");
} catch(Exception idc) {
//ON THIS LINE, out is ServletOutputStream.
idc.printStackTrace(out);
}
//System.out.println("connection made");
}
【问题讨论】:
-
请下次解释您对文本的问题,而不仅仅是代码。此外,当您对答案感到满意时,接受它。谢谢!