Servlet是多线程的,最好定义局部变量,不要定义全局变量

import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class helloServlet extends HttpServlet {
//String clientName =null ;      //这里的clientName为全局变量 
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doProcess(request, response);

}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doProcess(request, response);
}
public void doProcess(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {


response.setContentType("text/html");
PrintWriter out = response.getWriter();
//clientName 局部变量
String clientName =request.getParameter("Name");
if (clientName !=null) {
clientName=new String (clientName.getBytes("ISO-8859-1"),"GB2312");

}else {
clientName="My fiends";
}
String title ="helloServlet";
String handle="This is output from helloServlet by doGet:";

out.print(""+title+"");
out.print(handle);
out.print("

"+clientName+": hello

");
out.print("");

}

}

//因为servlet本身就是多线程的定义全局变量会使浏览器访问时出现混乱,最好定义局部变量

你可能感兴趣的:(代码)