刚入门Servlet,为什么显示的是无时间标记!
package firstservlet;
import java.util.*;
/**
* <p> Title: </p>
*
* <p> Description: </p>
*
* <p> Copyright: Copyright (c) 2007 </p>
*
* <p> Company: </p>
*
* @author not attributable
* @version 1.0
*/
public class ContextObject {
private List list = new ArrayList();
private MyTimer timer = null;
private boolean isCont = true;
public ContextObject() {
}
public void time() {
Date some = new Date();
list.add(some);
}
public List getTime() {
return list;
}
public void startTimeStamp() {
timer = new MyTimer();
timer.start();
}
public void stopTimeStamp() {
isCont = false;
timer.interrupt();
}
class MyTimer extends Thread {
public void run() {
while (isCont) {
try {
sleep(1000);
} catch (InterruptedException ex) {
interrupt();
break;
}
time();
}
}
}
}
package firstservlet;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;
import javax.servlet.*;
import javax.servlet.http.*;
import java.util.Date;
/**
* <p> Title: </p>
*
* <p> Description: </p>
*
* <p> Copyright: Copyright (c) 2007 </p>
*
* <p> Company: </p>
*
* @author not attributable
* @version 1.0
*/
public class SimpleServlet extends HttpServlet {
private static final String CONTENT_TYPE = "text/html; charset=gb2312 ";
//Initialize global variables
public void init(ServletConfig cnf) throws ServletException {
super.init(cnf);
ServletContext cnt = cnf.getServletContext();
ContextObject obj = new ContextObject();
obj.startTimeStamp();
cnt.setAttribute( "obj ", obj);
System.out.println( "SimpleServlet.init() ");
}
//Process the HTTP Get request
public void doGet(HttpServletRequest request, HttpServletResponse response) throws
ServletException, IOException {
response.setContentType(CONTENT_TYPE);
PrintWriter out = response.getWriter();
out.println( " <html> ");
out.println( " <head> <title> ContextObject </title> </head> ");
out.println( " ");
out.println( " <body> <center> ");
ServletContext cnt = getServletContext();
Object cobj = cnt.getAttribute( "obj ");
if (cobj != null) {
ContextObject obj = (ContextObject) cobj;
List list = obj.getTime();
writeTimeTable(out, list);
} else {
out.println( "未找到 ContextObject! ");
}
out.println( " </center> </body> ");
out.println( " </html> ");
out.close();
}
/**
* writeTimeTable
*
* @param out PrintWriter
* @param list List
*/
private void writeTimeTable(PrintWriter out, List list) {
int size = list.size();
if (size != 0) {
out.println( " <table border=\ "1\ "> ");
out.println( " <tr> <th> 次数 </th> <th> 时间 </th> </tr> ");
for (int i = 0; i < size; i++) {
out.println( " <tr> ");
out.println( " <td> 第 " + (i + 1) + "次 </td> ");
out.println( " <td> " + ((Date) list.get(i)).toString() + " </td> ");
out.println( " </tr> ");
}
out.println( " </table> ");
} else {
out.println( "无时间标记! ");
}
}
//Process the HTTP Post request
public void doPost(HttpServletRequest request, HttpServletResponse response) throws
ServletException, IOException {
doGet(request, response);
}
//Clean up resources
public void destroy() {
ServletContext cnt = getServletContext();
Object cobj = cnt.getAttribute( "obj ");
if (cobj != null) {
ContextObject obj = (ContextObject) cobj;
obj.stopTimeStamp();
cnt.removeAttribute( "obj ");
}
System.out.println( "SimpleServlet.destroy() ");
}
}
<?xml version= "1.0 " encoding= "UTF-8 "?>
<!DOCTYPE web-app PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN " "http://java.sun.com/dtd/web-app_2_3.dtd ">
<web-app>
<servlet>
<servlet-name> simpleservlet </servlet-name>
<servlet-class> firstservlet.SimpleServlet </servlet-class>
</servlet>
<servlet-mapping>
<servlet-name> simpleservlet </servlet-name>
<url-pattern> /simpleservlet </url-pattern>
</servlet-mapping>
</web-app>
[解决办法]
因为servlet在你第一次访问的时候才初始化 所以你访问的时候没有数据 你刷新肯定有就有了
你可以设置让这个servlet 与tomcat一起启动
就可以了 第一次就看见数据
<servlet>
<servlet-name> simpleservlet </servlet-name>
<servlet-class> firstservlet.SimpleServlet </servlet-class>
<load-on-startup> 1 </load-on-startup>
</servlet>
这样改就可以了