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. MySQL的binlog日志<转>

    binlog 基本认识 MySQL的二进制日志可以说是MySQL最重要的日志了,它记录了所有的DDL和DML(除了数据查询语句)语句,以事件形式记录,还包含语句所执行的消耗的时间,MySQL的二进制日 ...

  2. 【原】使用Tkinter绘制GUI并结合Matplotlib实现交互式绘图

    在数据分析的过程中,往往需要对所建立的模型进行可视化,并调整其中的某些参数. 通常情况下,在Python中可以通过Matplotlib来进行绘制图像.然而该绘制过程是静态的,也就是每次调整完参数需要重 ...

  3. 看雪CTF第十五题

    1.直接运行起来,再用OD附加 在此处luajit加载并调用main函数 004021C7 E8 64FE0000 call CrackMe. ; luaL_newstate 004021CC 8BF ...

  4. 转载:安装Ubuntu 15.10后要做的事

    转载:安装Ubuntu 15.10后要做的事 原文转载于:http://blog.csdn.net/skykingf/article/details/45267517 Ubuntu 15.10发布了, ...

  5. CTimeSpan

    要获取两个时间差,如两个CTime的时间差,可以使用MFC中的CTimeSpan类. CTime time1 = CTime::GetCurrentTime(); CTime time2 = CTim ...

  6. AbtestingGateway 复制请求到其他服务上

    perface 最近有需求,需要把服务a发给服务b的请求复制给服务c,服务a发给服务b的时候会经过nginx,这个nginx是有lua脚本来辅助工作的.说白了,这个nginx+lua就是abtesti ...

  7. [IR] Huffman Coding

    为了保证:Block中,所有的叶子在所有的中间结点的前面.Static: Huffman coding Dynamic: Adaptive Huffman 一些概念 压缩指标 • Compress a ...

  8. THINKPHP5 volist标签循环不能设置循环变量为$i

    在thinkphp5的volist标签中不要用$i作为id,举个简单例子 控制器这样写 模板这样写 结果是 这很令人费解啊.然后换一个循环变量看看 循环正常了,看来这个id这里设置循环变量的时候 不能 ...

  9. Polygon Offset

    https://www.cnblogs.com/bitzhuwei/p/polygon-offset-for-stitching-andz-fighting.html 一个大于0的offset 会把模 ...

  10. TSPL学习笔记(1)

    扩展语法(Syntactic extensions) 扩展语法就是通过核心语法或已经定义的扩展语法创建一种新的语法模式. Scheme核心语法模式包括: 顶层定义 常量 变量 过程应用 '(quote ...