Web动态项目,Instance同一个实例

时间:2013-08-21 15:42:46

标签: java

我正在开发一个Web项目,我有一个名为Connection的类,这个类通过TCP / IP与另一个设备建立连接,当有一个http get请求我实例化一个对象“connection_o”并启动一个线程保持通信如下:“connection_o.start”所以建立连接,在下一个http请求中我必须发送一条消息,但是当我再次执行“doGet”以避免nullpointer异常时我需要实例该对象,但我不能,因为我需要使用我之前运行的相同实例,在我的测试中,连接继续工作,但我无法访问我已经创建的线程。所以我需要某种静态类或使用已经运行的线程的方法。

这是套接字的代码

   import java.io.*;
import java.net.*;
public class Provider extends Thread{

    ServerSocket providerSocket;
    Socket connection = null;
    ObjectOutputStream out;
    ObjectInputStream in;
    String message;
    Provider(){}


    public void run()
    {
        try{
            //1. creating a server socket
            providerSocket = new ServerSocket(2004, 10);
            //2. Wait for connection
            System.out.println("Waiting for connection");
            connection = providerSocket.accept();
            System.out.println("Connection received from " + connection.getInetAddress().getHostName());
            //3. get Input and Output streams
            out = new ObjectOutputStream(connection.getOutputStream());
            out.flush();
            in = new ObjectInputStream(connection.getInputStream());
            sendMessage("Connection successful");
            //4. The two parts communicate via the input and output streams
            do{
                try{
                    message = (String)in.readObject();
                //  System.out.println("client>" + message);



                    if (message.equals("cambio la variable"))
                        System.out.println("Abriendo Puerta");
                    //  sendMessage("bye");
                }
                catch(ClassNotFoundException classnot){
                    System.err.println("Data received in unknown format");
                }
            }while(!message.equals("bye"));
        }
        catch(IOException ioException){
            ioException.printStackTrace();
        }
        finally{
            //4: Closing connection
            try{
                in.close();
                out.close();
                providerSocket.close();
            }
            catch(IOException ioException){
                ioException.printStackTrace();
            }
        }
    }
    public void sendMessage(String msg)
    {
        try{
            out.writeObject(msg);
            out.flush();
        //  System.out.println("server>" + msg);
        }
        catch(IOException ioException){
            ioException.printStackTrace();
        }
    }

这是doGet

    protected void doGet(HttpServletRequest request, HttpServletResponse response) 

throws ServletException, IOException {
        String s = request.getParameter("s");
        //Routine to send blink to RPi
    if (s.equals("Start")){
      Provider c = new Provider();
      c.Inicio();
    }

    if (s.equals("Send")){

          c.sendMessage("Blink");
        }

提前致谢

2 个答案:

答案 0 :(得分:0)

我假设的问题是,HTTP是无状态协议。这意味着默认情况下,服务器对客户端一无所知。

这就是Sessions到位的地方。在初始请求中,您必须为发出请求的客户端初始化会话。从那时起,您就能够“识别”客户端的请求(例如,恢复客户端状态)。

您还必须在会话上下文中保存“连接”。您可能需要考虑使用所谓的“连接池”,因为处理所有已建立的连接并正确关闭它们(例如会话超时等)可能非常复杂。

我建议您阅读有关HTTP协议,会话和(例如数据库)连接处理的基础知识。

答案 1 :(得分:0)

可能是你可以保留

Provider c = new Provider();

作为servlet类的实例变量并更改代码:

if (s.equals("Start")){
      c.Inicio();
    }

注意:在这种情况下,您需要注意线程的安全性。因为现在多个请求线程会调用c.Inicio()