因为你用的是网页显示的,所以可能会出现中文乱码!我下面写了一个网页访问人数统计的,可以供你参考一下!
package servletBao;
import java.io.*;
import java.util.*;
import java.text.*;
import javax.servlet.ServletException;
import javax.servlet.http.*;
public class HelloServlet extends HttpServlet {
/**
* The doGet method of the servlet. <br>
*
* This method is called when a form has its tag value method equals to get.
*
* @param request the request send by the client to the server
* @param response the response send by the server to the client
* @throws ServletException if an error occurred
* @throws IOException if an error occurred
*/
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
System.out.println("调用doGet");
Date now=new Date();
SimpleDateFormat format=new SimpleDateFormat("yyyy年MM月dd日");
//解决中文乱码
response.setContentType("text/html;charset=GBK");
//会话跟踪 创建Session
HttpSession session=request.getSession(true);
// session.setAttribute("counter",new Integer(counter));
Object count=session.getAttribute("counter");
int counter=0;
if(count==null){
System.out.println("这也运行了");
counter=1;
session.setAttribute("counter", new Integer(1));
}else{
System.out.println((Integer)count);
System.out.println(counter);
counter=((Integer)count).intValue();
counter++;
session.setAttribute("counter", new Integer(counter));
System.out.println(counter);
}
System.out.println(counter);
PrintWriter out=response.getWriter();
out.println("欢迎你第"+counter+"次访问Accp网页");
out.flush();
out.close();
// String username=request.getParameter("username");
// String password=request.getParameter("password");
// if(username.equals("accp") && password.equals("123")){
// response.sendRedirect("success.jsp");
// }else{
// response.sendRedirect("error.jsp");
// }
}
/**
* The doPost method of the servlet. <br>
*
* This method is called when a form has its tag value method equals to post.
*
* @param request the request send by the client to the server
* @param response the response send by the server to the client
* @throws ServletException if an error occurred
* @throws IOException if an error occurred
*/
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
System.out.println("调用doPost()");
doGet(request, response);
}
/**
* 调用父类的构造方法
*/
public HelloServlet(){
super();
}
/**
* servlet初始化
*/
// public void inint() throws ServletException(){
// System.out.println("初始化时,init()方法被调用");
// }
public void init() throws ServletException{
System.out.println("init()方法被调用");
}
/**
* 释放资源
*/
public void destroy(){
super.destroy();
System.out.println("释放系统资源时destroy()方法被调用");
}
}
这是我以前写的一个!也许会对你有帮助! |