Servlet简介和ServletContext
0x01: 什么是Servlet?
- 是sun公司开发动态web的技术
- 实现了servlet接口的Java程序
0x02: Servlet的实现类有哪些?
Servlet接口默认有两个实现类
- HttpServlet
- GenericServlet
HttpServlet 继承自 GenericServlet, 一般我们自己写类只需要继承HttpServlet,重写方法就可以了
public class HelloServlet extends HttpServlet {
//由于get或者post只是请求实现的不同的方式,可以相互调用,业务逻辑都一样;
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//ServletOutputStream outputStream = resp.getOutputStream();
PrintWriter writer = resp.getWriter(); //响应流
writer.print("Hello,Serlvet");
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
doGet(req, resp);
0x03:Servlet的原理
浏览器发送http请求给web容器,web容器产生Request和Response对象,这两个对象调用servlet接口的Service方法,Service方法再将返回的Responce信息返回给Responce对象,web容器从Responce对象读取将其响应给客户端。(注意,如果web容器是首次被访问,需要先把我们的java类变成class字节码文件)
什么是ServletContext
Web容器在启动时,为每个web程序创建一个对应的ServletContext,它代表当前web应用
ServletContext的一些用法和特性
在同一个web应用中,不同的servlet可以共享数据
public class HelloServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//this.getInitParameter() 初始化参数
//this.getServletConfig() Servlet配置
//this.getServletContext() Servlet上下文
ServletContext context = this.getServletContext();
String username = "黎星澄"; //数据
context.setAttribute("username",username); //将一个数据保存在了ServletContext中,名字为:username 。值 username
}
}
public class GetServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
ServletContext context = this.getServletContext();
String username = (String) context.getAttribute("username");
resp.setContentType("text/html");
resp.setCharacterEncoding("utf-8");
resp.getWriter().print("名字"+username);
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
doGet(req, resp);
}
}
获取初始化参数
<!--在web.xml配置一些web应用初始化参数-->
<context-param>
<param-name>url</param-name>
<param-value>jdbc:mysql://localhost:3306/mybatis</param-value>
</context-param>
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
ServletContext context = this.getServletContext();
String url = context.getInitParameter("url"); //得到name为url的参数
resp.getWriter().print(url);
}
请求转发
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
ServletContext context = this.getServletContext();
System.out.println("进入了ServletDemo04");
//RequestDispatcher requestDispatcher = context.getRequestDispatcher("/xxx"); //转发的请求路径
//requestDispatcher.forward(req,resp); //调用forward实现请求转发;
context.getRequestDispatcher("/gp").forward(req,resp);
}
//请求转发和重定向的区别?
请求转发:像当于在服务器内部将请求转给请求的资源,然后由服务器相应给客户端
重定向:服务器将请求资源作为响应信息响应给客户端,客户端根据这个信息对服务器发起请求
读取资源文件
在java目录下新建properties
在resources目录下新建properties
都被打包到同一路径:classes
username=admin
password=123456
public class Servlet_Properties extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
InputStream is = this.getServletContext().getResourceAsStream("/WEB-INF/classes/com/hu/servlet/test.properties"); //输入流
Properties prop = new Properties(); //创建一个Properties对象
prop.load(is); //将读取到的加载到prop里
String user = prop.getProperty("username");
String pwd = prop.getProperty("password");
resp.getWriter().print(user+":"+pwd);
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
doGet(req, resp);
}
}
Servlet简介和ServletContext的更多相关文章
- JavaEE:Servlet简介及ServletConfig、ServletContext
Servlet简介 1.Servlet是sun公司提供的一门用于开发动态web资源的技术*静态web资源:固定数据文件*动态web资源:通过程序动态生成数据文件2.Servlet技术基于Request ...
- Servlet基础(一) Servlet简介 关键API介绍及结合源码讲解
Servlet基础(一) Servlet基础和关键的API介绍 Servlet简介 Java Servlet是和平台无关的服务器端组件,它运行在Servlet容器中. Servlet容器负责Servl ...
- Servlet简介与Servlet和HttpServlet运行的流程
1.Servlet [1] Servlet简介 > Server + let > 意为:运行在服务器端的小程序. > Ser ...
- Servlet学习笔记【1】--- 背景和基础知识(CGI、Web服务器发展史、Servlet简介、任务、继承结构)
本文主要讲Servlet的基础知识和背景知识. 1 CGI简介 CGI(Common Gateway Interface 公共网关接口)是WWW技术中最重要的技术之一,有着不可替代的重要地位.CGI是 ...
- Servlet简介及使用
javaweb学习总结(五)——Servlet开发(一) 一.Servlet简介 Servlet是sun公司提供的一门用于开发动态web资源的技术. Sun公司在其API中提供了一个servlet接口 ...
- servlet简介及生命周期
Servlet 简介 Servlet 是什么? Java Servlet 是运行在 Web 服务器或应用服务器上的程序,它是作为来自 Web 浏览器或其他 HTTP 客户端的请求和 HTTP 服务器上 ...
- (一)Servlet简介
相关名词解释 HTML:Hyper Text Markup Language,超文本标记语言 HTTP:Hyper Text Transfer Protocol,超文本传输协议 URL:Uniform ...
- 【Servlet】(1)Servlet简介、Servlet底层原理、Servlet实现方式、Servlet生命周期
一.Servlet简介 1.Servlet定义: Servlet(Server Applet)是Java Servlet的简称,是为小服务程序或服务连接器,用Java编写的服务器端程序,主要功能在于交 ...
- 超全面的JavaWeb笔记day09<Servlet&GenericServlet&HttpServlet&ServletContext>
1.Servlet概述 2.Servlet接口 3.GenericServlet 4.HttpServlet 5.Servlet细节 6.ServletContext(重要) Servlet概述 生命 ...
- 1.1(学习笔记)Servlet简介及一个简单的实例
一.Servlet简介 Servlet是使用Java语言编写的服务器端程序,可以生产动态的Web界面. 主要运行在服务器端,Servlet可以方便的处理客户端传来的HTTP请求,并返回一个响应. 二. ...
随机推荐
- P3Depth: Monocular Depth Estimation with a Piecewise Planarity Prior
1. 论文简介 论文题目:P3Depth: Monocular Depth Estimation with a Piecewise Planarity Prior Paper地址:paper Code ...
- AtCoder Regular Contest 148 B - dp
题面 For a string \(T\) of length \(L\) consisting of d and p, let \(f(T)\) be \(T\) rotated \(180\) d ...
- 又一重要进展发布!OpenMMLab算法仓支持昇腾AI训练加速
摘要:上海人工智能实验室的浦视开源算法体系(OpenMMLab)团队基于昇腾AI发布了MMDeploy 0.10.0版本,该版本已支持OpenMMLab算法仓库在昇腾异构计算架构CANN上的推理部署. ...
- MySQL 表的创建、复制、修改与删除
MySQL中如何利用代码完成表的创建.复制.修改和删除. 一.创建表 --创建新表,如果存在则覆盖 drop table [if exists] 表名; --创建新表,如果存在则返回 create t ...
- ionic+vue+capacitor系列笔记--02项目中集成Capacitor,添加android,ios平台,真机运行项目
Capacitor是什么? Capacitor是由ionic团队开发的一款跨平台移动应用构建工具,可轻让我们轻松的构建Android.iOS.Electron和Web应用程序. Capacitor是A ...
- 【Unity 框架】 QFramework v1.0 使用指南 工具篇: 16. LiveCodingKit 写代码不用停止运行的利器 | Unity 游戏框架 | Unity 游戏开发 | Unity 独立游戏
我们在用 Unity 开发的时候,每次编写或修改一点代码就需要进行 停止运行->编写代码->等待编译->运行游戏. 而在很多情况下这个过程是一个比较耗神的过程,因为开发者需要等待,还 ...
- 各种排序算法实现(JAVA)
转载: https://blog.csdn.net/qq_42453117/article/details/100036347 Exer010Sort01BubbleSortV1 import ja ...
- 【unity】EventSystem.current.IsPointerOverGameObject()报空指针错误的解决
我前几天遇见了EventSystem.current.IsPointerOverGameObject()这么个问题,只要我一触发就给我报空指针!!我那时根本就没动这段码,但就是报错!!!卡了老子十小时 ...
- git分支的一些处理情况记录
一.开发分支(dev)上的代码更新后,要合并到 master 分支 git checkout dev #切换到dev分支 git pull #将远程更新的代码同步到本地 git checkout ma ...
- Docker安装elasticsearch、kibana
一.Docker 安装elasticsearch 7.10 1. 拉取镜像 docker pull docker.elastic.co/elasticsearch/elasticsearch:7.10 ...