// 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>"); } %>
// 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); } }
<%@ 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(output, username, password); %> <%! public void showUserInfo(java.io.PrintWriter _output, String _username, String _password) { //为了突出并发问题,在这儿首先执行一个费时操作 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>"); } %>