现在就是要求HTTP和SOCKET并行,我的SOCKET是监听2222端口。我不知道现在该怎么做。请各位帮我指点指点啊!
在线等待!

解决方案 »

  1.   

    你的Server类没有无参构造函数
    new Server(参数列表);
      

  2.   

    jsp页面里输入servlet所需的参数,提交servlet后,servlet去执行
      

  3.   

    我的源程序是这样的:Server.java
    package com.intel.sms.clientcommunications;
    import java.net.*; 
    import java.io.*; 
    import java.lang.*; public class Server implements Runnable{
      Socket socket1;
          
     String reply="123";
    public Server(Socket s){
    socket1=s;

    }

    public void run(){
    try{
    BufferedReader in=new BufferedReader(new InputStreamReader(socket1.getInputStream()));
    PrintWriter out=new PrintWriter(new BufferedWriter(new OutputStreamWriter(socket1.getOutputStream())),true);
        String str=in.readLine();//get imformation
        String address=socket1.getInetAddress().getHostAddress();
        String fileName="20041213a";
          String tftpServer="218.108.248.9"; 
          String flag="y";  
           String Len1,Len2,Len3;
          int len1=fileName.length();
          Integer obj1= new Integer(len1);
             if(len1<10)
             {
             
        Len1='0'+obj1.toString();
             }
             else Len1=obj1.toString();
           
            int len2=tftpServer.toString().length();
            Integer obj2= new Integer(len2);
            if(len2<10)
             {
             
           Len2='0'+obj2.toString();
             }
             else Len2=obj2.toString();
         
            int len3=flag.toString().length();
            Integer obj3= new Integer(len3);
            if(len3<10)
             {
           
          Len3='0'+obj3.toString();
             }
           else Len3=obj3.toString();
            
       reply='F'+Len1+fileName +'T'+Len2+tftpServer+'Y'+Len3+flag+address;
    out.println(reply);
    System.out.println(str);
    }
    catch(IOException e){
    System.err.println("IO Exception");
    }
    finally{
    try{
    socket1.close();
    }
    catch(IOException e){
    System.err.println("Socket not closed");
    }
    }
    }
    public static void main(String[] args)throws IOException{ 
    ServerSocket s; 
    s=new ServerSocket(2222); 
    try{
    while(true){
    Socket socket1=s.accept(); 
    try{
    new Server(socket1);
    }
    catch(Exception e){
    socket1.close();
                  }
    }
    }
    finally{
    s.close();
    }}
    public String getresponse()
    { return reply;
    }
    }
         可能楼上的大哥(或大姐)说的是对的,但我还是不知道怎么实现,各位帮我想想好吧!小妹感激不尽。
      

  4.   

    jsp页面输入参数提交servlet,servlet再处理socket
      

  5.   

    不考虑你在JSP里启动Server的合理性, 先给个办法
    JSP中:
        Server.startServer();源程序: 增加一个函数,修改一个函数
       /**
       * 启动Server
       * @throws Exception
       */
      public static void startServer() {
        ServerSocket s = null;
        try {
          s = new ServerSocket(2222);
          while (true) {
            Socket socket1 = s.accept();
            new Server(socket1);
          }
        }
        catch (IOException e) {
          e.printStackTrace();
        }
        finally {
          try {
            s.close();
          }
          catch (IOException e) {
            e.printStackTrace();
          }
        }
      }  public static void main(String[] args) throws IOException {
        startServer();
      }
      

  6.   

    “jsp页面输入参数提交servlet,servlet再处理socket”能再详细点吗?小妹以前是用ASP,刚接触JSP,SERVLET还不是很会。谢谢!
      

  7.   

    谢谢kingfish,肯定加分,太及时了!我觉得可能可行,我试试看。
      

  8.   

    我上面的有一句要修改为: new Thread(new Server(socket1)).start();另外,如果你对jsp,servelt不太明白, 先看看资料, 别人几句话恐怕给你解释不清楚.
      

  9.   

    在jsp页面里用个表单提交到servlet,在servlet的doPost里面执行socket程序的启动方法,也就把你的socket里面的main()方法改成楼上的启动方法。
      

  10.   

    原来的系统对表单的处理都是在JSP页面上处理的,没有采用Servlet,能不能在JSP页面上通过某一种方式启动执行SOCKET程序啊?
      我照楼上所说的  Server.startServer();
    但出现空指针错误啊!
      

  11.   

    因为我们这套系统是要HTTP和SOCKET并行,就是说一种产品通过HTTP来注册升级,另一种产品通过SOCKET通信来注册升级。
      

  12.   

    你在代码里面加一个无参数的构造函数就OK了
    public Server(){}
      

  13.   

    还是不行啊,出现java.lang.NullPointerException
    我是在index.jsp页面中加了:
    Server server=new Server();
    Server.startServer();
    痛苦,小妹学得太差了,17号就要联合调试了,现在进行不下去了,各位一定要帮我啊!
    我不会忘了给你们加分的,谢谢!
      

  14.   

    我的SERVLET是这样写的,但调试错误。抛出SMSException can't be declared or caught.我根本就没有定义SMSException啊!
    public class login extends HttpServlet{
    HttpSession session;
    public void doPost(HttpServletRequest req,HttpServletResponse res)throws IOException
    {
    Server1.startServer();
    res.setContentType("text/html");
    PrintWriter toClient=res.getWriter();
    try{
    String sUseraction = (String)req.getParameter("useraction");

       if ("login".equals(sUseraction))
         {
            String sUsername = req.getParameter("username");
            String sPassword = req.getParameter("password");   
            OperatorManager operatorManager = null; 
            Operator operator = null;
            String sLoginPageMsg="";
            if ( sUsername != null && sPassword != null )
            {
                 // perform the login (validate user/password pair)
                 operatorManager = new OperatorManager();      
                 operator = operatorManager.authorizeOperator(sUsername, sPassword);
            }        
            
            if ( operator == null )
            {
                sLoginPageMsg = "&#29992;&#25143;&#21517;&#25110;&#23494;&#30721;&#19981;&#27491;&#30830;";
                toClient.println("<html>");
                toClient.println("<title></title>");
                toClient.println(sLoginPageMsg);
                toClient.println("</html>");
                if (null==sUsername || sUsername.trim().length()==0)
                    sUsername = "NoInput";
                OperationHistoryManager.addLoginHistory(sUsername, (String) req.getRemoteHost(), "Fail");
            }
            else
            {        
                session.setAttribute("FUM_Operator", operator);
                session.setAttribute("FUM_Username", sUsername);
                OperationHistoryManager.addLoginHistory(sUsername, (String) req.getRemoteHost(), "Succeed");            
                res.sendRedirect("index.jsp");
                return;
            }
       } }
    catch(IOException e){
            e.printStackTrace();
         toClient.println("A problen occured while recording your answers."+"Please try again");
                              }
         toClient.close();
    }
    }
      

  15.   

    谢谢!在你们的提示下,这个Servlet我调试出来了,
    正确的是:import java.io.*;
    import java.util.*;
    import javax.servlet.*;
    import javax.servlet.http.*;
    import com.intel.sms.accountmanagement.operatormanager.*;
    import com.intel.fum.common.*;
    import com.intel.sms.utility.SMSException;
    import com.intel.sms.clientcommunications.Server1;
    public class login extends HttpServlet{
    HttpSession session;
    public void doPost(HttpServletRequest req,HttpServletResponse res)throws IOException
    {
    res.setContentType("text/html");
    PrintWriter toClient=res.getWriter();
    Server1.startServer();
    try{
    String sUseraction = (String)req.getParameter("useraction");

       if ("login".equals(sUseraction))
         {
            String sUsername = req.getParameter("username");
            String sPassword = req.getParameter("password");   
            OperatorManager operatorManager = null; 
            Operator operator = null;
            String sLoginPageMsg="";
            if ( sUsername != null && sPassword != null )
            {
                 // perform the login (validate user/password pair)
                 operatorManager = new OperatorManager();
                 try{   
                 operator = operatorManager.authorizeOperator(sUsername, sPassword);
                   }
               catch(SMSException smsexception)
              {
                throw new Exception(smsexception.toString());
              }
            }        
            
            if ( operator == null )
            {
                sLoginPageMsg = "&#29992;&#25143;&#21517;&#25110;&#23494;&#30721;&#19981;&#27491;&#30830;";
                toClient.println("<html>");
                toClient.println("<title></title>");
                toClient.println(sLoginPageMsg);
                toClient.println("</html>");
                if (null==sUsername || sUsername.trim().length()==0)
                    sUsername = "NoInput";
                OperationHistoryManager.addLoginHistory(sUsername, (String) req.getRemoteHost(), "Fail");
            }
            else
            {        
                session.setAttribute("FUM_Operator", operator);
                session.setAttribute("FUM_Username", sUsername);
                OperationHistoryManager.addLoginHistory(sUsername, (String) req.getRemoteHost(), "Succeed");            
                res.sendRedirect("index.jsp");
                return;
            }
       } }
     catch(Exception e)
              {
                toClient.println(e.toString());
              }
    finally{

         toClient.close();}
    }
    }
    但就是不起作用,我的login.jsp就放在
    C:\Inetpub\wwwroot\SMS下面,然后新编译好的login.class文件也是放在C:\Inetpub\wwwroot\SMS下面,那我在login.jsp页面中的<form method="post" action="">这一部分怎么设置啊!好心的大哥大姐们,再继续帮我,就快成功了!
      

  16.   

    我的电脑上有装JRUN,SERVLET的运行环境应该具备,因为原来的系统中就有用SERVLET,现在关键是路径问题,谢谢!很急!大家帮帮我好吗?我将不甚感激!
      

  17.   

    就是FORM里面这个ACTION的路径,还有上面那段程序中 res.sendRedirect("index.jsp");这个路径的设置,因为我浏览的时候,提示找不到网页,路径错误。用绝对的是怎么设置啊?
     谢谢!
    在线等待!
      

  18.   

    假如你的环境中,sms是设置的的应用程序的环境路径。而index.jsp又在sms下的话,绝对路径就是/sms/index.jsp
      

  19.   

    谢谢KINGFISH,谢谢楼上的其他几位大哥。以上所述的问题我已经基本上有了解决办法,就是把我SERVER类文件做成一个可执行文件,再加到系统启动文件中去。
    但,我又出现了新问题:SERVER端我是用JAVA写的,CLIENT端别人是用C/C++写的,现在测试了却不能通信,连不通。怎么办啊?是应该可以通信的啊,是不是还要作一些什么其他的设置啊?