主题:写一个线程安全的Servlet以便显示该Servlet被访问的次数
写一个线程安全的Servlet以便显示该Servlet被访问的次数。
这是偶写的,能实现统计次数效果,但具体什么是线程安全的?
请高手给点意见及改进~谢谢
package myServlet;
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
public class Count extends javax.servlet.http.HttpServlet implements javax.servlet.Servlet {
int count = 0;
public void init(ServletConfig config)throws ServletException
{
super.init(config);
String initial = config.getInitParameter("initial");
try {
count = Integer.parseInt(initial);
} catch (Exception e) {
count = 0;
}
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
response.setContentType("text/html;charset=gbk");
response.setCharacterEncoding("gbk");
PrintWriter out = response.getWriter();
count ++;
out.print("<html><head><title>test</title></head><body>已经被访问"+ count +"次</body></html>");
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
}
}