Servlet知识点

转自:http://blog.csdn.net/haofengjiao/article/details/70184138?locationNum=3&fps=1

1.servlet的作用

处理请求,生成响应.


2.创建servlet的方式
1>实现servlet接口
2>继承javax.servlet.GenericServlet

3>继承javax.servlet.http.HttpServlet

方式一代码(实现servlet接口):

[java] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. package com.jyh.servlet;  
  2.   
  3. import java.io.IOException;  
  4.   
  5. import javax.servlet.Servlet;  
  6. import javax.servlet.ServletConfig;  
  7. import javax.servlet.ServletException;  
  8. import javax.servlet.ServletRequest;  
  9. import javax.servlet.ServletResponse;  
  10.   
  11. public class AServlet implements Servlet {  
  12.     private ServletConfig config;  
  13.   
  14.     public void init(ServletConfig arg0) throws ServletException {  
  15.         this.config = arg0;  
  16.         System.out.println("调用init方法");  
  17.     }  
  18.       
  19.     public void service(ServletRequest arg0, ServletResponse arg1)  
  20.             throws ServletException, IOException {  
  21.         arg1.getWriter().print("hello word!");  
  22.         System.out.println("调用service方法");  
  23.     }  
  24.   
  25.     public void destroy() {  
  26.         System.out.println("调用destroy方法");  
  27.     }  
  28.       
  29.     public ServletConfig getServletConfig() {  
  30.         return config;  
  31.     }  
  32.   
  33.     public String getServletInfo() {  
  34.         return null;  
  35.     }  
  36. }  


3.Servlet的生命周期方法
生命周期指的是 必须要经历的过程.
对于servlet来讲,有3个生命周期方法.
1> 出生 ==> init方法.在构造方法调用之后 调用.

2> 使命 ==> service方法 ,当请求发来时,处理请求使用.

3> 销毁 ==> destory方法, 当服务器关闭时,会销毁servlet,在销毁之前调用该方法释放资源.


4.Servlet中的其他方法.
getServletInfo ==> 获得servlet的信息(版本,作者,版权..),没用.

getServletConfig ==> 返回servletConfig对象.


5.ServletConfig对象
ServletConfig封装了servlet在web.xml中的配置.
方法:
1>getServletName ==> 获得配置文件中 <servlet-name> 元素的内容
2>getInitParameter ==> 根据 <init-param>中的 <param-name> 获得 </param-value>
<init-param>
<param-name>name</param-name>
<param-value>tom</param-value>
</init-param>
3>getInitParameterNames 返回所有<param-name> .

4>getServletContext

创建继承servlet方式二(继承javax.servlet.GenericServlet)

[java] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. package com.jyh.servlet;  
  2.   
  3. import java.io.IOException;  
  4. import java.util.Enumeration;  
  5.   
  6. import javax.servlet.Servlet;  
  7. import javax.servlet.ServletConfig;  
  8. import javax.servlet.ServletContext;  
  9. import javax.servlet.ServletException;  
  10. import javax.servlet.ServletRequest;  
  11. import javax.servlet.ServletResponse;  
  12.   
  13. public abstract class MyGernericServlet implements Servlet, ServletConfig {  
  14.   
  15.   
  16.     private ServletConfig config;  
  17.   
  18.     public void init(ServletConfig config) throws ServletException {  
  19.         this.config = config;  
  20.         init();  
  21.     }  
  22.   
  23.     //该重载方法是给继承该类的人重写所用,因为初始化方法必须要初始化config  
  24.     //为了避免重写init方法的人没有初始化config,所以重载了init方法  
  25.     public void init() {  
  26.     }  
  27.   
  28.     //由于service是处理请求响应的地方,所以将service声明成抽象方法,让继承该类的人去实现  
  29.     public abstract void service(ServletRequest req, ServletResponse res)  
  30.             throws ServletException, IOException;  
  31.       
  32.       
  33.     /* 
  34.      * 将ServletConfig对象中的四个方法单独抽离出来 
  35.      */  
  36.     public String getServletName() {  
  37.         return getServletConfig().getServletName();  
  38.     }  
  39.   
  40.     public String getInitParameter(String name) {  
  41.         return getServletConfig().getInitParameter(name);  
  42.     }  
  43.   
  44.     public Enumeration<String> getInitParameterNames() {  
  45.         return getServletConfig().getInitParameterNames();  
  46.     }  
  47.   
  48.     public ServletContext getServletContext() {  
  49.         return getServletConfig().getServletContext();  
  50.     }  
  51.   
  52.       
  53.       
  54.     public ServletConfig getServletConfig() {  
  55.         return config;  
  56.     }  
  57.   
  58.   
  59.     public String getServletInfo() {  
  60.         return null;  
  61.     }  
  62.   
  63.     public void destroy() {  
  64.     }  
  65.   
  66. }  


创建servlet方式三(继承javax.servlet.http.HttpServlet):

[java] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. package com.jyh.servlet;  
  2.   
  3. import java.io.IOException;  
  4.   
  5. import javax.servlet.ServletException;  
  6. import javax.servlet.ServletRequest;  
  7. import javax.servlet.ServletResponse;  
  8. import javax.servlet.http.HttpServletRequest;  
  9. import javax.servlet.http.HttpServletResponse;  
  10.   
  11. public abstract class MyHttpServlet extends MyGernericServlet {  
  12.     private final String Http_GET = "GET";  
  13.     private final String Http_POST = "POST";  
  14.       
  15.   
  16.     //因为JavaWeb项目大多数都是基于Http协议的  
  17.     //所以将request和response对象强转成HttpServletRequest和HttpServletResponse  
  18.     public void service(ServletRequest req, ServletResponse res)  
  19.             throws ServletException, IOException {  
  20.           
  21.         HttpServletRequest httpReq = null;  
  22.         HttpServletResponse httpRes = null;  
  23.           
  24.         try{  
  25.             httpReq = (HttpServletRequest) req;  
  26.             httpRes = (HttpServletResponse) res;  
  27.         }catch(ClassCastException e){  
  28.             throw new ServletException("异常");  
  29.         }  
  30.         service(httpReq, httpRes);  
  31.     }  
  32.       
  33.     //根据请求方式调用不同的方法  
  34.     public void service(HttpServletRequest httpReq, HttpServletResponse httpRes)  
  35.             throws ServletException, IOException {  
  36.         String method = httpReq.getMethod();  
  37.         if(method.equals(Http_GET)){  
  38.             doGet(httpReq,httpRes);  
  39.         }else if(method.equals(Http_POST)){  
  40.             doPost(httpReq,httpRes);  
  41.         }  
  42.     }  
  43.   
  44.     public void doPost(HttpServletRequest httpReq,HttpServletResponse httpRes) {  
  45.     }  
  46.   
  47.     public void doGet(HttpServletRequest httpReq,HttpServletResponse httpRes) {  
  48.     }  
  49.   
  50. }  

 
6.Servlet中的其他细节
1>关于Servlet线程安全问题
因为在servlet运行期间只有一个servlet实例存在.可能会同时处理多个请求.
那么我们在servlet中声明成员变量来存储用户数据是有线程安全问题的.
我们应该如何解决呢?
1.实现SigleThreadModel  (不推荐!)
2.使用局部变量保存用户数据.(推荐使用!)

2>关于servlet随着项目的启动而创建
使用<load-on-startup>配置来实现.
例如:
<servlet>
<servlet-name>AServlet</servlet-name>
<servlet-class>cn.itcast.servlet.hello.AServlet</servlet-class>
<load-on-startup>3</load-on-startup>
</servlet>

填写一个整数,整数越小优先级越高.如果优先级一样,启动顺序按照配置顺序.

3>关于Servlet路径配置问题

设项目名为MyServlet

<url-pattern>  
路径匹配:
/AServlet             http://localhost:8080/MyServlet/AServlet
/ABC/AServlet       http://localhost:8080/MyServlet/ABC/AServlet
/ABC/ABC/AServlet   http://localhost:8080/MyServlet/ABC/ABC/AServlet
/ABC/ABC/*http://localhost:8080/MyServlet/ABC/ABC/oasdojasdjioasd
/*http://localhost:8080/MyServlet/asdiojoiajsidojoasd 
     
后缀名匹配:
*.do http://localhost:8080/MyServlet/asdiojoiajsidojoasd.do
*.action http://localhost:8080/MyServlet/asdiojoiajsidojoasd.action
*.html http://localhost:8080/MyServlet/asdiojoiajsidojoasd.html
注意:1.路径配置的路径匹配范围越大优先级越低.
2.两种匹配模式不能混用. 例如错误的例子: /*.do



ServletContext对象

1.获得: servletConfig.getServletContext
2.servletContext 的作用
1> servletContext 封装了web.xml 中的配置
<context-param>
<param-name>name</param-name>
<param-value>jerry</param-value>
</context-param>
<context-param>
<param-name>password</param-name>
<param-value>1234</param-value>
</context-param>

getInitParameterNames();  ==> 获得所有键
getInitParameter(key);  ==> 根据键获得对应的值
2> servlet技术中3大域对象之一. 
ServletContext对应着Application(应用)域.利用了一个项目中只有一个ServletContext实例的特点.在servletContext中放置了一个map用作数据通信.
这个Map就是所谓域.
关于域的操作,有4个.
放入键值对 setAttribute(key,value)
通过键取值 getAttribute(key)
通过键删除 removeAttribute(key)
遍历所有键 getAttributeNames()
3>获得项目中资源.  
所有servletContext中关于路径的获得,相对路径都是相对的 WebRoot(项目根)下
getRealPath  ==> 通过相对路径获得绝对路径
getResourceAsStream ==> 根据相对路径获得指定资源流

3.servlet技术中对象的范围
servlet ==> 项目启动期间一个servlet只有一个servlet实例
request ==> 项目启动期间,request对象的数量,要看当前有多少个请求正在处理.
response ==> 同上.
servletConfig ==> 一个servlet实例对应一个servletConfig对象
servletContext ==> 整个项目中,永远只有一个servletContext实例存在.


类加载器获得资源和路径


一:使用getClass().getResourceAsStream方法获得资源,相对路径分为两种情况
1: 加"/"  ==> 相对的是classes目录
2: 不加"/" ==> 相对的是本类当前目录也就是src目录
InputStream is = this.getClass().getResourceAsStream("students.xml");
System.out.println(is);
二:使用this.getClass().getClassLoader().getResourceAsStream("");获得资源
只有一个相对路径 ==> 就是相对于 classes目录
InputStream is2 = this.getClass().getClassLoader().getResourceAsStream("students.xml");
System.out.println(is2);


注意: 使用类和类加载器加载资源文件时
1 getClassLoader()原本是用来加载.class文件的, 所以缓存设计的很小.不要用他加载一些别较大的资源.
2 jvm运行期间只加载一次. 但是使用下面的代码可以解决这个问题(不获取资源,只获取路径).
String path = this.getClass().getClassLoader().getResource("students.xml").getPath();
File file = new File(path.substring(1, path.length()));
System.out.println(path);

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值