Servlet (二)ServletContext
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的更多相关文章
- JavaWeb学习 (六)————Servlet(二)
一.ServletConfig讲解 1.1.配置Servlet初始化参数 在Servlet的配置文件web.xml中,可以使用一个或多个<init-param>标签为servlet配置一些 ...
- servlet ServletConfig ServletContext
ServletConfig对象 在Servlet的配置文件中,可以使用一个或者多个<init-param>标签为servlet配置一些初始化参数. 当servlet配置了初始化参数后,we ...
- 2.深入学习Servlet的ServletContext对象
一.建立项目servlet01 在入门Servlet项目中建立一个子项目模块(此处不再赘述如何建立),补全maven项目中的java和resources文件夹,添加类HelloServlet.java ...
- Servlet、ServletContext与ServletConfig的详解及区别
Servlet.ServletContext与ServletConfig的详解及区别 一.Servlet详解 Servlet是一个interface,全局限定名:javax.servlet.Servl ...
- Servlet之ServletContext以及文件操作
ServletContext ------------------------------------ ServletContext是什么? 与cookie,session比较. 可以把它想象成一个共 ...
- Servlet学习(二)——ServletContext对象
1.什么是ServletContext对象 ServletContext代表是一个web应用的环境(上下文)对象,ServletContext对象内部封装是该web应用的信息,一个web应用只有一个S ...
- servlet中servletContext的五大作用(二)
1. 获取web的上下文路径 2. 获取全局的参数 3. 作为域对象使用 4. 请求转发 5. 读取web项目的资源文件 package day10.about_serv ...
- Servlet 之 ServletContext
package cn.jiemoxiaodi.servlet_servletcontext; import java.io.IOException; import java.io.PrintWrite ...
- web开发之Servlet 二
在上一篇文章中,我们演示也证明了Servlet 是一种动态web资源开发的技术,即我可以在浏览器中输入URL,然后就可以在浏览器中看到我们编写的Servlet资源. 那当我们在浏览器上一起一个HTTP ...
随机推荐
- 【GMT43智能液晶模块】例程六:WWDG看门狗实验——复位ARM
实验原理: STM32内部包含窗口看门狗,通过看门狗可以监控程序运行,程序运行 错误时,未在规定时间喂狗,自动复位ARM.本实验通过UI界面中按钮按下 停止喂狗,制造程序运行错误,从而产生复位. 示例 ...
- 【Unity】Protobuf的使用与常见问题
Protobuf的使用流程 protobuf参考教程:https://www.jianshu.com/p/b135676dbe8d 手写.proto文件后,用CMD命令行运行protoc.exe编译器 ...
- 一键解包/打包boot.img/recovery.img工具(高通/MTK双版 支持android 5.1以上)
下载地址: 链接: https://pan.baidu.com/s/1hsA2oWc 密码: skdx
- 树莓GPIO &&python
from http://www.cnblogs.com/xiaobo-Linux/p/8969324.html 命令行控制LED灯 echo 12 > /sys/class/gpio/ex ...
- [Paper] **Before GAN: sparse coding
读罢[UFLDL] ConvNet,为了知识体系的完整,看来需要实战几篇论文深入理解一些原理. 如下是未来博文系列的初步设想,为了hold住 GAN而必备的知识体系,也是必经之路. [Paper] B ...
- 一个整型数组里除了一个数字之外,其他的数字都出现了两次。要求时间复杂度是O(n),空间复杂度是O(1),如何找出数组中只出现一次的数字
思路分析:任何一个数字异或它自己都等于0,根据这一特性,如果从头到尾依次异或数组中的每一个数字,因为那些出现两次的数字全部在异或中抵消掉了,所以最终的结果刚好是那些只出现一次的数字. 代码如下: #i ...
- ASP.NET MVC 4 (十二) Web API
Web API属于ASP.NET核心平台的一部分,它利用MVC框架的底层功能方便我们快速的开发部署WEB服务.我们可以在常规MVC应用通过添加API控制器来创建web api服务,普通MVC应用程序控 ...
- 采用镜像的方法安装python第三方库
转自:https://blog.csdn.net/s740556472/article/details/68557330 pip install --index https://pypi.mirror ...
- 开源库RxJava、ButterKnife
1. 简介 RxJava "RxJava is a Java VM implementation of Reactive Extensions: a library for composin ...
- mongodb new file allocation failure
话说那天正在向mongodb中写入数据,突然就蹦出了 new file allocation failure ,以为是数据有错误,就检查了一番,可没问题啊,看着像是mongo自己的问题,于是百度了一番 ...