package cn.sasa.serv;

import java.io.IOException;

import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse; public class ContextServlet extends HttpServlet {
private static final long serialVersionUID = 1L; public ContextServlet() {
super();
} protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//获得全局初始化参数
ServletContext context = this.getServletContext();
String url = context.getInitParameter("url");
System.out.println(url); //取得绝对路径getRealPath()
String path1 = context.getRealPath("index.html");
System.out.println(path1); // String path = context.getRealPath("WEB-INF/classes/c3p0-config.xml");
// System.out.println(path);
//classes下的文件
String path = ContextServlet.class.getClassLoader().getResource("c3p0-config.xml").getPath();
System.out.println(path); //ServletContext是全局的,可以获取另一个Servlet设置的值
int count = (int)context.getAttribute("count");
System.out.println(count);
response.getWriter().append("Served at: ").append(request.getContextPath());
} protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doGet(request, response);
} }
package cn.sasa.serv;

import java.io.IOException;
import java.sql.SQLException; import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse; import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler; import cn.sasa.domain.MyUser;
import cn.sasa.utils.C3P0Utils; public class Login extends HttpServlet {
private static final long serialVersionUID = 1L; public Login() {
super();
} public void init(){
//初始化时创建一个变量
int count = 0;
this.getServletContext().setAttribute("count", count);
} protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { String userName = request.getParameter("userName");
String pwd = request.getParameter("pwd");
//System.out.println(userName+"====="+pwd); QueryRunner query = new QueryRunner(C3P0Utils.getDataSource());
String sql = "select * from user where name=? and pwd=?";
MyUser user = null;
try {
user = query.query(sql, new BeanHandler<MyUser>(MyUser.class), userName,pwd);
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
if(user != null) {
int count = (int)this.getServletContext().getAttribute("count");
count++;
this.getServletContext().setAttribute("count", count);
response.getWriter().write(user.toString()+"==="+count);
}else {
response.getWriter().write("wrong");
}
} protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doGet(request, response);
} }

Servlet (二)ServletContext的更多相关文章

  1. JavaWeb学习 (六)————Servlet(二)

    一.ServletConfig讲解 1.1.配置Servlet初始化参数 在Servlet的配置文件web.xml中,可以使用一个或多个<init-param>标签为servlet配置一些 ...

  2. servlet ServletConfig ServletContext

    ServletConfig对象 在Servlet的配置文件中,可以使用一个或者多个<init-param>标签为servlet配置一些初始化参数. 当servlet配置了初始化参数后,we ...

  3. 2.深入学习Servlet的ServletContext对象

    一.建立项目servlet01 在入门Servlet项目中建立一个子项目模块(此处不再赘述如何建立),补全maven项目中的java和resources文件夹,添加类HelloServlet.java ...

  4. Servlet、ServletContext与ServletConfig的详解及区别

    Servlet.ServletContext与ServletConfig的详解及区别 一.Servlet详解 Servlet是一个interface,全局限定名:javax.servlet.Servl ...

  5. Servlet之ServletContext以及文件操作

    ServletContext ------------------------------------ ServletContext是什么? 与cookie,session比较. 可以把它想象成一个共 ...

  6. Servlet学习(二)——ServletContext对象

    1.什么是ServletContext对象 ServletContext代表是一个web应用的环境(上下文)对象,ServletContext对象内部封装是该web应用的信息,一个web应用只有一个S ...

  7. servlet中servletContext的五大作用(二)

    1.    获取web的上下文路径 2.    获取全局的参数 3.    作为域对象使用 4.    请求转发 5.    读取web项目的资源文件 package day10.about_serv ...

  8. Servlet 之 ServletContext

    package cn.jiemoxiaodi.servlet_servletcontext; import java.io.IOException; import java.io.PrintWrite ...

  9. web开发之Servlet 二

    在上一篇文章中,我们演示也证明了Servlet 是一种动态web资源开发的技术,即我可以在浏览器中输入URL,然后就可以在浏览器中看到我们编写的Servlet资源. 那当我们在浏览器上一起一个HTTP ...

随机推荐

  1. Android——Android和SVN::::SVN+delete项目

    SVN使用笔记(比较详细) http://www.cnblogs.com/merray/p/4182380.html 删除项目 http://jingyan.baidu.com/article/c74 ...

  2. Java知多少(30)多态和动态绑定

    在Java中,父类的变量可以引用父类的实例,也可以引用子类的实例. 请大家先看一段代码: public class Demo { public static void main(String[] ar ...

  3. Java知多少(34)final关键字:阻止继承和多态

    在 Java 中,声明类.变量和方法时,可使用关键字 final 来修饰.final 所修饰的数据具有“终态”的特征,表示“最终的”意思.具体规定如下: final 修饰的类不能被继承. final ...

  4. python中的生成器函数是如何工作的?

    以下内容基于python3.4 1. python中的普通函数是怎么运行的? 当一个python函数在执行时,它会在相应的python栈帧上运行,栈帧表示程序运行时函数调用栈中的某一帧.想要获得某个函 ...

  5. 第三百九十八节,Django+Xadmin打造上线标准的在线教育平台—生产环境部署CentOS6.5系统环境设置

    第三百九十八节,Django+Xadmin打造上线标准的在线教育平台—生产环境部署CentOS6.5系统环境设置 1.Linux安装配置 注意事项: 虚拟机网卡桥接模式 不要拨VPN 如果,网络怎么都 ...

  6. Python encode() 方法(转)

    转自:http://www.cnblogs.com/wushuaishuai/p/7686290.html 描述 encode() 方法以指定的编码格式编码字符串,默认编码为 'utf-8'. 对应的 ...

  7. [TensorBoard] Train and Test accuracy simultaneous tracking

    训练时的实时状态跟踪的重要性 不言而喻. [Tensorboard] Cookbook - Tensorboard  讲解调节更新频率 直接上代码展示: import numpy as np impo ...

  8. HTML 选择目录

    <input type="file" webkitdirectory directory multiple/>

  9. 【代码审计】YzmCMS_PHP_v3.6 任意文件删除漏洞分析

      0x00 环境准备 YzmCMS官网:http://www.yzmcms.com/ 程序源码下载:http://pan.baidu.com/s/1pKA4u99 测试网站首页: 0x01 代码分析 ...

  10. 【Important】数据库索引原理

    为什么要给表加上主键? 为什么加索引后会使查询变快? 为什么加索引后会使写入.修改.删除变慢? 什么情况下要同时在两个字段上建索引? 想理解索引原理必须清楚一种数据结构(平衡树非二叉)也就是b tre ...