Servlet 体系结构
Servlet的实现方式
1、实现 javax.servlet.Servlet 接口 ( 最底层,最原始 )
测试案例:
Servlet Life Cycle
Servlet的实现方式:
1、实现 javax.servlet.Servlet 接口 ( 最底层,最原始 )
2、继承 javax.servlet.GenericServlet 类,实现 service 方法
3、继承 javax.servlet.http.HttpServlet 类 ,重写 doGet 和 doPost 方法
4、继承 javax.servlet.http.HttpServlet 类 ,重写 service( HttpServletRequest , HttpServletResponse ) 方法
FirstServlet
ecut.lifecycle.FirstServlet
FirstServlet
/first
SecondServlet
ecut.lifecycle.SecondServlet
SecondServlet
/second
/second/*
ThirdServlet
ecut.lifecycle.ThirdServlet
ThirdServlet
/third
ForthServlet
ecut.lifecycle.ForthServlet
ForthServlet
/forth
LifeCycleServlet
ecut.lifecycle.LifeCycleServlet
0
LifeCycleServlet
/cycle
ConfigurationServlet
ecut.lifecycle.ConfigurationServlet
encoding
UTF-8
content-type
text/html
ConfigurationServlet
/config
package ecut.lifecycle;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.Servlet;
import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
public class FirstServlet implements Servlet {
@Override
public void init(ServletConfig config) throws ServletException {
System.out.println( "初始化" );
}
@Override
public void service(ServletRequest req , ServletResponse resp ) throws ServletException, IOException {
System.out.println( "服务" );
req.setCharacterEncoding( "UTF-8" );
resp.setCharacterEncoding( "UTF-8" );
// 接受来自客户端的数据
String username = req.getParameter( "username" );
System.out.println( "username : " + username );
String password = req.getParameter( "password" );
System.out.println( "password : " + password );
// 向客户端生成响应数据
resp.setContentType( "text/html;charset=UTF-8" );
PrintWriter w = resp.getWriter();
w.println( "你好 , " + username + " !
" );
}
@Override
public void destroy() {
System.out.println( "销毁" );
}
@Override
public ServletConfig getServletConfig() {
return null;
}
@Override
public String getServletInfo() {
return null;
}
}
运行结果如下:
信息: Server startup in 350 ms
初始化
服务
username : AmyZheng
password : 123456
2、继承 javax.servlet.GenericServlet 类,实现 service 方法
测试案例:
package ecut.lifecycle;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.GenericServlet;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
public class SecondServlet extends GenericServlet{
private static final long serialVersionUID = -2575698410270033395L;
@Override
public void service(ServletRequest req , ServletResponse resp) throws ServletException, IOException {
req.setCharacterEncoding( "UTF-8" );
resp.setCharacterEncoding( "UTF-8" );
// 接受来自客户端的数据
String username = req.getParameter( "username" );
System.out.println( "username : " + username );
String password = req.getParameter( "password" );
System.out.println( "password : " + password );
// 向客户端生成响应数据
resp.setContentType( "text/html;charset=UTF-8" );
PrintWriter w = resp.getWriter();
w.println( "你好 , " + username + " !
" );
}
}
运行结果如下:
username : AmyZheng
password : AmyZheng
username : AmyZheng
password : 125678
username : AmyZheng
password : 14789
3、继承 javax.servlet.http.HttpServlet 类 ,重写 doGet 和 doPost 方法
测试案例:
package ecut.lifecycle;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class ThirdServlet extends HttpServlet{
private static final long serialVersionUID = -1117957788110702968L;
@Override
protected void doGet( HttpServletRequest request, HttpServletResponse response ) throws ServletException, IOException {
System.out.println( "GET" );
request.setCharacterEncoding( "UTF-8" );
response.setCharacterEncoding( "UTF-8" );
// 接受来自客户端的数据
String username = request.getParameter( "username" );
System.out.println( "username : " + username );
String password = request.getParameter( "password" );
System.out.println( "password : " + password );
// 向客户端生成响应数据
response.setContentType( "text/html;charset=UTF-8" );
PrintWriter w = response.getWriter();
w.println( "你好 , " + username + " !
" );
}
@Override
protected void doPost( HttpServletRequest request , HttpServletResponse response ) throws ServletException, IOException {
System.out.println( "POST" );
request.setCharacterEncoding( "UTF-8" );
response.setCharacterEncoding( "UTF-8" );
// 接受来自客户端的数据
String username = request.getParameter( "username" );
System.out.println( "username : " + username );
String password = request.getParameter( "password" );
System.out.println( "password : " + password );
// 向客户端生成响应数据
response.setContentType( "text/html;charset=UTF-8" );
PrintWriter w = response.getWriter();
w.println( "你好 , " + username + " !
" );
}
}
运行结果如下:
GET
username : AmyZheng
password : 12589
POST
username : AmyZheng
password : AmyZhengf
4、继承 javax.servlet.http.HttpServlet 类 ,重写 service( HttpServletRequest , HttpServletResponse ) 方法
测试案例:
package ecut.lifecycle;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class ForthServlet extends HttpServlet {
private static final long serialVersionUID = -1403874146599150561L;
@Override
protected void service( HttpServletRequest request, HttpServletResponse response )
throws ServletException, IOException {
request.setCharacterEncoding( "UTF-8" );
response.setCharacterEncoding( "UTF-8" );
// 接受来自客户端的数据
String username = request.getParameter( "username" );
System.out.println( "username : " + username );
String password = request.getParameter( "password" );
System.out.println( "password : " + password );
// 向客户端生成响应数据
response.setContentType( "text/html;charset=UTF-8" );
PrintWriter w = response.getWriter();
w.println( "你好 , " + username + " !
" );
}
}
运行结果如下:
username : AmyZheng
password : AmyZheng1
username : AmyZheng
password : AmyZheng2
Servlet的生命周期
1、加载和实例化
容器只为相应的Servlet初始化一个实例(容器里servlet对象只创建一个)。
2、初始化:调用init() 方法
3、处理请求:调用service() 方法
4、服务结束:调用destory() 方法
tomcat将重启
reload 该webapp
关闭或重启计算机(Servlet容器也随之关闭)
测试案例:
LifeCycleServlet
ecut.lifecycle.LifeCycleServlet
LifeCycleServlet
/cycle
package ecut.lifecycle;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.Servlet;
import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
public class LifeCycleServlet implements Servlet {
static {
System.out.println( "加载并初始化LifeCycleServlet类" );
}
public LifeCycleServlet(){
System.out.println( "创建 LifeCycleServlet 类的实例" );
}
@Override
public void init( ServletConfig config ) throws ServletException {
System.out.println( "对 LifeCycleServlet 实例进行初始化" );
}
@Override
public void service( ServletRequest req , ServletResponse resp ) throws ServletException, IOException {
System.out.println( "LifeCycleServlet 的实例对外提供服务" );
resp.setCharacterEncoding( "UTF-8" );
resp.setContentType( "text/html;charset=UTF-8" );
// 向客户端生成响应数据
PrintWriter w = resp.getWriter();
w.println( "Hello !
" );
}
@Override
public void destroy() {
System.out.println( "准备销毁 LifeCycleServlet 的实例" );
}
@Override
public ServletConfig getServletConfig() {
return null;
}
@Override
public String getServletInfo() {
return null;
}
}
运行结果如下:
信息: Server startup in 337 ms
加载并初始化LifeCycleServlet类
创建 LifeCycleServlet 类的实例
对 LifeCycleServlet 实例进行初始化
LifeCycleServlet 的实例对外提供服务
LifeCycleServlet 的实例对外提供服务
如下修改xml:
LifeCycleServlet
ecut.lifecycle.LifeCycleServlet
0
LifeCycleServlet
/cycle
指定当web 应用启动时,装载Servlet 的次序
如果这个数字是正数或0,则容器先加载数值较小的servlet,再加载数值稍大的
如果是负数或者没有设置,容器将在web 客户端首次访问该servlet 时才加载
运行结果如下:
加载并初始化LifeCycleServlet类
创建 LifeCycleServlet 类的实例
对 LifeCycleServlet 实例进行初始化
四月 08, 2018 7:07:00 下午 org.apache.coyote.AbstractProtocol start
信息: Starting ProtocolHandler ["http-nio-8080"]
四月 08, 2018 7:07:00 下午 org.apache.coyote.AbstractProtocol start
信息: Starting ProtocolHandler ["ajp-nio-8009"]
四月 08, 2018 7:07:00 下午 org.apache.catalina.startup.Catalina start
信息: Server startup in 346 ms
LifeCycleServlet 的实例对外提供服务
LifeCycleServlet 的实例对外提供服务
ServletConfig
1、ServletConfig接口:servlet容器在初始化期间将信息传递给servlet的servlet配置对象(用于描述一个Servlet 的配置信息)。
2、常用方法
String getServletName()
String getInitParameter(String name)根据name 获取指定的初始化参数的值,如果name 对应的初始化参数不存在,返回null
Enumeration getInitParameterNames()返回当前Servlet 实例的所有的初始化参数的名称,注意返回类型是java.util.Enumeration
ServletContext getServletContext()
测试案例:
ConfigurationServlet
ecut.lifecycle.ConfigurationServlet
encoding
UTF-8
content-type
text/html
ConfigurationServlet
/config
package ecut.lifecycle;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Enumeration;
import javax.servlet.Servlet;
import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
public class ConfigurationServlet implements Servlet {
@Override
public String getServletInfo() {
return this.config.getServletName() ;
}
@Override
public ServletConfig getServletConfig() {
return this.config ;
}
private ServletConfig config ;
@Override
public void init( ServletConfig config ) throws ServletException {
this.config = config ;
System.out.println( "初始化" );
System.out.println( config );
System.out.println( "ServletName : " + config.getServletName() );//web.xm配置文件中的servlet名称
/*
String encoding = config.getInitParameter( "encoding" );
System.out.println( "encoding : " + encoding );
*/
Enumeration initParamNames = config.getInitParameterNames();
while( initParamNames.hasMoreElements() ){
String name = initParamNames.nextElement() ;
String value = config.getInitParameter( name );
System.out.println( name + " : " + value );
}
}
@Override
public void service( ServletRequest req , ServletResponse resp ) throws ServletException, IOException {
System.out.println( "对外提供服务" );
String ct = config.getInitParameter( "content-type" ) ;
String en = config.getInitParameter( "encoding" ) ;
String contentType = ct + ";charset=" + en ; // "text/html;charset=UTF-8"
resp.setContentType( contentType );
PrintWriter w = resp.getWriter();
w.println( "欢迎 !
" );
}
@Override
public void destroy() {
System.out.println( "销毁" );
}
}
配置Servlet 的初始化参数
其中
使用config.getInitParameter( name ) 可以获取指定名称的参数对应的值
使用config.getInitParameterNames() 可以获取所有的参数的名称
运行结果如下:
初始化
org.apache.catalina.core.StandardWrapperFacade@4c12c6a
ServletName : ConfigurationServlet
content-type : text/html
encoding : UTF-8
对外提供服务
转载请于明显处标明出处
http://www.cnblogs.com/AmyZheng/p/8742900.html