JavaWeb学习笔记——开发动态WEB资源(四)打印当前使用的是get方法

时间:2023-03-08 16:17:32
JavaWeb学习笔记——开发动态WEB资源(四)打印当前使用的是get方法

该工程的名称是testhttp,功能是在页面中表格打印浏览过程中的相关头信息。

  新建一个工程,然后在这个工程里面新建一个servlet,这样便可以省去编写web.xml的过程

以下是TestHttpServlet.java中的代码

package org.common.servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.Enumeration; import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse; 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(" <tr><th>名字</th><th>值</th></tr> ");
//返回头消息名字集合,返回的是一个枚举
Enumeration enums = request.getHeaderNames();
//遍历获取所有头信息名和值
while(enums.hasMoreElements()){
//获取每一个头消息的名字
String headName = (String)enums.nextElement();
out.println(" <tr> ");
out.println(" <td> " + headName + " </td> ");
//getHeader(java.lang.String name)
//Returns the value of the specified request header as a String.
//返回这个名字的头信息的值
out.println(" <td> " + request.getHeaders(headName) + " </td> ");
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
} }

然后部署并启动Tomcat服务器,在浏览器中输入http://127.0.0.1:8080/testhttp/servlet/TestHttpServlet