java 取得http报头信息

  • 内容
  • 评论
  • 相关

假设你的web项目为tomcat安装目录/webapps/test/,在src下面新建java文件:

import javax.servlet.*;
import java.io.*;
import javax.servlet.http.*;
import java.util.*;

public class OutputInfo extends HttpServlet{
    public void doGet(HttpServletRequest req, HttpServletResponse resp)
            throws ServletException,IOException{
                resp.setContentType("text/html;charset=gb2312");
                PrintWriter out = resp.getWriter();
                Enumeration headName = req.getHeaderNames();
                out.println("<html><head>");
                out.println("<title>Info Page</title>");
                out.println("</head>");
                out.println("<body><center>");
                out.println("<table border=1 align=center>");
                out.println("<caption>Servlet 接受到的HTTP报头信息</caption>");
                out.println("<tr bgcolor=#999999>");
                out.println("<th>消息报头名字</th>");
                out.println("<th>消息报头值</th>");
                out.println("</tr>");
                while (headName.hasMoreElements()) {
                    String name = (String)headName.nextElement();
                    String value = req.getHeader(name);
                    out.println("<tr>");
                    out.println("<td>"+name+"</td>");
                    out.println("<td>"+value+"</td>");
                    out.println("</tr>");
                }
                out.println("</table><p>");
                
                out.println("<table border=1 align=center>");
                out.println("<caption>其它访问信息</caption>");
                
                out.println("<tr>");
                out.println("<td>客户端IP</td>");
                out.println("<td>"+req.getRemoteAddr()+"</td>");
                out.println("</tr>");
                
                out.println("<tr>");
                out.println("<td>服务端IP</td>");
                out.println("<td>"+req.getLocalAddr()+"</td>");
                out.println("</tr>");
                
                out.println("<tr>");
                out.println("<td>客户端端口号</td>");
                out.println("<td>"+req.getRemotePort()+"</td>");
                out.println("</tr>");
                
                out.println("<tr>");
                out.println("<td>服务端端口</td>");
                out.println("<td>"+req.getLocalPort()+"</td>");
                out.println("</tr>");
                
                out.println("</table>");
                out.println("</center></body></html>");
                out.close();
        
    }
}

然后进入其所在目录,编译:

javac OutpunInfo.java

将编译好的OutpunInfo.class文件复制到WEB-INF下面的class文件夹,然后编辑web.xml文件:

<?xml version="1.0" encoding="ISO-8859-1"?>

<web-app xmlns="http://java.sun.com/xml/ns/j2ee"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"
    version="2.4">

    <servlet>
        <servlet-name>TestName</servlet-name>
        <servlet-class>test</servlet-class>
    </servlet>
    <servlet>
        <servlet-name>FormName</servlet-name>
        <servlet-class>Form</servlet-class>
    </servlet>
    <servlet>
        <servlet-name>OutputInfo</servlet-name>
        <servlet-class>OutputInfo</servlet-class>
    </servlet>
    <servlet-mapping>
        <servlet-name>TestName</servlet-name>
        <url-pattern>/myweb</url-pattern>
    </servlet-mapping>
    <servlet-mapping>
        <servlet-name>FormName</servlet-name>
        <url-pattern>/forTest</url-pattern>
    </servlet-mapping>
    <servlet-mapping>
        <servlet-name>OutputInfo</servlet-name>
        <url-pattern>/info</url-pattern>
    </servlet-mapping>
</web-app>

然后运行:

http://localhost:8080/test/info

即可看到相关信息
其实当你熟悉之后,你可以使用IDE建立web项目,例如使用强大的netbeans建立web项目

评论

0条评论

发表评论

您的电子邮箱地址不会被公开。 必填项已用*标注