ResquestInfoServlet类通过访问HttpServletRequest对象的各种方法来读取HTTP请求中的特定信息,并且把它们写入到HTML中

ResquestInfoServlet类通过访问HttpServletRequest对象的各种方法来读取HTTP请求中的特定信息,并且把它们写入到HTML中

ResquestInfoServlet.java

package com.demo.servlet;

import com.sun.xml.internal.ws.policy.privateutil.PolicyUtils;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Enumeration;

/**
 * Created by 谭雪娇 on 2017/4/5.
 */
public class RequestInfoServlet extends HttpServlet {
    /*响应客户请求*/
    public void doGet(HttpServletRequest request, HttpServletResponse response)throws IOException,ServletException {
        //设置HTTP响应的正文的类型
        response.setContentType("text/html;charset=GB2312");
        /*输出html文档*/
        PrintWriter out=response.getWriter();
        out.println("<html><head><title>RequestInfo</title></head>");
        out.println("<body>");
        out.println("<br>LocalAddr:"+request.getLocalAddr());
        out.println("<br>LocalName:"+request.getLocalName());
        out.println("<br>LocalPort:"+request.getLocalPort());
        out.println("<br>Protocol:"+request.getProtocol());
        out.println("<br>RemoteAddr:"+request.getRemoteAddr());
        out.println("<br>RemoteHost:"+request.getRemoteHost());
        out.println("<br>RemotePort:"+request.getRemotePort());
        out.println("<br>Method:"+request.getMethod());
        out.println("<br>URI:"+request.getRequestURI());
        out.println("<br>ContextPath:"+request.getContextPath());
        out.println("<br>QueryString:"+request.getQueryString());
        /*打印HTTP请求头*/
        out.println("<br>***打印HTTP请求头***");
        Enumeration eu=request.getHeaderNames();
        while(eu.hasMoreElements()){
            String headerName=(String)eu.nextElement();
            out.println("<br>"+headerName+":"+request.getHeader(headerName));
        }
        out.println("<br>***打印请求头结束***<br>");
        out.println("<br>username :"+request.getParameter("username"));
        out.println("</body></html>");
        //关闭输入流
        out.close();

}
}

web.xml

<servlet>
    <servlet-name>dispatcher</servlet-name>
    <servlet-class>com.demo.servlet.RequestInfoServlet</servlet-class>

</servlet>
<servlet-mapping>
    <servlet-name>dispatcher</servlet-name>
    <url-pattern>/info</url-pattern>
</servlet-mapping>

ResquestInfoServlet生成的HTML页面

ResquestInfoServlet类通过访问HttpServletRequest对象的各种方法来读取HTTP请求中的特定信息,并且把它们写入到HTML中

上一篇:个股与指数的回归分析(自带python ols 参数解读)


下一篇:Http 请求头包含哪些信息?