2014年8月10日 星期日

Java取得Host相關資料


    public String getHostname() {
        String localhost=((HttpServletRequest)FacesContext.getCurrentInstance().getExternalContext().getRequest()).getRemoteHost();
        hostname=localhost.toUpperCase();
//僅取主機名      
        int pos = hostname.indexOf(".");                
        hostname=hostname.substring(0, pos);
        return hostname;
    }

    public String getHostIP() {
        String clientIpAddress =      
             ((HttpServletRequest)FacesContext.getCurrentInstance().getExternalContext().getRequest()).getRemoteAddr(); 
        hostIP=clientIpAddress;
        return hostIP;
    }

Set Session

userSession.setAttribute("EmpID", UserName);