我目前正在研究带有servlet的Java EE应用程序的基础知识。我已经在我只能看到的本地主机上建立了与Servlet的连接:
“服务于:/样品”
但是,我希望显示的是
“这是一个测试”
由以下代码定义:
package com.javaservletsample;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
* Servlet implementation class MyServlet
*/
@WebServlet("/MyServlet")
public class MyServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
public MyServlet() {
super();
// TODO Auto-generated constructor stub
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doPost(request, response);
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
response.setContentType("text/html");
PrintWriter out = response.getWriter();
try {
out.println("<html> <head> <title>This is a test</title> </head> </html>");
}finally {out.close();}
}
}
请注意底部的try{}
语句,在此我表示html的println语句。
为什么不显示该html?
=> 编辑-我意识到 body 不会显示“ This is a test”,而是标题。尽管如此,标题仍然不正确,问题仍然存在。