Servlet第一节内容整理

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中读取

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值