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. Halcon 2D测量

    * This program shows how to detect the edges of a diamond * with subpixel accuracy and calculate the ...

  2. ubuntu之视频转换(Avconv的使用)

    1.安装 sudo apt-get install ffmpeg libav-tools 2.基本操作 avconv [options] [[infile options] -i infile] [[ ...

  3. tensorflow随机梯度下降算法使用滑动平均模型

    在采用随机梯度下降算法训练神经网络时,使用滑动平均模型可以提高最终模型在测试集数据上的表现.在Tensflow中提供了tf.train.ExponentialMovingAverage来实现滑动平均模 ...

  4. 和我一起学《HTTP权威指南》——安全HTTP与HTTPS

    安全HTTP HTTPS是最流行的HTTP安全形式. HTTPS方案的URL以https://开头 使用HTTPS时,所有的HTTP请求和响应数据在发送到网络之前,都要进行加密.HTTPS在HTTP传 ...

  5. mysqldump导出数据时,某些表不导出,排除某些表,不导出某些表

    需求说明: 今天一同事问,在通过mysqldump导出数据库的时候,能不能把某些表不导出,或者叫做排除在外呢, 记得应该是可以实现,就搜索了下,通过mysqldump的--ignore-table参数 ...

  6. [JS] ECMAScript 6 - Async : compare with c#

    一段引言: Promise 是异步编程的一种解决方案,比传统的解决方案——回调函数和事件——更合理和更强大. 它由社区最早提出和实现,ES6 将其写进了语言标准,统一了用法,原生提供了Promise对 ...

  7. [Python] 动态函数调用(通过函数名)

    2018-04-09 update 利用python中的内置函数 eval() ,函数说明: def eval(*args, **kwargs): # real signature unknown & ...

  8. windows下特殊字符无法用来命名

    原则上可以利用键盘输入的英文字母.符号.空格.中文等均可以作为合法字符,但由于以下字符由系统保留它用,因此不能用在文件命名中: :   /  \  ?  *  “  <  >  |  注: ...

  9. 【netcore基础】wwwroot下静态资源文件访问权限控制

    本文参考如下博问 https://q.cnblogs.com/q/107836 业务要求 上传的资源文件(.mp3 .mp4等)只有购买了之后才能有权限访问,所以对上传的资源文件目录进行访问权限控制 ...

  10. numpy中数据合并,stack ,concentrate,vstack,hstack

    在python的numpy库中有一个函数np.stack(), 看过一些博文后觉得别人写的太复杂,然后自己有了一些理解之后做了一些比较简单的解释 np.stack 首先stack函数用于堆叠数组,其调 ...