我正在創建一個處理來自客戶端的多個請求的servlet(代理服務器)。我知道如何處理多個請求到同一個servlet上。我應該使用什麼來處理多個請求。servlet中的多個客戶端請求
例如我有兩個HTML page.which在同一time.how將請求發送到相同的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的的getParameter並將其存儲在數據庫中,並同樣我也有來存儲數據乙在另一個table.I具有要發送給這些客戶端的響應作爲
甲------> 1
乙-------->目標= 12;動作= setchannel;信道= 34
怎麼辦這兩個不同的客戶端的響應
我已編輯我的問題。請看看它 – bharathi
再次,不要在乎這一點。所有變量都屬於方法範圍。這意味着即使1000個服務於1000個客戶端的線程同時運行相同的doGet(),每個實例運行它自己的「盒子」,也有它自己的變量和HttpResponse對象。所以,只需創建響應。該應用程序。服務器會關心將其傳送到「正確的」瀏覽器。 – AlexR