Repository navigation
Expand file tree
/
Copy pathatom.xml
More file actions
111 lines (98 loc) · 19.2 KB
/
Copy pathatom.xml
File metadata and controls
111 lines (98 loc) · 19.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
<?xml version="1.0" encoding="utf-8"?>
<feed xmlns="http://www.w3.org/2005/Atom">
<title><![CDATA[Bin's blog]]></title>
<link href="/atom.xml" rel="self"/>
<link href="http://yoursite.com/"/>
<updated>2015-11-03T16:07:10.208Z</updated>
<id>http://yoursite.com/</id>
<author>
<name><![CDATA[Wayne Wu]]></name>
</author>
<generator uri="http://hexo.io/">Hexo</generator>
<entry>
<title><![CDATA[Java Servlet 基础原理]]></title>
<link href="http://yoursite.com/2015/11/03/Java-Servlet-basic/"/>
<id>http://yoursite.com/2015/11/03/Java-Servlet-basic/</id>
<published>2015-11-03T15:04:14.000Z</published>
<updated>2015-11-03T16:07:10.208Z</updated>
<content type="html"><![CDATA[<h3 id="问题:Servlet如何工作?Servlet_如何初始化、共享变量、多线程处理"><strong>问题:Servlet如何工作?Servlet 如何初始化、共享变量、多线程处理</strong></h3><p>假设我有一个运行了大量 Servlet 的 web 服务器。通过 Servlet 之间传输信息得到 Servlet 上下文,并设置 session 变量。</p>
<p>现在,如果有两名或更多使用者向这个服务发送请求,接下来 session 变量会发生什么变化?究竟是所有用户都是用共同的变量?还是不同的用户使用的变量都不一样?如果是后者,服务器如何区分不同用户?</p>
<p>另一个相似的问题,如果有 <em>n</em> 名用户访问一个特定的 Servlet,那么该 Servlet 是仅在第一个用户首次访问的时候实例化,还是分别为每个用户实例化?<br><a id="more"></a></p>
<h3 id="回答"><strong>回答</strong></h3><h4 id="1-_容器"><strong>1. 容器</strong></h4><p>servlet 工作原理得从servlet 容器开始说起。</p>
<p>题外话:什么是容器?<br>容器便是接收放置某东西的器皿,而servlet 容器形象的阐释了用来放置servlet的“器皿”</p>
<p>Tomcat web容器其实指的就是servlet容器<br><img src="http://www.ibm.com/developerworks/cn/java/j-lo-servlet/image002.jpg" alt="图一"></p>
<p>从上图就可以看出,tomcat容器等级中,Context 容器是直接管理 Servlet 在容器中的包装类 Wrapper,所以 Context 容器如何运行将直接影响 Servlet 的工作方式。</p>
<h4 id="2-_容器加载"><strong>2. 容器加载</strong></h4><p>当 Servlet 容器(比如 Apache Tomcat)启动后,会部署和加载所有 web 应用。当web 应用被加载,Servlet 容器会创建一次ServletContext,然后将其保存在服务器的内存中。<br>web 应用的 web.xml 被解析,找到其中所有 servlet、filter 和 Listener 或 @WebServlet、@WebFilter 和 @WebListener 注解的内容,创建一次并保存到服务器的内存中。</p>
<p><img src="http://pic002.cnblogs.com/images/2011/348076/2011110919004322.jpg" alt=""></p>
<p>当问题中的 Servlet 配置的 load-on-startup 或者 @WebServlet(loadOnStartup) 设置了一个大于 0 的值,则同样会在启动web 应用的时候立即调用 init() 方法。</p>
<p>“load-on-startup”中的值表示那些Servlet会以相同顺序初始化。如果配置的值相同,会遵循 web.xml 中指定的顺序或 @WebServlet 类加载的顺序。</p>
<p>另外,如果不设置 “load-on-startup” 值,init() 方法只在第一次 HTTP 请求命中问题中的 Servlet 时才被调用。也就是说 当第一次请求该servlet,web容器将其初始化,并调用init(),下一次再有同样的servlet请求到来时,web 容器会先从内存中查看是否存在该servlet实例,如果有则返回该实例,反之则重新创建servlet实例对象</p>
<h4 id="3-_容器停止"><strong>3. 容器停止</strong></h4><p>当 Servlet 容器停止,将卸载所有 web 应用,调用所有初始化的 Servlet 和过滤器的 destroy() 方法,最后回收 ServletContext 和所有 Servlet、Filter 与 Listener 实例。</p>
<h4 id="4-_HttpSession"><strong>4. HttpSession</strong></h4><p>当用户第一次访问该 web 应用时,会通过 request.getSession() 第一次获得 HttpSession。之后 Servlet 容器将会创建 HttpSession,生成一个唯一的 ID(可以通过 session.getId() 获取)并储存在服务器内存中。然后 Servlet 容器在该次 HTTP 响应的 Set-Cookie 头部设置一个 Cookie,以 JSESSIONID 作为 Cookie 名字,那个唯一的 session ID 作为 Cookie 的值。</p>
<p>你可以在 web.xml 中设置 session-timeout ,默认值为 30 分钟。超时到达之前 HttpSession 会一直存活。所以当客户端不再访问该 web 应用超过 30 分钟后,Servlet 容器就会回收这个 session。后续每个请求,即使指定 cookie 名称也不能再访问到相同的 session。Servlet 容器会创建一个新的 Cookie。</p>
<p>另一方面,客户端上的 session cookie 有一个默认存活时间,该事件和该浏览器实例运行时间一样长。所以,当客户端关闭该浏览器实例(所有标签和窗口)后,这个 session 就会被客户端回收。新浏览器实例不再发送与该 session 关联的 cookie。一个新的 request.getSession() 将会返回新的 HttpSession 并设置一个拥有新 session ID 的 cookie。<br><img src="http://www.ibm.com/developerworks/cn/java/j-lo-servlet/image024.jpg" alt=""></p>
<h4 id="5-_线程安全"><strong>5. 线程安全</strong></h4><p>java servlet看似是单例,其实不是,只是servlet容器在处理的时候做成单例的样子,因为每一个http请求到达后,servlet容器会查询内存中是否有对应的servlet实例。<br>所以所有的servlet和filter都是被request和session共享</p>
<p>所以不要将任何数据赋值给servlet或filter<br>线程不安全例子<br><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">ExampleServlet</span> <span class="keyword">extends</span> <span class="title">HttpServlet</span> </span>{</span><br><span class="line"> </span><br><span class="line"> <span class="keyword">private</span> Object thisIsNOTThreadSafe;</span><br><span class="line"> </span><br><span class="line"> <span class="function"><span class="keyword">protected</span> <span class="keyword">void</span> <span class="title">doGet</span><span class="params">(HttpServletRequest request, HttpServletResponse response)</span> <span class="keyword">throws</span> ServletException, IOException </span>{</span><br><span class="line"> Object thisIsThreadSafe;</span><br><span class="line"> </span><br><span class="line"> thisIsNOTThreadSafe = request.getParameter(<span class="string">"foo"</span>); <span class="comment">// BAD!! Shared among all requests!</span></span><br><span class="line"> thisIsThreadSafe = request.getParameter(<span class="string">"foo"</span>); <span class="comment">// OK, this is thread safe.</span></span><br><span class="line"> }</span><br></pre></td></tr></table></figure></p>
<h4 id="参考"><strong>参考</strong></h4><ol>
<li><a href="http://mp.weixin.qq.com/s?__biz=MjM5NzMyMjAwMA==&mid=400318743&idx=2&sn=b42f0355b59b8c170647254ad0102ce2&scene=1&srcid=1103byB6my5rbYMTWfJEgmYp#rd" target="_blank" rel="external">Java servlet 工作原理</a></li>
<li><a href="http://www.cnblogs.com/mount/archive/2011/11/09/2243490.html" target="_blank" rel="external">Servlet工作原理解析</a></li>
</ol>
]]></content>
<summary type="html">
<![CDATA[<h3 id="问题:Servlet如何工作?Servlet_如何初始化、共享变量、多线程处理"><strong>问题:Servlet如何工作?Servlet 如何初始化、共享变量、多线程处理</strong></h3><p>假设我有一个运行了大量 Servlet 的 web 服务器。通过 Servlet 之间传输信息得到 Servlet 上下文,并设置 session 变量。</p>
<p>现在,如果有两名或更多使用者向这个服务发送请求,接下来 session 变量会发生什么变化?究竟是所有用户都是用共同的变量?还是不同的用户使用的变量都不一样?如果是后者,服务器如何区分不同用户?</p>
<p>另一个相似的问题,如果有 <em>n</em> 名用户访问一个特定的 Servlet,那么该 Servlet 是仅在第一个用户首次访问的时候实例化,还是分别为每个用户实例化?<br>]]>
</summary>
<category term="Java,Servlet" scheme="http://yoursite.com/tags/Java-Servlet/"/>
</entry>
<entry>
<title><![CDATA[用hexo和github搭建自己的博客]]></title>
<link href="http://yoursite.com/2015/10/22/how-to-setup-hexoblog/"/>
<id>http://yoursite.com/2015/10/22/how-to-setup-hexoblog/</id>
<published>2015-10-22T08:45:55.000Z</published>
<updated>2015-11-03T16:00:29.095Z</updated>
<content type="html"><![CDATA[<p>之前用过wordpress配置自己的博客空间,但是你得有个服务器托管。而目前github免费服务器托管解决了这事,且拥有300MB空间<br>对于静态页面处理,这些容量还是绰绰有余。</p>
<p>如何生成静态页面呢?</p>
<p>可以利用hexo功能,将你的文本自动转化为静态页面,然后通过hexo 部署到github服务器上。</p>
<a id="more"></a>
<h2 id="环境准备"><strong>环境准备</strong></h2><h4 id="安装NodeJS"><strong>安装NodeJS</strong></h4><p>本人使用的是windows 64位操作系统,所以下载对应的<a href="https://nodejs.org/en/" target="_blank" rel="external">nodejs容器</a></p>
<p><img src="http://ww2.sinaimg.cn/large/74311666jw1exasnejv5kj20kr05aaaf.jpg" alt="图一"></p>
<h4 id="安装git"><strong>安装git</strong></h4><p>git shell <a href="https://git-scm.com/downloads" target="_blank" rel="external">下载地址</a> 务必安装,后面hexo安装需要借助于git shell command窗口指令<br>git客户端很多,我选用的是<a href="https://desktop.github.com/" target="_blank" rel="external">github desktop</a></p>
<h4 id="安装markdown编辑器"><strong>安装markdown编辑器</strong></h4><p>小书匠 编辑器不错,既有本地客户端软件,又有web 版本<br>在线编辑 <a href="http://markdown.xiaoshujiang.com/" target="_blank" rel="external">小书匠</a></p>
<h2 id="GitHub"><strong>GitHub</strong></h2><p>1.注册github账号,账户名==your_name==<br>2.创建与你名字对应的仓库==your_name==.github.io<br>3.添加SSH公钥到account settings里</p>
<h5 id="前两步骤不说了,直接跳到第三步"><strong>前两步骤不说了,直接跳到第三步</strong></h5><h4 id="1-_生成公钥"><strong>1. 生成公钥</strong></h4><p>打开gitshell<br>输入下列指令<br><figure class="highlight"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">git config --global user.email "${your_account}@qq.com" git config --global user.name "${your_name}"</span><br></pre></td></tr></table></figure></p>
<p>生成密钥:<br><figure class="highlight"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">ssh-keygen -t rsa -C "${your_account}@qq.com"</span><br></pre></td></tr></table></figure></p>
<p>随后需要输入公钥保存路径<br><figure class="highlight"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">Enter file in which to save the key (/Users/your_user_directory/.ssh/id_rsa):<回车就好></span><br></pre></td></tr></table></figure></p>
<p>然后系统会要你输入加密串(Passphrase):可以直接回车<br><figure class="highlight"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">Enter passphrase (empty for no passphrase):<输入加密串> Enter same passphrase again:<再次输入加密串></span><br></pre></td></tr></table></figure></p>
<p>当你看到下面情况说明已经搞定<br><figure class="highlight ruby"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="constant">Your</span> identification has been saved <span class="keyword">in</span> /c/<span class="constant">Users</span>/<span class="constant">IBM_ADMIN</span>/.ssh/id_rsa.</span><br><span class="line"><span class="constant">Your</span> public key has been saved <span class="keyword">in</span> /c/<span class="constant">Users</span>/<span class="constant">IBM_ADMIN</span>/.ssh/id_rsa.pub.</span><br></pre></td></tr></table></figure></p>
<h4 id="2-添加SSH_Key到github"><strong>2.添加SSH Key到github</strong></h4><p>在本机设置SSH Key之后,需要添加到GitHub上,以完成SSH链接的设置。</p>
<p>用文本编辑工具打开id_rsa.pub文件,如果看不到这个文件,你需要设置显示隐藏文件。准确的复制这个文件的内容,才能保证设置的成功。</p>
<p>在GitHub的主页上点击设置按钮:<br><img src="http://ww1.sinaimg.cn/large/74311666jw1exasool7pvj20s70h6jv1.jpg" alt=""></p>
<p><img src="http://ww2.sinaimg.cn/large/74311666jw1exaspnn35tj20ki020mxc.jpg" alt=""><br>将选择SSH Keys项,把复制的内容粘贴进去,然后点击Add Key按钮即可:</p>
<p><img src="http://ww2.sinaimg.cn/large/74311666jw1exasqe20sdj20v90l8ju7.jpg" alt=""></p>
<h4 id="3-测试一下"><strong>3.测试一下</strong></h4><p>可以输入下面的命令,看看设置是否成功,==git@github.com==的部分不要修改:<br><figure class="highlight nginx"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="title">ssh</span> -T git<span class="variable">@github</span>.com</span><br></pre></td></tr></table></figure></p>
<p>如果显示下列情况,说明OK<br><figure class="highlight applescript"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">You've successfully authenticated, <span class="keyword">but</span> GitHub <span class="keyword">does</span> <span class="keyword">not</span> provide shell access.</span><br></pre></td></tr></table></figure></p>
<h2 id="安装HEXO"><strong>安装HEXO</strong></h2><p>在安装hexo之前,一般都会在本地创建repo 文件夹,例如:<br>==C:\Users\IBM_ADMIN\Documents\GitHub==<br>随后在这个路径下创建 hexo文件夹<br>==C:\Users\IBM_ADMIN\Documents\GitHub\hexo==</p>
<h4 id="1-安装hexo"><strong>1.安装hexo</strong></h4><p>打开git shell,然后执行下面cmd 指令<br><figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">npm <span class="operator"><span class="keyword">install</span> -<span class="keyword">g</span> hexo</span></span><br></pre></td></tr></table></figure></p>
<h4 id="2-初始化hexo"><strong>2.初始化hexo</strong></h4><p>进入到hexo文件夹,然后输入指令<br><figure class="highlight nginx"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="title">hexo</span> init</span><br></pre></td></tr></table></figure></p>
<h4 id="3-生成静态页面"><strong>3.生成静态页面</strong></h4><p>生成的静态页面在${path}/hexo/public<br><figure class="highlight gcode"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">hexo <span class="keyword">ge</span><span class="label">nerate</span></span><br></pre></td></tr></table></figure></p>
<h4 id="4-本地启动"><strong>4.本地启动</strong></h4><p>执行以下指令<br><figure class="highlight axapta"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">hexo <span class="keyword">server</span></span><br></pre></td></tr></table></figure></p>
<p>随后打开 <a href="http://localhost:4000" target="_blank" rel="external">http://localhost:4000</a></p>
<h4 id="5-_部署到github"><strong>5. 部署到github</strong></h4><p>首修改你的 ==_config.yml== 文件</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">deploy:</span><br><span class="line"> <span class="built_in">type</span>: git</span><br><span class="line"> repository: https://github.com/<span class="variable">${your_github_name}</span>/<span class="variable">${your_repo_name}</span>.git</span><br><span class="line"> branch: master</span><br></pre></td></tr></table></figure>
<p>注意type类型是目前的hexo最新版本是 ==git== 而<strong>不是</strong> ==github==<br>另外 <strong>特别注意</strong></p>
<p>==type:== ==repository:== ==repository:== 这三个后面的value 中间必须有空格</p>
<p>随后输入指令<br><figure class="highlight nginx"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="title">hexo</span> deploy</span><br></pre></td></tr></table></figure></p>
<p>最后你会看到你的github上对应的repo 里出现hexo的一些本地配置<br><img src="http://ww3.sinaimg.cn/large/74311666jw1exasr8g9e7j20t10g5q6c.jpg" alt=""></p>
<p>最后可以访问你的github 博客<br><a href="http://{your_repo_name}" target="_blank" rel="external">http://{your_repo_name}</a><br>我的<a href="http://binwayne.github.io" target="_blank" rel="external">博客地址</a></p>
<h2 id="结束语"><strong>结束语</strong></h2><p>如果你跟着这篇不那么详尽的教程,成功搭建了自己的博客,恭喜你!剩下的就是保持热情的去写自己的文章吧。</p>
<h3 id="参考">参考</h3><p><a href="http://ibruce.info/2013/11/22/hexo-your-blog/" target="_blank" rel="external">hexo你的博客</a> - ibruce<br><a href="http://yangjian.me/workspace/building-blog-with-hexo/" target="_blank" rel="external">使用hexo搭建博客</a> - Alimon’s blog<br><a href="http://zipperary.com/categories/hexo/" target="_blank" rel="external">zipperary hexo 博客搭建</a> - Zipperary<br><a href="http://www.jianshu.com/p/05289a4bc8b2" target="_blank" rel="external">hexo 教程</a> - hexo 教程</p>
]]></content>
<summary type="html">
<![CDATA[<p>之前用过wordpress配置自己的博客空间,但是你得有个服务器托管。而目前github免费服务器托管解决了这事,且拥有300MB空间<br>对于静态页面处理,这些容量还是绰绰有余。</p>
<p>如何生成静态页面呢?</p>
<p>可以利用hexo功能,将你的文本自动转化为静态页面,然后通过hexo 部署到github服务器上。</p>]]>
</summary>
<category term="hexo,github,博客" scheme="http://yoursite.com/tags/hexo-github-%E5%8D%9A%E5%AE%A2/"/>
</entry>
</feed>