Servlet第一节整理
1.杂记: E-HR:人力资源系统
E-SALES:消费系统
ERP:企业内部管理系统 ----》行业解决方案
2.HTTP协议:http://ip地址:端口号/web应用程序名/web应用程序的某一页
3.servlet 1.官方定义的servlet接口
2.程序员通过继承HttpServlet开发定义的Servlet
4.生命周期: init() 方法只执行一次
service() 方法运行几次执行几次,间接调用do get方法
destroy() 方法,销毁
5.ServletConfig: 用于获得参数信息
5.Context 上下文环境:
//使用上下文环境用来记录访问本页面的次数
@Override
public void init() throws ServletException {
// TODO Auto-generated method stub
super.init();
int count=0;
ServletContext sc=this.getServletContext();
sc.setAttribute("count", count);
}
/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
response.setContentType("text/html;charset=utf-8");
PrintWriter pw=response.getWriter();
ServletContext sc=this.getServletContext();
int count=(int) sc.getAttribute("count");
count++;
sc.setAttribute("count", count);
pw.println("<h2>访问本页面的次数为:"+count+"次</h2>");
pw.close();
}
//使用上下文环境在其他页面加载上下文环境记录的内容
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
response.setContentType("text/html;charset=utf-8");
PrintWriter pw=response.getWriter();
ServletContext sc=this.getServletContext();
int count=(int) sc.getAttribute("count");
pw.println("<h2>访问主页面的次数为:"+count+"</h2>");
}
//使用参数,取得参数定义编码格式
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
ServletConfig sc=this.getServletConfig();
String encode=sc.getInitParameter("encode");
response.setContentType("text/html;charset="+encode);
PrintWriter pw=response.getWriter();
pw.println("中国");
pw.close();
}
6.通过xml文件来部署servlet
name 的名字可以不和类名一样 但是上下的name要一致
如果有这个true表示强制从xml中读取