-1

我的 web.xml 文件代码

<welcome-file-list>
    <welcome-file>/index.jsp</welcome-file>
</welcome-file-list>
<listener>
    <listener-class>com.vts.action.Server</listener-class>
</listener>

Server.java 代码....

public class Server extends HttpServlet implements ServletContextListener {
    private static final long serialVersionUID = 1L;

    Socket clientsocket;
    ServerSocket socket = null;
    int PORT = 9090;

    @Override
    public void contextDestroyed(ServletContextEvent arg0) {
        // TODO Auto-generated method stub
        System.out.println("ServletContextListener destroyed");

    }
       @Override
       public void contextInitialized(ServletContextEvent arg0) 
        {
           // hear code create socket server and accept multiple client connection and                        
              String via gps and save string in database is working correctly 
        }
}

但是现在我的问题是我想通过在后台运行的 gps 接受继续字符串

但是 index.jsp 页面没有启动以为是我的欢迎文件。我不确定我的web.xml中的监听器配置是对还是错.....请给建议

感谢和问候

克里希纳坎特

4

1 回答 1

1

Thanks for Reply ,Finally i get Solution... i am use threading in server side with TCP connection and create two java class MyListerner.java in that use contextListner method

@Override
public void contextDestroyed(ServletContextEvent arg0) {
    System.out.println("ServletContextListener destroyed");
}
@Override
public void contextInitialized(ServletContextEvent arg0) {
    ExecutorService executorService = Executors.newSingleThreadExecutor();
    executorService.submit(new RunClassOnDeployment(arg0.getServletContext()));
}

in contextInitialized call server.java(TCP connecton with threading)

vtsDataDao vtsData ;
ServletContext myServletContext; // in case you need the servelet context

public server(ServletContext sc) {
       myServletContext = sc;
}
@Override
public void run() {
    ServerSocket serverSocket = null;
    try {
        // Create the server socket.
        serverSocket = new ServerSocket(9090);
       } catch (IOException ioe) {
        System.out.println("Could not create server socket at 9090. Quitting.");
    }
    System.out.println("Listening for clients on 9090...");
    while (true) {
        try {
            Socket clientSocket = serverSocket.accept();
            try{
                DataInputStream is = new DataInputStream(new BufferedInputStream(clientSocket.getInputStream()));
                while (true) 
                   {
                    byte[] byteData = receiveData(is);
                    String frameData = new String(byteData).trim();
                    System.out.println("Incoming String's:-"+frameData);
                    new vtsDataDao().sendInputFrame(frameData);
                   }//end while
               }catch(Exception e){

               }//end ctry catch                
        } catch (IOException ioe) {
            System.out
                    .println("Exception encountered on accept. Ignoring. Stack Trace :");
            ioe.printStackTrace();
        }
    }
}

** Note:- in above class i am use two method 1. receiveData(DataInputStream is) for read i/p data and return VTS Data String And sendInputFrame(frameData);

web.XML

    <listener>
    <listener-class>com.vts.action.MyListener</listener-class>
</listener>
于 2013-03-29T10:31:36.767 回答