连接到Tomcat上的套接字? [英] Connecting to socket on Tomcat?

查看:108
本文介绍了连接到Tomcat上的套接字?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试从独立的applet连接到运行在Tomcat上的servlet:

I'm trying to connect from a standalone applet to a servlet running on tomcat:

Servlet

public void init(ServletConfig config) throws ServletException { 
    super.init(config);
    // Start a daemon thread 
    try { 
        daemonThread = new Daemon(this);
        daemonThread.start();
    }
    catch (Exception e) { 
    }
}

protected int getSocketPort() { 
    return 8080;
}

public void handleClient(Socket client){ 
    new ScribbleThread(this, client).start();
}

protected void doGet(HttpServletRequest request, HttpServletResponse response) 
throws ServletException, IOException {
    PrintWriter out = response.getWriter();
    StringBuffer sb = new StringBuffer();
    sb.append("<html><body bgcolor=pink text=red>");
    sb.append("<h1 align=center>RUNNING</h1><hr>");
    sb.append("</body></html>");
    out.println(sb);
    out.close();
}
}

Servlet的 init()创建以下内容:

Servlet's init() creates this:

class Daemon extends Thread { 
private ServerSocket serverSocket;
private SocketServlet servlet;

public Daemon(SocketServlet servlet) { 
this.servlet = servlet;
}

public void run() { 
    try { 
        // Create a server socket to accept connections 
        serverSocket = new ServerSocket(servlet.getSocketPort());
    }
    catch (Exception e) { 
        return;
    }
    try { 
        while (true) { 
            try { 
                servlet.handleClient(serverSocket.accept());
            }
            catch (IOException ioe) { 
            }
        }
    }

我通过Eclipse将它部署到TomCat。我的问题是我的applet要将套接字插入哪个地址?当我访问http:// localhost:8080 / scrabServ / connect 时,我从 doGet()是它需要指向的地方吗?

I have this deployed by eclipse to TomCat. My question is what address does my applet need to make the socket to? When i visit http://localhost:8080/scrabServ/connect I see the 'RUNNING' message from the doGet() so is that where it needs to point?

Applet:

public static String testConnection(){
    InputStream in = null; 
    try { 
        // Make socket connection to servlet
        String servlet = new String("localhost/scrabServ/connect");
        Socket socket = new Socket(servlet, 8080); 

这给了我:

Exception in testConnection()java.net.UnknownHostException: localhost/scrabServ/connect
at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:195)
at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:432)
at java.net.Socket.connect(Socket.java:529)
at java.net.Socket.connect(Socket.java:478)
at java.net.Socket.<init>(Socket.java:375)
at java.net.Socket.<init>(Socket.java:189)
at scribble.Scribble.testConnection(Scribble.java:41)
at scribble.Scribble.main(Scribble.java:28)

并指向 new Socket(servlet,8080)行。

推荐答案

您只需打开一个套接字,即可连接到 localhost,8080 ,然后发出 GET scrabServ / connect 命令。您无法打开指向特定URL的套接字。

You would have only to open a socket to "localhost", 8080 and then issue a GET scrabServ/connect command. You can't open a socket to a specific URL.

要与Servlet进行通信,您可以通过请求参数来实现,基本上是发出GET命令,例如: http://www.jmarshall.com/easy/http/http_footnotes.html#getsubmit

To communicate with a servlet you do it via request parameters, basically issuing a GET command such as: http://www.jmarshall.com/easy/http/http_footnotes.html#getsubmit

也许您应该使用 URLConnection 代替。如果您详细说明了您想做什么,也许我可以更好地帮助您,也许您甚至不需要HTTP服务器。

Maybe you should use URLConnection instead. If you detail what exactly you want to do, maybe I can have a better idea of how to help you, maybe a HTTP server is not even needed for what you want to do.

这篇关于连接到Tomcat上的套接字?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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