서블릿에 여러 클라이언트가 요청 시
[펌]
http://annehouse.tistory.com/285
서블릿에 여러 클라이언트가 동시에 요청하는 경우
=> Thread 생성
=> 서블릿에서 멤버변수는 모든 클라이언트가 공유하는 데이터가 되고
=> service, doget, dopost 내 의 지역 변수는 각 각 독자적을 사용하는 데이터가 됨
=> WAS가 서블릿을 최초 요청 시 하나만 만들고 다른 요청들에 대해서는 Thread를 생성해서 멤버변수를 공유하게 되기 때문에 서블릿 작성 시 왠만하면 멤버변수보다는 지역변수를 사용하도록 한다.
[실습] Exam05.java
@WebServlet("/exam05")
public class Exam05 extends HttpServlet{
String str = null;
public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException{
response.setContentType("text/html;charset=utf-8");
PrintWriter out = response.getWriter();
out.print("<h1>결과!!");
//String str = null;
str = request.getParameter("p");
for(int i=0;i<10;i++){
out.print("<br>"+str);
System.out.println(str);
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
out.close();
}
}