java 取得http报头信息

假设你的web项目为tomcat安装目录/webapps/test/,在src下面新建java文件:[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();

}
}[/java]
然后进入其所在目录,编译:[shell]
javac OutpunInfo.java[/shell]
将编译好的OutpunInfo.class文件复制到WEB-INF下面的class文件夹,然后编辑web.xml文件:[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>[/xml]
然后运行:[shell]
http://localhost:8080/test/info[/shell]
即可看到相关信息
其实当你熟悉之后,你可以使用IDE建立web项目,例如使用强大的netbeans建立web项目

Avatar photo

About Blackford

这是个最好的时代,这是个最坏的时代,这是个充满希望的春天,这是个令人绝望的冬天,我们前面什么都有,我们前面什么都没有。梦想,让我们一次次的走远,又一次次的回头,一个关于人生的梦想还在不断奔跑,带着喜悦和疼痛,不过一切才刚刚开始,并且直到今天也远远没有结束
This entry was posted in Java编程语言 and tagged , , . Bookmark the permalink.

发表评论

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