doGet()方法里调用其他方法的问题

danwell7 2012-03-07 07:37:20
package web1Pac;

import java.io.IOException;
import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.*;
@WebServlet("/TestServlet")
public class TestServlet extends HttpServlet
{ private static final long serialVersionUID = 1L;
private static final int doGetCount=0;
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
{ response.setContentType("text/html;charset=utf-8");
PrintWriter out;
out=response.getWriter();
out.print("会家了");
System.out.println("doGetCount method called"+doGetCount+"times");
processRequest(request,response);
}
public void processRequest(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException
{ PrintWriter out;
String title="Servlet Life Cycle Demo Page";
int iniCount=0;
int calledCount=0;
int destryCount=0;
String heading1="Init times"+iniCount;
String heading2="Called times"+calledCount;
String heading3="Destroy times"+destryCount;
response.setContentType("text/html;charset=utf-8");
out=response.getWriter();
out.print("<html><head><title>"+title+"<title>");
out.print("<head><body>");
out.print("<h1>"+heading1+"</h1>");
out.print("<h1>"+heading2+"</h1>");
out.print("<h1>"+heading3+"</h1>");
out.print("</body></html>");
out.close();

}

}
上面的程序使用的springsource软件编译的,启动服务器后,一会服务器端就会有输出doGetCount的值,而浏览器端却是空白,在doGet()中
调用自定义的processRequest(request,response)方法为什么没有用???怎样改才能够确保processRequest(request,response)调用了??
...全文
416 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
宏Lee 2012-03-07
  • 打赏
  • 举报
回复
注意看你这个
out.print("<html><head><title>"+title+"<title>");
out.print("<head><body>");
out.print("<h1>"+heading1+"</h1>");
out.print("<h1>"+heading2+"</h1>");
out.print("<h1>"+heading3+"</h1>");
out.print("</body></html>");
都没加/加上肯定好了,注意点
安特矮油 2012-03-07
  • 打赏
  • 举报
回复
<head><title>" + title + "<title></head>
去掉这段就可以了
danwell7 2012-03-07
  • 打赏
  • 举报
回复
说错了。是没任何输出,也没报错
danwell7 2012-03-07
  • 打赏
  • 举报
回复
但是当把doGet()方法内的代码都加注释,只留调用的方法processRequest().浏览器端也没有任何输出,也报错,这个该怎么改,使processrequest()的内容在浏览器端输出??
宏Lee 2012-03-07
  • 打赏
  • 举报
回复
out.print("会家了");这句话就已经返回客户端了,所以没有用
danwell7 2012-03-07
  • 打赏
  • 举报
回复
destroy()方法查了一下,意思是服务器结束servlet时调用,是服务器程序关闭时调用它。这儿的服务器关闭程序是用一个方法实现,还是怎么做的??楼上的能否列举一个使用destroy()方法存文件或数据库的实例??
宏Lee 2012-03-07
  • 打赏
  • 举报
回复
服务器都听停了destryCount这个肯定不能加了啊,要做你这样的,只有存文件或者数据库啊,咋可能你这样嘛
danwell7 2012-03-07
  • 打赏
  • 举报
回复
上面的问题是没有使用关闭的</head></title>,解决了,还有一个问题是在类中增加了destroy()方法如下:
public void destroy()
{ destryCount++;
System.out.println("destry method called"+destryCount+"times");
}

这个方法在我停止服务器在从启服务器的时候没有在服务器窗口输出结果??

67,513

社区成员

发帖
与我相关
我的任务
社区描述
J2EE只是Java企业应用。我们需要一个跨J2SE/WEB/EJB的微容器,保护我们的业务核心组件(中间件),以延续它的生命力,而不是依赖J2SE/J2EE版本。
社区管理员
  • Java EE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧