servlet

2018-07-16  本文已影响0人  在努力中
1. 什么是Servlet

先有的Servlet ,后有的Jsp,Servlet是在服务器上运行的小程序。一个Servlet就是一个Java类,并且可以通过“请求-响应”编程模型来访问这个驻留在服务器内存里的Servlet程序

2. Tomcat容器等级
image.png
Engine:引擎
HOSt:主机
Context:一个Context对应一个Web工程
3. 手工编写第一个Servlet

3.1 继承 HttpServlet (Java类)
3.2 重写 doGet()或doPost() 方法

package lsy;

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 HelloServlet extends HttpServlet
{

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        System.out.println("处理get() 请求");
        
        //设置字符,中文正确显示,显示html标签
        resp.setContentType("text/html;charset=utf-8");
        // 创建 PrintWriter 实例对象,可以输出语句到页面
        PrintWriter out = resp.getWriter();
        
        out.print("<strong>使用get方法提交</strong>");
        
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        System.out.println("处理post() 请求");
        //设置字符,中文正确显示,显示html标签
        resp.setContentType("text/html;charset=utf-8");
        // 创建 PrintWriter 实例对象,可以输出语句到页面
        PrintWriter out = resp.getWriter();
        out.print("<strong>使用post方法提交</strong>");
    }

}

3.3 在web.xml文件中,注册该Servlet

  <servlet>
    <servlet-name>Hello</servlet-name>
    <servlet-class>lsy.HelloServlet</servlet-class>
  </servlet>
  
  <servlet-mapping>
    <servlet-name>Hello</servlet-name>
    <url-pattern>/lsy.HelloServlet</url-pattern>
  </servlet-mapping>
4. 使用 MyEclipse 编写Servlet

4.1 src->new -> Servlet
myeclipse可以自动注册Servlet
4.2 重写doGet()/doPost() 方法
4.3 部署运行

5. Server 生命周期
  1. 初始化阶段:调用init() 方法
  2. 响应客户请求阶段:由service 方法根据提交的方式,选择执行doGet/doPost
  3. 终止阶段:调用destory方法


    image.png

代码演示

TestServlet1
package servlet;

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 TestServlet1 extends HttpServlet {

    /**
         * Constructor of the object.
         */
    public TestServlet1() {
        System.out.println("TestServlet1 的 构造方法被执行...");
    }

    /**
         * Destruction of the servlet. <br>
         */
    public void destroy() {
        System.out.println("TestServlet1 的 销毁方法被执行...");
    }

    /**
         * The doGet method of the servlet. <br>
         *
         * This method is called when a form has its tag value method equals to get.
         * 
         * @param request the request send by the client to the server
         * @param response the response send by the server to the client
         * @throws ServletException if an error occurred
         * @throws IOException if an error occurred
         */
    public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        
        System.out.println("TestServlet1 的 doGet() 方法被执行...");
        response.setContentType("text/html;charset=utf-8");
        PrintWriter out = response.getWriter();
        out.println("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\">");
        out.println("<HTML>");
        out.println("  <HEAD><TITLE>A Servlet</TITLE></HEAD>");
        out.println("  <BODY>");
        out.println("<h1>大家好,我是TestServlet1!</h1>");
        out.println("  </BODY>");
        out.println("</HTML>");
        out.flush();
        out.close();
    }

    /**
         * The doPost method of the servlet. <br>
         *
         * This method is called when a form has its tag value method equals to post.
         * 
         * @param request the request send by the client to the server
         * @param response the response send by the server to the client
         * @throws ServletException if an error occurred
         * @throws IOException if an error occurred
         */
    public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        System.out.println("TestServlet1 的 doGet() 方法被执行...");
        doGet(request,response); //让doPost()执行与doGet()相同的操作。
    }

    /**
         * Initialization of the servlet. <br>
         *
         * @throws ServletException if an error occurs
         */
    public void init() throws ServletException {
        // Put your code here
    }

}
jsp
 <body>
    <h1>Servlet生命周期</h1>
    <hr>
    <a href="servlet/TestServlet1">以Get方式请求TestServlet1</a>
  </body>

执行结果

TestServlet1 的 构造方法被执行...
TestServlet1 的 init() 方法被执行
TestServlet1 的 doGet() 方法被执行...
结束服务器时候,
TestServlet1 的 销毁方法被执行...
  1. Servlet 启动时候自动装载某些Servlet
    在 web.xml中配置
<servlet>
...
<loadon-startup>1</loadon-sartup>
// 数字越小,装载权限越高
</servlet>
  1. 在Servlet容器启动后,客户首次向Servlet发送请求。

  2. Servlet类文件被更新后,重新装载Servlet。(Servlet对象长期保存在内存当中)
    Servlet被装载后,Servlet容器创建一个Servlet实例并且调用Servlet的init()方法进行初始化。在Servlet的整个生命周期内,init()方法只被调用一次
    编写代码判断Servlet 的方法执行顺序如下:
    Servlet 的各种方法被执行的顺序

Servlet 的构造方法被执行....
Servlet 的初始化方法被执行....
Servlet 的doGet()或者doPost()方法被执行
Servlet 的销毁方法被执行....

注意:
为了是Servlet 返回内容能正常显示中文,我们需要设置response.setContentType("text/html;charset=utf-8");
放在doGet()或者是doPost()方法中的

6. Servlet 获取 九大内置对象
7. Servlet 与 表单
8. Server 路径跳转

项目实践

上一篇 下一篇

猜你喜欢

热点阅读