writedby 张艳涛,在学第9章session的时候,做了个实验在给的demo代码中添加了

package com.zyt.tomcat.ex09.core;

public class SimpleWrapperValve implements Valve, Contained {

    protected Container container;

    public void invoke(Request request, Response response, ValveContext valveContext)
throws IOException, ServletException { SimpleWrapper wrapper = (SimpleWrapper) getContainer();
ServletRequest sreq = request.getRequest();
ServletResponse sres = response.getResponse();
Servlet servlet = null;
HttpServletRequest hreq = null;
if (sreq instanceof HttpServletRequest)
hreq = (HttpServletRequest) sreq;
HttpServletResponse hres = null;
if (sres instanceof HttpServletResponse)
hres = (HttpServletResponse) sres; //-- new addition -----------------------------------
Context context = (Context) wrapper.getParent();
request.setContext(context);
//-------------------------------------
// Allocate a servlet instance to process this request
try {
servlet = wrapper.allocate();
if (hres!=null && hreq!=null) {
servlet.service(hreq, hres);
}
else {
servlet.service(sreq, sres);
}
}
catch (ServletException e) {
}
} public String getInfo() {
return null;
} public Container getContainer() {
return container;
} public void setContainer(Container container) {
this.container = container;
}
}

当看看11章的时候,使用 StandardWrapper,再次调用

        System.setProperty("catalina.base", System.getProperty("user.dir"));
HttpConnector connector = new HttpConnector();
StandardWrapper wrapper1 = new StandardWrapper();
wrapper1.setName("Primitive");
wrapper1.setServletClass("PrimitiveServlet");
wrapper1.setDebug(2); StandardWrapper wrapper2 = new StandardWrapper();
wrapper2.setName("Modern");
wrapper2.setServletClass("ModernServlet");
wrapper2.setDebug(2); Wrapper wrapper3 = new StandardWrapper();
wrapper3.setName("SessionZYT");
wrapper3.setServletClass("SessionServletZYT");
//wrapper3.setDebug(2); Context context= new StandardContext();
context.setPath("/myApp");
context.setDocBase("myApp"); LifecycleListener listener = new SimpleContextConfig();
((Lifecycle) context).addLifecycleListener(listener); context.addChild(wrapper1);
context.addChild(wrapper2);
context.addChild(wrapper3);
context.addServletMapping("/myApp/SessionZYT","SessionZYT");

这是如果你调用/myApp/SessionZYT  会报错,追随代码的时候发现了request对象没有context成员变量,所以

SessionServletZYT 类编译了,放在app目录下
import javax.servlet.ServletException;
import javax.servlet.SingleThreadModel;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.io.PrintWriter; public class SessionServletZYT extends HttpServlet implements SingleThreadModel {
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
System.out.println("SessionServlet -- service");
response.setContentType("text/html");
PrintWriter out = response.getWriter();
out.println("<html>");
out.println("<head><title>SessionServlet</title></head>");
out.println("<body>");
String value = request.getParameter("value");
HttpSession session = request.getSession(true);
out.println("<br>the previous value is " +
(String) session.getAttribute("value"));
out.println("<br>the current value is " + value);
session.setAttribute("value", value);
out.println("<br><hr>");
out.println("<form>");
out.println("New Value: <input name=value>");
out.println("<input type=submit>");
out.println("</form>");
out.println("</body>");
out.println("</html>");
}
}

这句报错,要通过request.getContext() -->再次context.getManager(),查了半天也没答案,所以看到13章host就找到了设置的地方了

StandardHostMapper类中
public Container map(Request request, boolean update) {
// Has this request already been mapped?
if (update && (request.getContext() != null))
return (request.getContext()); // Perform mapping on our request URI
String uri = ((HttpRequest) request).getDecodedRequestURI();
Context context = host.map(uri); // Update the request (if requested) and return the selected Context
if (update) {
request.setContext(context);
if (context != null)
((HttpRequest) request).setContextPath(context.getPath());
else
((HttpRequest) request).setContextPath(null);
}
return (context); }

标记的就是 给request 设置了context的了

深入刨析tomcat 之---第9篇 how tomcat works 第9章,Session的实现 关于request.setContext(context)的更多相关文章

  1. 深入刨析tomcat 之---第8篇 how tomcat works 第11章 11.9应用程序,自定义Filter,及注册

    writed by 张艳涛, 标签:全网独一份, 自定义一个Filter 起因:在学习深入刨析tomcat的学习中,第11章,说了调用过滤链的原理,但没有给出实例来,自己经过分析,给出来了一个Filt ...

  2. 深入刨析tomcat 之---第12篇 how tomcat works( 第17章 ) 解析catalina.bat 梳理启动流程

    我们如何启动tomcat呢? 答案是双击startup.bat文件,这个文件在bin目录下 @echo off    不显示批处理命令 rem Licensed to the Apache Softw ...

  3. 深入刨析tomcat 之---第11篇 how tomcat works( 第15章 ) 如何解析web.xml 文件

    writedby 张艳涛 记得当年是学习jsp的时候,写过web.xml中的标签.在之后的springmvc中也是有关于配置mvc 过滤器 和dispatchServlet的标签,之前是看不懂呢!看到 ...

  4. 深入刨析tomcat 之---第10篇 how tomcat works 第13章,Response 发送错误信息 sendError

    writedby 张艳涛 在浏览器中发送一个错误应用url 那么tomcat是如何发送错误的呢? 基本上是发送http 的response协议,分为两部分一部分是response设置头信息, 那么先分 ...

  5. 深入刨析tomcat 之---第6篇 how tomcat works 第5章 容器实现原理

    writedby 张艳涛

  6. 深入刨析tomcat 之---第2篇,解决第3章bug 页面不显示内容http://localhost:8080/servlet/ModernServlet?userName=zhangyantao&password=1234

    writedby 张艳涛7月2日, 在学习第4张的过程中,发现了前一篇文章写的是关于1,2张的bug不用设置response响应头,需要在servlet的service()方法里面写是错误想 serv ...

  7. Orchard 刨析:导航篇

    之前承诺过针对Orchard Framework写一个系列.本应该在昨天写下这篇导航篇,不过昨天比较累偷懒的去玩了两盘单机游戏哈哈.下面进入正题. 写在前面 面向读者 之前和本文一再以Orchard ...

  8. 30s源码刨析系列之函数篇

    前言 由浅入深.逐个击破 30SecondsOfCode 中函数系列所有源码片段,带你领略源码之美. 本系列是对名库 30SecondsOfCode 的深入刨析. 本篇是其中的函数篇,可以在极短的时间 ...

  9. Orchard 刨析:Logging

    最近事情比较多,有预研的,有目前正在研发的,都是很需要时间的工作,所以导致这周只写了两篇Orchard系列的文章,这边不能保证后期会很频繁的更新该系列,但我会写完这整个系列,包括后面会把正在研发的东西 ...

随机推荐

  1. 合宙Luat | 电源设计——模块应用必看的2个要点

    在模块应用设计中,电源设计是很重要的一部分. 由于射频发射时,会在短时间有一个较大电流的突发脉冲.在突发脉冲阶段内,电源必须能够提供高的峰值电流,不然有可能会引起供电电压的跌落.   而很多初学的朋友 ...

  2. 初学springboot

    现在总是与数据库和前端打交道,让我觉得好厌烦,还是喜欢敲代码.最近问了几个朋友,都说潮流要学springCloud,然后学springCloud又要先学springboot,所以这段时间我会慢慢把sp ...

  3. ANDROID开发 Fatal signal 11(SIGSEGV) at 0x问题解决方案

    最近做ANDROID开发,也遇到了很多程序员遇到的一个问题:FATAL SIGNAL 11(SIGSEGV) at 0xxxxx,自然是各种搜索是否有人已然解决,虽然搜索出来的已有案例不少,基本都是内 ...

  4. 【进阶之路】深入理解Java虚拟机的类加载机制(长文)

    我们在参加面试的时候,经常被问到一些关于类加载机制的问题,也都会在面试之前准备的时候背好答案,但是我们是否有去深入了解什么是类加载机制呢?这段时间因为一些事情在家看了些书,这次就和大家分享一些关于Ja ...

  5. CentOS安装ffmpeg+h264

    CentOS安装ffmpeg+h264 前言 坑!坑!坑! 全是坑. 本文安装系统环境为CentOS7.2.我这里提供安装所需的四个源码包,均需要编译安装.所有组件最好都安装最新的稳定版,或者下载使用 ...

  6. Centos7搭建内网DNS服务器

      一.配置阿里云yum源 执行脚本配置阿里云的yum源,已配置yum源的可以忽略 #!/bin/bash # ******************************************** ...

  7. python读取json文件制作股票价格走势

  8. excel替换函数substitute

    1.substitute(需要执行替换操作的单元格区域,旧文本,新文本,第N个旧内容) 例:substitute(A1,"a","b",2)    A1=aab ...

  9. Redis的Pipeline、事务和lua

    1. Pipeline 1.1 Pipeline概念 Redis客户端执行一条命令分别为如下4个过程: 1) 发送命令 2) 命令排队 3) 命令执行 4) 返回结果 其中1)+4)称为Round T ...

  10. log4j和log4j2的配置部分

    log4j和log4j2的功能及形式对比 http://blog.csdn.net/fangaohua200/article/details/53561718 log4j2的proptise配置 ht ...