bharathi
bharathi

Reputation: 6271

Multiple Client request in servlet

I am creating a servlet(proxy server) which handles multiple request from the client.I do Know how to handle a muliple request top the same servlet.what should i have to use to handle the multiple request.

For example I have two HTML page.which send the request to the same servlet at the same time.how to handle the request from both the pages and how to respond to those pages (each one separately)

this is my servlet code.

   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());
         }



}

Suppose there is two clients A and B A sends http://companion_proxy/ocl.cgi?req=cnc_cmd;target=12;action=setchannel;channel=34

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

I have to getParameter of A and store it in database and same way i have to store the data of B in another table.I have to send the response to these clients as

A ------> 1

B-------->target=12;action=setchannel;channel=34

how to do the response to these two different clients

Upvotes: 0

Views: 3326

Answers (1)

AlexR
AlexR

Reputation: 115328

Typically you do not have to care about the fact that multiple requests are being processed simultaniosly. Application Server does it for you. Your servlet should be stateless, i.e. avoid storing any information in servlet's class variables. If you need such kind of information use request and session attributes instead.

I'd recommend you to refer to one of multiple available tutorials on Servlet API and servlet/jsp development at all. For example in your case more than 50% of code of your servlet generate HTML response. Obviously it is much more convenient to implement this kind of logic in JSP.

BTW there are some naming conventions in java. For example variables names must start with small letter.

Upvotes: 1

Related Questions