日期:2014-05-16 浏览次数:20376 次
// instanceconcurrenttest.jsp <%@ page contentType="text/html;charset=GBK" %> <%! //定义实例变量 String username; String password; java.io.PrintWriter output; %> <% //从request中获取参数 username = request.getParameter("username"); password = request.getParameter("password"); output = response.getWriter(); showUserInfo(); %> <%! public void showUserInfo() { //为了突出并发问题,在这儿首先执行一个费时操作 int i =0; double sum = 0.0; while (i++ < 200000000) { sum += i; } output.println(Thread.currentThread().getName() + "<br>"); output.println("username:" + username + "<br>"); output.println("password:" + password + "<br>"); } %>
a: http://localhost:8080/instanceconcurrenttest.jsp?username=a&password=123 b: http://localhost:8080/instanceconcurrenttest.jsp?username=b&password=456
// InstanceConcurrentTest.java import javax.servlet.*; import javax.servlet.http.*; import java.io.PrintWriter; public class InstanceConcurrentTest extends HttpServlet { String username; String password; PrintWriter out; public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException,java.io.IOException { //从request中获取参数 username = request.getParameter("username"); password = request.getParameter("password"); System.out.println(Thread.currentThread().getName() + " | set username:" + username); out = response.getWriter(); showUserInfo(); } public void showUserInfo() { //为了突出并发问题,在这儿首先执行一个费时操作 int i =0; double sum = 0.0; while (i++ < 200000000) { sum += i; } out.println("thread:" + Thread.currentThread().getName()); out.println("username:"+ username); out.println("password:" + password); } }