MyEclipse 通过浏览器显示主机和网络信息的小程序

来源:互联网 发布:千牛怎么装修淘宝店铺 编辑:程序博客网 时间:2024/04/29 14:23

项目名:ServletII

Servlet类名:RequestServlet.java   和  IpUtil.java


RequestServlet.java:

package com.helloweenvsfei.servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.security.Principal;
import java.util.Locale;
import com.helloweenvsfei.util.IpUtil;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class RequestServlet extends HttpServlet {

    

    private String getAccept(String accept) {
        StringBuffer buffer = new StringBuffer();
        if (accept.contains("image/gif"))
            buffer.append("GIF 文件,");
        if (accept.contains("image/x-xbitmap"))
            buffer.append("BMP 文件,");
        if (accept.contains("image/jpeg"))
            buffer.append("JPG 文件,");
        if (accept.contains("application/vnd.ms-excel"))
            buffer.append("EXCEL 文件,");
        if (accept.contains("application/vnd.ms-powerpoint"))
            buffer.append("PPT 文件,");
        if (accept.contains("application/msword"))
            buffer.append("Word 文件,");

        return buffer.toString().replace(", $", "");
    }

    private String getLocale(Locale locale) {
        if (Locale.SIMPLIFIED_CHINESE.equals(locale))
            return "简体中文";
        if (Locale.TRADITIONAL_CHINESE.equals(locale))
            return "繁体中文";
        if (Locale.ENGLISH.equals(locale))
            return "英文";
        if (Locale.JAPANESE.equals(locale))
            return "日文";

        return "未知语言环境";
    }

    private String getAddress(String ip) {
        return IpUtil.getHostName(ip);
    }

    private String getNavigator(String userAgent) {
        if (userAgent.indexOf("TencentTraveler") > 0)
            return "腾讯浏览器";
        if (userAgent.indexOf("Maxthon") > 0)
            return "Maxthon浏览器";
        if (userAgent.indexOf("MyIE2)") > 0)
            return "MyIE2浏览器";
        if (userAgent.indexOf("Firefox") > 0)
            return "Firefox浏览器";
        if (userAgent.indexOf("MSIE") > 0)
            return "IE浏览器";

        return "未知浏览器";

    }

    private String getOS(String userAgent) {
        if (userAgent.indexOf("Windows NT 5.1") > 0)
            return "Windows XP";
        if (userAgent.indexOf("Windows 98") > 0)
            return "Windows 98";
        if (userAgent.indexOf("Windows 2000") > 0)
            return "Windows 2000";
        if (userAgent.indexOf("Linux") > 0)
            return "Linux";
        if (userAgent.indexOf("Unix") > 0)
            return "Unix";

        return "未知";
    }

    /**
     * Constructor of the object.
     */
    public RequestServlet() {
        super();
    }

    /**
     * Destruction of the servlet. <br>
     */
    public void destroy() {
        super.destroy(); // Just puts "destroy" string in log
        // Put your code here
    }

    /**
     * The doGet method of the servlet. <br>
     *
     * This method is called when a form has its tag value method equals to get.
     *
     * @param request
     *            the request send by the client to the server
     * @param response
     *            the response send by the server to the client
     * @throws ServletException
     *             if an error occurred
     * @throws IOException
     *             if an error occurred
     */
    public void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {

        request.setCharacterEncoding("UTF-8");
        response.setCharacterEncoding("UTF-8");

        response.setContentType("text/html");
        String autoType = request.getAuthType();
        String localAddr = request.getLocalAddr();
        String localName = request.getLocalName();
        int localPort = request.getLocalPort();
        Locale locale = request.getLocale();
        String contextpath = request.getContextPath();
        String method = request.getMethod();
        String pathinfo = request.getPathInfo();
        String pathtranslated = request.getPathTranslated();
        String protocol = request.getProtocol();
        String querystring = request.getQueryString();
        String remoteaddr = request.getRemoteAddr();
        int port = request.getRemotePort();
        String remoteuser = request.getRemoteUser();
        String requestsessionid = request.getRequestedSessionId();
        String requestURI = request.getRequestURI();
        StringBuffer requestURL = request.getRequestURL();
        String scheme = request.getScheme();
        String servername = request.getServerName();
        int serverport = request.getServerPort();
        String servletpath = request.getServletPath();
        Principal principal = request.getUserPrincipal();
        String accept = request.getHeader("accept");
        String referer = request.getHeader("referer");
        String userAgent = request.getHeader("user-agent");

        String serverInfo = this.getServletContext().getServerInfo();

        PrintWriter out = response.getWriter();
        out
                .println("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\">");
        out.println("<HTML>");
        out.println(" <HEAD><TITLE>RequestServlet</TITLE></HEAD>");
        out
                .println(" <style>body, font, td, div{font-size:12px; line-height:18px; }</style>");
        out.println(" <BODY>");
        out.println("<b>您的IP为</b>" + remoteaddr + "<b>,位于</b>"
                + getAddress(remoteaddr) + "<b>;您使用</b>" + getOS(userAgent)
//                +"AAA"+"<b>;您使用</b>" + getOS(userAgent)
                + "<b>操作系统</b>," + getNavigator(userAgent) + "<b>.您使用</b>"
                + getLocale(locale) + ".<br/>");

        out.println("<b>服务器IP为</b>" + localAddr + "<b>,位于</b>"
                + getAddress(localAddr) + "<b>,服务器使用</b>" + serverport
//                + "AAA" + "<b>,服务器使用</b>" + serverport
                + "<b>端口,您的服务器使用了</b>" + port + "<b>端口访问本网页。</b><br/>");

        out.println("<b>服务器软件为</b>" + serverInfo + ".<b>服务器名称为</b>" + localName
                + ".<br/>");

        out.println("<b>您的浏览器接受</b>" + getAccept(accept) + ".<br/>");
        out.println("<b>您从</b>" + referer + "<b>访问到该页面.</b><br/>");
        out.println("<b>使用的协议为</b>" + protocol + ".<b>URL协议头</b>" + scheme
                + ",<b>服务器名称</b>" + servername + ",<b>您访问的URI为</b>"
                + requestURI + ".<br/>");
        out.println("<b>该Servlet路径为</b>" + servletpath + ",<b>该Servlet类名为</b>"
                + this.getClass().getName() + ".<br/>");

        out.println("本应用程序在硬盘的根目录为</b>"
                + this.getServletContext().getRealPath("") + ",<b>网络相对路径为</b>"
                + contextpath + "<br/>");
        
        out.println("<br/>");
        out.println("<br/><br/><a href="+requestURI+">单击刷新本页面</a>");
        out.println(" </BODY>");
        out.println(" </HTML>");
        out.flush();
        out.close();
    }

    /**
     * The doPost method of the servlet. <br>
     *
     * This method is called when a form has its tag value method equals to
     * post.
     *
     * @param request
     *            the request send by the client to the server
     * @param response
     *            the response send by the server to the client
     * @throws ServletException
     *             if an error occurred
     * @throws IOException
     *             if an error occurred
     */
    public void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {

        response.setContentType("text/html");
        PrintWriter out = response.getWriter();
        out
                .println("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\">");
        out.println("<HTML>");
        out.println("  <HEAD><TITLE>A Servlet</TITLE></HEAD>");
        out.println("  <BODY>");
        out.print("    This is ");
        out.print(this.getClass());
        out.println(", using the POST method");
        out.println("  </BODY>");
        out.println("</HTML>");
        out.flush();
        out.close();
    }

    /**
     * Initialization of the servlet. <br>
     *
     * @throws ServletException
     *             if an error occurs
     */
    public void init() throws ServletException {
        // Put your code here
    }

}

IpUtil.java:

package com.helloweenvsfei.util;

import java.net.InetAddress;
import java.net.UnknownHostException;

public class IpUtil {

    /**
     * 非法IP地址常量。
     *
     * @since 0.12
     */
    public static final String INVALID_IP = "0.0.0.0";
    /**
     * 未知主机名常量。
     *
     * @since 0.12
     */
    public static final String UNKNOWN_HOST = "";

    /**
     * 私有构造方法,防止类的实例化,因为工具类不需要实例化。
     * @return
     */
    private void IPUtil() {
    }

    /**
     * 根据主机名得到IP地址字符串。
     *
     * @param hostName
     *            要查找地址的主机名
     * @return 对应主机的IP地址,主机名未知或者非法时返回INVALID_IP。
     */
    public static String getByName(String hostName) {
        try {
            InetAddress inet = InetAddress.getByName(hostName);
            return inet.getHostAddress();
        } catch (UnknownHostException e) {
            return INVALID_IP;
        }
    }

    /**
     * 根据IP地址得到主机名。
     *
     * @param ip
     *            要查找主界面的IP地址
     * @return 对应IP的主机名,IP地址未知时返回UNKNOWN_HOST,IP地址未知也可能是网络问题造成的。
     */
    public static String getHostName(String ip) {
        try {
            InetAddress inet = InetAddress.getByName(ip);
            return inet.getHostName();
        } catch (UnknownHostException e) {
            return UNKNOWN_HOST;
        }
    }
}


Web.xml:

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee"
 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee   http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
 <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>RequestServlet</servlet-name>
  <servlet-class>com.helloweenvsfei.servlet.RequestServlet</servlet-class>
  <load-on-startup>-1</load-on-startup>
 </servlet>
 <servlet-mapping>
  <servlet-name>RequestServlet</servlet-name>
  <url-pattern>/servlet/RequestServlet</url-pattern>
 </servlet-mapping>
 <welcome-file-list>
  <welcome-file>index.jsp</welcome-file>
 </welcome-file-list>
 <login-config>
  <auth-method>BASIC</auth-method>
 </login-config>
</web-app>





原创粉丝点击