public class TestHttpServlet extends HttpServlet {
/** * Constructor of the object. */ public TestHttpServlet() { super(); }
/** * Destruction of the servlet. <br> */ public void destroy() { super.destroy(); // Just puts "destroy" string in log // Put your code here }
/** * The doGet method of the servlet. <br> * * This method is called when a form has its tag value method equals to get. * * @param request the request send by the client to the server * @param response the response send by the server to the client * @throws ServletException if an error occurred * @throws IOException if an error occurred */ //Servlet接口的参数service(ServletRequest arg0, ServletResponse arg1) //HttpServletRequest封装了所有的请求信息,其实就是Tomcat将请求信息按照JAVA EE的Servlet的规范 //封装好给我们 public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { //设置返回类型 //response.setContentType("text/html;charset=GBK"); response.setContentType("text/html"); response.setCharacterEncoding("GBK"); //获取输出流 PrintWriter out = response.getWriter(); out.println("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\">"); out.println("<HTML>"); out.println(" <HEAD><TITLE>testhttp Servlet</TITLE></HEAD>"); out.println(" <BODY>"); out.print("<center>"); out.print(" <h2>请求头信息列表<h2> "); out.print(" <table border=1> "); out.print(" |名字|值 "); //返回头消息名字集合,返回的是一个枚举 Enumeration enums = request.getHeaderNames(); //遍历获取所有头信息名和值 while(enums.hasMoreElements()){ //获取每一个头消息的名字 String headName = (String)enums.nextElement(); out.println(" <tr> "); out.println(" | " + headName + " "); //getHeader(java.lang.String name) //Returns the value of the specified request header as a String. //返回这个名字的头信息的值 out.println(" | " + request.getHeaders(headName) + " "); out.println(" </tr> "); } out.println(" </table> "); out.println(" <hr> "); //测试HttpServletRequest的方法 out.println("Method: " + request.getMethod() + "<br>"); out.println("Request URI: " + request.getRequestURI() + "<br>"); out.println("Protocol: " + request.getProtocol() + "<br>"); out.println("PathInfo: " + request.getPathInfo() + "<br>"); out.println("Remote Address: " + request.getRemoteAddr() + "<br>"); out.println("ContextPath: " + request.getContextPath() + "<br>"); out.println("getScheme: " + request.getScheme() + "<br>"); out.println("getServerName: " + request.getServerName() + "<br>"); out.println("getServerPort: " + request.getServerPort() + "<br>"); out.println("getRequestURI: " + request.getRequestURI() + "<br>"); String path = request.getContextPath(); //请求全路径 String basePath = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort() + request.getRequestURI(); out.println(" path: " + path + "<br>"); out.println(" basePath: " + basePath + "<br>"); //out.print(this.getClass()); //out.println(", using the GET method"); out.print("</center>"); out.println(" </BODY>"); out.println("</HTML>"); out.flush(); out.close(); }
/** * The doPost method of the servlet. <br> * * This method is called when a form has its tag value method equals to post. * * @param request the request send by the client to the server * @param response the response send by the server to the client * @throws ServletException if an error occurred * @throws IOException if an error occurred */ public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { //调用doGet方法 this.doGet(request, response); }
/** * Initialization of the servlet. <br> * * @throws ServletException if an error occurs */ public void init() throws ServletException { // Put your code here }