Servlet技术:
Servlet 是 JavaEE 规范之一。规范就是接口
Servlet 就 JavaWeb 三大组件之一。三大组件分别是:Servlet 程序、Filter 过滤器、Listener 监听
Servlet 是运行在服务器上的一个 java 小程序,它可以接收客户端发送过来的请求,并响应数据给客户端
手动实现Servlet程序(一):步骤
编写一个类去实现 Servlet
新建类实现Servlet接口,并且重写方法
实现 service 方法,处理请求,并响应数据
package Servlet;import javax.servlet.*;import javax.servlet.http.HttpServletRequest;import java.io.IOException;public class Hservlet1 implements Servlet { public Hservlet1() {
System.out.println("1.构造器初始化");
} @Override public void init(ServletConfig servletConfig) throws ServletException {
System.out.println("2.init初始化");
} @Override public ServletConfig getServletConfig() { return null;
} @Override//实现service方法 public void service(ServletRequest servletRequest, ServletResponse servletResponse) throws ServletException, IOException {
System.out.println("3 servie === hello Servlet"); //类型转换() HttpServletRequest httpServletRequest = (HttpServletRequest) servletRequest; //获取请求的方式 String method = httpServletRequest.getMethod(); if ("GET".equals(method)){
doGet();
}else if ("POST".equals(method)){
doPost();
}
} /** * get请求操作 */ public void doGet(){
System.out.println("get请求");
} /** * post请求操作 */ public void doPost(){
System.out.println("post请求");
} @Override public String getServletInfo() { return null;
} @Override public void destroy() {
}
}
3. 到 web.xml 中去配置servlet程序的访问地址
<?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_4_0.xsd"
version="4.0">
<!-- servlet标签给Tomcat配置Servlet程序--> <servlet><!-- servlet-name给Servlet程序起一个别名--> <servlet-name>helloServlet</servlet-name><!-- servlet-class是Servlet程序的全类名--> <servlet-class>Servlet.helloServlet </servlet-class>
</servlet><!-- servlet-mapping给servlet程序配置访问地址 --> <servlet-mapping><!-- servlet-name告诉服务器,当前配置的地址给那个servlet程序使用--> <servlet-name>helloServlet</servlet-name><!-- url-pattern配置访问地址 / 斜杠在服务器解析的时候,表示地址为http://ip:port/工程路径 /hello 表示地址为:http://ip:port/工程路径/hello--> <url-pattern>/hello</url-pattern>
// /hello需要和action对应:如:<form action="http://localhost:8080/Servlet/hello" method="post">
</servlet-mapping>
</web-app>
url 地址到 Servlet 程序的访问解析
Servlet 的生命周期
1、执行 Servlet 构造器方法 2、执行 init 初始化方法第一、二步,是在第一次访问,的时候创建 Servlet 程序会调用。3、执行 service 方法(每次访问都会调用。) 4、执行 destroy 销毁方法(在web工程停止的时候调用)
GET 和 POST 请求的分发处理
@Overridepublic void service(ServletRequest servletRequest, ServletResponse servletResponse) throws ServletException, IOException {
System.out.println("3 servie === hello Servlet"); //类型转换() HttpServletRequest httpServletRequest = (HttpServletRequest) servletRequest; //获取请求的方式 String method = httpServletRequest.getMethod(); if ("GET".equals(method)){
doGet();
}else if ("POST".equals(method)){
doPost();
}
}/** * get请求操作 */public void doGet(){
System.out.println("get请求");
}/** * post请求操作 */public void doPost(){
System.out.println("post请求");
}
实现Servlet程序(二):通过继承HttpServlet实现Servlet程序:步骤
1.继承HttpServlet类
2.重写doGet() 和 doPost()
3.到web.xml中配置Servlet程序的访问地址
<servlet>
<servlet-name>httpServlet</servlet-name>
<servlet-class>Servlet.httpServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>httpServlet</servlet-name>
<url-pattern>/http</url-pattern>
</servlet-mapping>