web03-OutputInfo
电影网站:www.aikan66.com
项目网站:www.aikan66.com
游戏网站:www.aikan66.com
图片网站:www.aikan66.com
书籍网站:www.aikan66.com
学习网站:www.aikan66.com
Java网站:www.aikan66.com
iOS网站:www.aikan66.com
新建web项目,web03-OutputInfo
新建servlet,OutputInfo.java
其内容为:、
public void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { resp.setContentType("text/html;charset=utf-8"); PrintWriter out=resp.getWriter(); Enumeration headNames=req.getHeaderNames(); out.println("<center>"); out.println("<table border=1 align=center>"); out.println("<caption>Servelet接收到的Http消息报头的信息</caption"); out.println("<tr bgcolor=#999999"); out.println("<th>消息报头的名字</th>"); out.println("<th>消息报头的值</th>"); out.println("</tr>"); while(headNames.hasMoreElements()) { String name=(String)headNames.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>客户端的端口号</td>"); out.println("<td>"+req.getRemotePort()+"</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.getLocalPort()+"</td>"); out.println("</tr>"); out.println("</table>"); out.println("</center></body></html>"); out.close(); }
----
配置web.xml
为:
<servlet> <description>This is the description of my J2EE component</description> <display-name>This is the display name of my J2EE component</display-name> <servlet-name>OutputInfo</servlet-name> <servlet-class>OutputInfo</servlet-class> </servlet> <servlet-mapping> <servlet-name>OutputInfo</servlet-name> <url-pattern>/OutputInfo</url-pattern> </servlet-mapping>
----
在浏览器中访问:http://localhost:8080/web03-outPutInfo/OutputInfo
结果为: