Web02。 Context方法。response 方法。案例文件下载

1 篇文章 0 订阅

doGet

Context
1.ServletContext context = request.getServletContext(); -----获得
2.context.getContextPath();------获得应用名
3.context.getInitParameter(“driver”);–获得全局参数
4.context.getRealPath(“WEB-INF/classes/a.txt”);–通过资源的相对路径获得绝对路径(这里的资源路径为Web02)

5.//访问量
ServletContext context = request.getServletContext();
context.setAttribute(“fwl”,“访问量:”+i++);
Object fwl = context.getAttribute(“fwl”);
System.out.println(fwl);
6.//添加属性名,值
context.setAttribute(“zhangsan”,“十八”);
context.setAttribute(“lisi”, “十九”);
//通过属性名来获得属性值
context.getAttribute(“zhangsan”);

response

1.//重定向最终代码,路径:绝对路径,从应用名开始。(不管什么请求都可以)
response.sendRedirect("/web02/ServletContextDemo2");

2.设置编码
response.setContentType(“text/html; charset =utf-8”);

3.在页面上显示
response.getWriter().write(“hello Bonnie…”);

4.字符流输出
PrintWriter writer = response.getWriter();
writer.println(“哈哈”);
writer.println("
");
writer.println(“hello”);

5.文件下载
(文件在webContext目录下)

	String filename = "a.jpg";
	response.setHeader("Content-Disposition","attachment;filename="+filename);
	String path = request.getServletContext().getRealPath("download/"+filename);
	

	InputStream in = new FileInputStream(path);
	OutputStream out = response.getOutputStream();
	byte[] bs = new byte[1024];
	int len = -1;
	while((len = in.read(bs)) != -1) {
		out.write(bs,0,len);
	}
	in.close();

上课笔记

回顾:
HTTP协议
请求
方式、uri
客户端信息 Map
post参数
响应
状态码
服务器信息 Map
内容

Servlet 规范用来接收请求,返回响应

实现Servlet步骤:
1.自定义类,实现Servlet接口
2.重写service
3.去web.xml注册
	<servlet>
		<servlet-name>自定义名字
		<servlet-class>完整类名
	<servlet-mapping>
		<servlet-name>一致的名字
		<url-pattern>资源路径 /xx
4.访问:http://localhost:8080/应用名/资源名	

Servlet生命周期:- 单例模式
init:对象创建时,初始化调用
	 默认第一次接收请求创建
	 可以修改为服务器开启时创建
	 	<load-on-startup>n
service:每一次请求接收时调用
destroy:对象销毁 服务器关闭

request、response

类路径:WEB-INF/classes

ServletConfig:


driver
com.mysql.jdbc.Driver

HttpServlet 类 间接实现了 Servlet接口
实现了service方法,调用了自己的service方法
判断请求方式,调用 doGet/doPost

发送了10次请求,调用了几次init、doGet/doPost、destroy
几个request、response

web02:
|- hi.html
|- hello
|- kitty.html
|- /* : / 目录下所有未匹配到的Servlet资源
/hello/* : /hello目录下面所有未匹配到的资源
/ : 只要找不到匹配的Servlet,就交给我处理
|- /hello
|- /helloTom

/hello/h

<!-- 方式一:完全匹配 -->
<!-- <url-pattern>/HelloServlet</url-pattern> -->
<!-- 方式二:目录匹配 - Filter/SpringMVC -->
<!-- http://localhost:8080/web02/hello/kitty.html -->
<url-pattern>/hello/*</url-pattern>
<!-- 方式三:后缀匹配 - 管理系统 -->
<!-- <url-pattern>*.html</url-pattern> -->

Servlet是接收请求的规范

结论:所有的资源都是由 Servlet 来处理的
html jpg css js jsp … .do …Servlet

 有一个默认的Servlet来接收web.xml中匹配不到的资源 
 全局的web.xml中配置了缺省/默认的Servlet来接收其他请求
 	DefaultServlet
 	<servlet-mapping>
        <servlet-name>default</servlet-name>
        <url-pattern>/</url-pattern>
	</servlet-mapping>

总结:
web02
|- a.html
|- b.jpg
|- hello/c.html
— Servlet
|- /hello
|- /a
|- *.do

localhost:8080/web02/hello  -- 96
localhost:8080/web02/h      -- 99
localhost:8080/web02/a.html -- 99
localhost:8080/web02/b.jpg  -- 99 |- /*
localhost:8080/web02/b.jpg.do -- 98

localhost:8080/web02/a.html -- DefaultServlet[/] -> 静态资源 -- 92

localhost:8080/web02/a1.html -- default - 404

DefaultServlet -- 全局的web.xml中

ServletContext -> ApplicationContext
应用上下文:属于应用,一个应用只有一个上下文
创建:服务器开启
销毁:服务器关闭

1.获得ServletContext
	  config.getServletContext()
2.request.getServletContext();
	小范围对象可以获得大范围对象的引用
3.servlet.getServletContext();
location c3p0-config.xml

ServletContext 和 ServletContext域

域对象:存取数据使用 Map<String, Object>
域范围:参考生命周期
setAttribute(key, value)
value getAttribute(key)
removeAttribute(key);

HttpServletResponse -> ServletResponse
response:
响应行:状态码
响应头:服务器信息
响应体:内容

删除:
1.发送请求,servlet接收到,执行delete操作
2.重定向 - select
3.重新回到了邮件列表 select

验证码:防止机器人攻击

tomcat编码:
ISO8859-1

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值