码迷,mamicode.com
首页 > Web开发 > 详细

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

时间:2018-02-19 21:13:01      阅读:244      评论:0      收藏:0      [点我收藏+]

标签:util   ext   tab   alt   info   names   next   并且   页面   

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中

标签:util   ext   tab   alt   info   names   next   并且   页面   

原文地址:https://www.cnblogs.com/sinceForever/p/8454392.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!