servlet中的多个客户端请求

时间:2011-07-16 11:10:22

标签: java servlets

我正在创建一个servlet(代理服务器)来处理来自客户端的多个请求。我知道如何在同一个servlet上处理多个请求。我应该用什么来处理多个请求。

例如我有两个HTML页面。它同时将请求发送到同一个servlet。如何处理来自两个页面的请求以及如何响应这些页面(每个页面分别)

这是我的servlet代码。

   protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    // TODO Auto-generated method stub
    String ip_Address=null;
    int ip_port=0;
    String request_action = null;
    String Target=null;
    String Action = null;
    String Setchannel = null;
    String AssetID=null;
    String AssetURI=null;
    String Position=null;
    String Speed=null;
    String Keywords=null;

    Connection con = null;

    ResultSet rs = null;
         /* String myMessageText = "action=play;assetId=1000;assetURI=/movies/avatar.ts;position=123.32";

          String[] parts=myMessageText.split(";");
          System.out.println(parts[3])*/;
          response.setContentType("text/html");
          PrintWriter out = response.getWriter();
          out.println("<html>");

          out.println("<body>");
          out.println("<h1>Servlet JDBC</h1>");




        try {
            ip_Address=request.getRemoteHost();
            ip_port=request.getRemotePort();
            request_action = request.getParameter(CommonConstants.REQ_PARAM);
            Target=request.getParameter(CommonConstants.TARGET_PARAM);
            Action=request.getParameter(CommonConstants.ACTION_PARAM);
            Setchannel=request.getParameter(CommonConstants.CHANNEL_PARAM);
            AssetID=request.getParameter(CommonConstants.ASSETID_PARAM);
            AssetURI=request.getParameter(CommonConstants.ASSETURI_PARAM);
            Position=request.getParameter(CommonConstants.POSITION_PARAM);
            Speed=request.getParameter(CommonConstants.SPEED_PARAM);
            Keywords=request.getParameter(CommonConstants.KEYORDS_PARAM);


        } 
        catch(Exception e)
        {


        }

          try {
             // Establish the connection. 
             SQLServerDataSource ds = new SQLServerDataSource();
             ds.setUser("sa");
             ds.setPassword("password123");
             ds.setServerName("ENMEDIA-EA6278E\\ENMEDIA");
            ds.setDatabaseName("IBC_ProxyServer");

             con = ds.getConnection();

             // Execute a stored procedure that returns some data.
             Statement stmt = con.createStatement();



           String sql="INSERT INTO  " + CommonConstants.Table_name + " ("+CommonConstants.Column1_ipaddress+","+CommonConstants.Column2_ip_port+","+CommonConstants.Column3_req+","+CommonConstants.Column4_target+","+CommonConstants.Column5_action+","+CommonConstants.Column6_channel +","+CommonConstants.Column7_assetID +","+CommonConstants.Column8_assetURI +","+CommonConstants.Column9_position +","+CommonConstants.Column10_speed +","+CommonConstants.Column11_keywords+" ) VALUES(?,?,?,?,?,?,?,?,?,?,?)";
            //stmt.executeUpdate("INSERT INTO " + CommonConstants.Table_name + "("+CommonConstants.Column1_ipaddress+","+CommonConstants.Column2_ip_port+","+CommonConstants.Column3_req+","+CommonConstants.Column4_target+","+CommonConstants.Column5_action+","+CommonConstants.Column6_channel +","+CommonConstants.Column7_assetID +","+CommonConstants.Column8_assetURI +","+CommonConstants.Column9_position +","+CommonConstants.Column10_speed +","+CommonConstants.Column11_keywords+") VALUES('{0}','{1}','{2}','{3}','{4}','{5}','{6}','{7}','{8}','{9}','{10}','{11}')",ip_Address,ip_port,request_action,Target,Action,Setchannel,AssetID,AssetURI,Position,Speed,Keywords);
           PreparedStatement pst = con.prepareStatement(sql);
           pst.setString(1, ip_Address);
           pst.setLong(2, ip_port);
           pst.setString(3, request_action);
           pst.setString(4, Target);
           pst.setString(5, Action);
           pst.setString(6, Setchannel);
           pst.setString(7, AssetID);
           pst.setString(8, AssetURI);
           pst.setString(9, Position);
           pst.setString(10, Speed);
           pst.setString(11, Keywords);
           pst.executeUpdate();
           con.close();
           out.println("<br>"+ip_Address+"</br>");
           out.println("<br>"+ip_port+"</br>");
           out.println("<br>"+request_action+"</br>");
           out.println("<br>"+Target+"</br>");
           out.println("<br>"+Action+"</br>");
           out.println("<br>"+Setchannel+"</br>");
           out.println("<br>"+AssetID+"</br>");
           out.println("<br>"+AssetURI+"</br>");
           out.println("<br>"+Position+"</br>");
           out.println("<br>"+Speed+"</br>");
           out.println("<br>"+Keywords+"</br>");

           out.println("</body></html>");  
         } catch (Exception e) {
             System.err.println("Got an exception! ");
             System.err.println(e.getMessage());
         }



}

假设有两个客户端A和B. A发送http://companion_proxy/ocl.cgi?req=cnc_cmd;target=12;action=setchannel;channel=34

B发送http://companion_proxy/ocl.cgi?req=registerdevice;type=CAM1;name=Livingroom

我必须得到A的参数并将其存储在数据库中,同样我必须将B的数据存储在另一个表中。我必须将响应发送到这些客户端

A ------&gt; 1

B --------&gt; target = 12; action = setchannel; channel = 34

如何对这两个不同的客户做出响应

1 个答案:

答案 0 :(得分:1)

通常,您不必关心多个请求正在同时处理的事实。 Application Server为您完成。您的servlet应该是无状态的,即避免在servlet的类变量中存储任何信息。如果您需要此类信息,请使用请求和会话属性。

我建议你参考Servlet API和servlet / jsp开发的多个可用教程之一。例如,在您的情况下,超过50%的servlet代码会生成HTML响应。显然,在JSP中实现这种逻辑要方便得多。

BTW在java中有一些命名约定。例如,变量名称必须以小写字母开头。