因此我被要求创建一个读取ascii文件的servlet,并将输出发送到调用servlet的浏览器。
我已经开始这个但最后我不知道上面究竟是什么意思......
以下是我所拥有的,有人可以告诉我,如果我所做的是什么请求吗?如果不是我需要做什么来完成我的servlet?
public class MessageServlet extends HttpServlet {
public void doGet(HttpServletRequest req, HttpServletResponse res)
throws IOException, ServletException
{
// output an HTML page
res.setContentType("text/html");
// load a configuration parameter (you must set this yourself)
String root = getInitParameter("root");
// print some html
ServletOutputStream out = res.getOutputStream();
out.println("<html>");
out.println("<head><title>Bryan Rigsbee's Page</title></head>");
out.println("<body><h1>My Test Servlet for Understanding</h1>");
// print the file
InputStream in = null;
try {
in = new BufferedInputStream
(new FileInputStream(root + "/message.txt") );
int ch;
while ((ch = in.read()) !=-1) {
out.print((char)ch);
}
}
finally {
if (in != null) in.close(); // very important
}
// finish up
out.println("</body></html>");
}
}