Servlet
2017-10-25 本文已影响8人
达摩君
servlet 是运行在 Web 服务器中的小型 Java 程序(即:服务器端的小应用程序)。servlet 通常通过 HTTP(超文本传输协议)接收和响应来自 Web 客户端的请求。
编写一个servlet程序
- 写一个java类,实现servlet接口
import javax.servlet.*;
import java.io.IOException;
public class ServletDemo1 implements Servlet {
@Override
public void init(ServletConfig servletConfig) throws ServletException {
}
@Override
public ServletConfig getServletConfig() {
return null;
}
@Override
public void service(ServletRequest servletRequest, ServletResponse servletResponse) throws ServletException, IOException {
System.out.println("hello servlet");
}
@Override
public String getServletInfo() {
return null;
}
@Override
public void destroy() {
}
}
- 修改web.xml文件,给servlet提供一个可访问的URI地址
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
version="3.1">
<!--创建一个servlet实例-->
<servlet>
<servlet-name>servletDemo1</servlet-name>
<servlet-class>com.ljb.ServletDemo1</servlet-class>
</servlet>
<!-- 给servlet提供一个可供客户端访问的URI -->
<servlet-mapping>
<servlet-name>servletDemo1</servlet-name>
<url-pattern>/demo1</url-pattern>
</servlet-mapping>
</web-app>
- 部署应用到Tomcat服务器
- 测试:http://localhost:8080/servlet/demo1
执行过程
执行过程.pngServlet生命周期
实例化-->初始化-->服务->销毁
出生:(实例化-->初始化)第一次访问Servlet就出生(默认情况下)
--如何让servlet在服务器启动时就创建
<servlet>
<servlet-name>servletDemo1</servlet-name>
<servlet-class>com.ljb.ServletDemo1</servlet-class>
<!--数字越小先启动-->
<load-on-startup>2</load-on-startup>
</servlet>
活着:(服务)应用活着,servlet就活着
死亡:(销毁)应用卸载了servlet就销毁
public class ServletDemo1 implements Servlet {
//在第一次访问时调用
public ServletDemo1() {
System.out.println("-----ServletDemo1----");
}
//在第一次访问时调用
@Override
public void init(ServletConfig servletConfig) throws ServletException {
System.out.println("-----------init--------");
}
@Override
public ServletConfig getServletConfig() {
return null;
}
//每次访问时调用
@Override
public void service(ServletRequest servletRequest, ServletResponse servletResponse) throws ServletException, IOException {
// System.out.println("hello servlet");
System.out.println("-----------servlet--------");
}
@Override
public String getServletInfo() {
return null;
}
//应用卸载的时候
@Override
public void destroy() {
System.out.println("-----------destroy--------");
}
}
输出结果
-----ServletDemo1----
-----------init--------
-----------servlet--------
-----------destroy--------
Servlet的三种创建方式
- 实现javax.servlet.Servlet接口(如上)
- 继承javax.servet.GenericServlet类(适配器模式)
public class ServletDemo2 extends GenericServlet {
@Override
public void service(ServletRequest servletRequest, ServletResponse servletResponse) throws ServletException, IOException {
System.out.println("GenericServlet");
}
}
- 继承javax.servlet.http.HttpServlet类(模板方法设计模式)
(开发中常用方式)
public class ServletDemo3 extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
// super.doGet(req, resp);
System.out.println("doget");
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
// super.doPost(req, resp);
System.out.println("dopost");
}
}
Servlet --> GenericServlet --> HttpServlet --> (继承HttpServlet)
曾祖父 --> 爷爷 --> 爸爸 --> 孙子
servlet映射细节
<servlet-mapping>
<servlet-name>servletDemo3</servlet-name>
<url-pattern>/demo3</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>servletDemo3</servlet-name>
<url-pattern>/demo33</url-pattern>
</servlet-mapping>
- 通配符* 代表任意字符串
url-pattern: .do 以.字符串的请求都可以访问 注:不要加/
url-pattern: /* 任意字符串都可以访问
url-pattern: /action/* 以/action开头的请求都可以访问
匹配规则:
优先级:从高到低
绝对匹配--> /开头匹配 --> 扩展名方式匹配
如果url-pattern的值是/,表示执行默认映射。所有资源都是servlet
Servlet的线程安全
单实例:每次访问多线程
解决线程安全问题的最佳办法,不要写全局变量,而写局部变量。
Servlet获取配置信息
ServletConfig的使用
作用1:可以获取servlet配置信息
@WebServlet(name = "ServletConfigDemo1", urlPatterns = {"/confdemo1"}, initParams = {@WebInitParam(name = "encoding",value = "GBK")})
public class ServletConfigDemo1 extends HttpServlet {
private ServletConfig config;
@Override
public void init(ServletConfig config) throws ServletException {
super.init(config);
this.config = config;
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String s = config.getInitParameter("encoding");
System.out.println(s);
}
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// String s = config.getInitParameter("encoding");
String s = this.getInitParameter("encoding");
System.out.println(s);
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// String s = config.getInitParameter("encoding");
// String s = this.getInitParameter("encoding");
String s = this.getServletConfig().getInitParameter("encoding");
System.out.println(s);
}
作用2:可以获得ServletContext对象
ServletContext
ServletContext: 代表的是整个应用。一个应用只有一个ServletContext对象。单实例。
作用
1.域对象:在一定范围内(当前应用),使多个Servlet共享数据。
常用方法:
void setAttribute(String name,object value);//向ServletContext对象的map中添加数据
Object getAttribute(String name);//从ServletContext对象的map中取数据
void rmoveAttribute(String name);//根据name去移除数据
@WebServlet(name = "ServletContextDemo1", urlPatterns = "/context1")
public class ServletContextDemo1 extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
ServletContext servletContext = this.getServletContext();
servletContext.setAttribute("name","ljb");
}
}
-----------------------
@WebServlet(name = "ServletContextDemo2", urlPatterns = "/context2")
public class ServletContextDemo2 extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String name = (String) this.getServletContext().getAttribute("name");
System.out.println(name);
}
}
2.获取全局配置信息
<!--配置全局信息-->
<context-param>
<param-name>encoding</param-name>
<param-value>UTF-8</param-value>
</context-param>
String name = this.getServletContext().getInitParameter("encoding");
System.out.println(name);
- 获取资源路径
String getRealPath(String path);//根据资源名称得到资源的绝对路径.
可以得到当前应用任何位置的任何资源。 - 实现Servlet的转发
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// ServletContext servletContext = this.getServletContext();
// servletContext.setAttribute("name","ljb");
System.out.println("我要转发了");
ServletContext application = this.getServletContext();
application.getRequestDispatcher("/context2").forward(request,response);
System.out.println("转发结束");
}