JSP:新的套接字("www",80);经过多年的努力后停止工作了 [英] JSP: new Socket ("www", 80); stopped working after years of working OK

查看:91
本文介绍了JSP:新的套接字("www",80);经过多年的努力后停止工作了的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Tomcat的JSP应用程序中,以下代码用于产生页面的整个地址(来自此答案):

In a JSP app, in Tomcat, the following code used to produce the whole address of the page (from this answer):

String myUrl = "no network";
try {
    Socket s = new Socket("www", 80);
    myUrl = "http://"+s.getLocalAddress().getHostAddress()+":"+request.getLocalPort()+request.getRequestURI();
    s.close();
} catch (Exception ex) {
} finally {
}

之后,miUrl将具有以下值(不是真正的IP地址):http://111.101.101.2:8080/mypage.jsp

After that miUrl would have the folowing value (not the real IP addr): http://111.101.101.2:8080/mypage.jsp

它已经工作了几年了.

一周前,miUrl开始具有无网络"作为值,表明发生了异常.

A week ago miUrl began having "no network" as value, indicating that an exception happened.

我发布了ex.printStackTrace(),并说:java.net.UnknownHostException: www

创建一个用文字"www" 用来工作的袜子,现在突然停止了工作.

Creating a socked with the literal "www" used to work, now out of a sudden it stopped working.

问题:

  • 它工作多年的技术原因是什么?
  • 它突然停止工作的技术原因是什么?
  • 以编程方式生成任何不易出错的JSP页面的整个地址的最佳方法是什么?

这是一个文件共享应用程序,在用户的工作站中运行,我希望用户能够复制该地址以与他人共享链接,并且http://localhost:8080/downloadpage.jsp(如浏览器的地址字段所示)是不好的共享的.如果您向我展示如何在不使用套接字的情况下获取相同的信息,将会有所帮助.

It's a file-sharing app, running in the users's workstation, I want users to be able to copy the address to share links with others, and http://localhost:8080/downloadpage.jsp (as shown in address field of browser) is no good for sharing. It would help if you show me how to get that same info without the socket hack.

推荐答案

在不使用套接字的情况下解决了IP地址问题.

Solved the IP addr part without using a socket.

public String getIP(){
    String ip="no network";
    try {
        Enumeration<NetworkInterface> n = NetworkInterface.getNetworkInterfaces();
        outmost:
        for (; n.hasMoreElements();)
        {
            NetworkInterface e = n.nextElement();
            Enumeration<InetAddress> a = e.getInetAddresses();
            for (; a.hasMoreElements();)
            {
                InetAddress addr = a.nextElement();             
                if (addr instanceof Inet4Address){ // return the first IPv4 addr (127.0.1.1 is always last)
                    if (addr.isSiteLocalAddress()){
                        ip=addr.getHostAddress();
                        break outmost;
                    }

                }
            }
        }           
    } catch (UnknownHostException e1) {
    } catch (SocketException e) {
    } 
    return ip;      
}

然后

String miUrl = "http://"+getIP()+":"+request.getLocalPort()+request.getRequestURI();

这篇关于JSP:新的套接字("www",80);经过多年的努力后停止工作了的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆