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. 基于AC有限状态机的多模匹配算法

    参考链接:http://www.cnblogs.com/zzqcn/p/3525636.html 感谢原文作者. 花了两天半时间实现并测试了算法. 按照上文的思路实现了一遍,可能是原文中有些地方描述的 ...

  2. Win10共享打印机所需要的设置(无需密码访问实现打印机共享,共享不要密码)

    原文:https://m.baidu.com/from=1086k/bd_page_type=1/ssid=0/uid=0/pu=usm@0,sz@1320_1002,ta@iphone_2_5.1_ ...

  3. 3D 特征点概述(2)

    还是紧接着上一文章的思路继续介绍3D特征点的基本概念问题,还是这个表格: Feature Name Supports Texture / Color Local / Global / Regional ...

  4. java代码执行字符串中的逻辑运算方法

    转载:https://www.jb51.net/article/143967.htm 方法一:Java调用js方法执行: /** * * @author: Longjun * @Description ...

  5. 8 -- 深入使用Spring -- 6... Spring的事务

    8.6 Spring 的事务 8.6.1 Spring支持的事务策略 8.6.2 使用XML Schema配置事务策略 8.6.3 使用@Transactional 参考1. 啦啦啦 我早就肯定我的身 ...

  6. 【代码审计】大米CMS_V5.5.3 代码执行漏洞分析

      0x00 环境准备 大米CMS官网:http://www.damicms.com 网站源码版本:大米CMS_V5.5.3试用版(更新时间:2017-04-15) 程序源码下载:http://www ...

  7. ORA-03135 防火墙超时设置断开db link 连接

    [现象] 应用使用数据库连接池,访问A库时通过dblink查询B库,应用时不时会报错ORA. [过程还原] 当应用获取了一个数据库连接,并在数据库连接中使用了dblink,如果应用到A库的连接不释放, ...

  8. The app references non-public selector in MyApp : id

      id<FBGraphUser>friend id<FBGraphUserExtraFields>user 应该使用 [user objectForKey:@"id ...

  9. linux-Centos 7下tftp-server服务的安装与配置

    TFTP(Trivial File Transfer Protocol,简单文件传输协议)是TCP/IP协议族中的一个用来在客户机与服务器之间 进行简单文件传输的协议,提供不复杂.开销不大的文件传输服 ...

  10. MySQL 之【视图】【触发器】【存储过程】【函数】【事物】【数据库锁】【数据库备份】

    1.视图 视图:是一个虚拟表,其内容由查询定义.同真实的表一样,视图包含一系列带有名称的列和行数据 视图有如下特点;  1. 视图的列可以来自不同的表,是表的抽象和逻辑意义上建立的新关系.  2. 视 ...