J2EE梳理(三)——servlet基础知识


通过servlet实现登录功能
来自于how2java

步骤一:创建login.html

  • ctrl+n 新建 html
  • 添加form元素:
    • action=“login” 标题会提交到login路径,login路径在后续步骤会映射到LoginServlet
    • method=“post” post方式表示提交的密码信息在浏览器地址栏看不到
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>登录页面</title>
</head>
<body>
  
<form action="login" method="post">
账号: <input type="text" name="name"> <br>
密码: <input type="password" name="password"> <br>
<input type="submit" value="登录">
</form>
  
</body>
</html>

步骤二:创建LoginServlet

  • 因为浏览器中的form的method是post,所以LoginServlet需要提供一个doPost方法
  • 在doPost方法中,通过request.getParameter 根据name取出对应的账号和密码
import java.io.IOException;
 
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
  
public class LoginServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        String name = request.getParameter("name");
        String password = request.getParameter("password");
  
        System.out.println("name:" + name);
        System.out.println("password:" + password);
    }
}

步骤四:映射loginservlet到路径login

  • 即为了当访问login.html 点击form action时候,对应的login 映射到servlet,
  • 在web.xml中新增映射
 <servlet>
        <servlet-name>LoginServlet</servlet-name>
        <servlet-class>LoginServlet</servlet-class>
    </servlet>
 
    <servlet-mapping>
        <servlet-name>LoginServlet</servlet-name>
        <url-pattern>/login</url-pattern>
    </servlet-mapping>   

步骤五:提交数据

  • 重启tomcat
  • 访问http://localhost/login
  • 在这里插入图片描述

步骤六:返回html响应

根据浏览器提交的账号密码返回登录成功或者失败
这一步本来应该通过访问数据库来实现,这里简化一下,直接在内存中进行校验
如果账号是 admin,密码是123, 就返回登录成功,否则返回登录失败

  • 判断账号密码是否为 admin 123,如果是就打印
    success 否则就打印 fail
  • 根据账号密码,创建对应的html字符串。
  • 然后通过response.getWriter().println() 发送到浏览器。
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 LoginServlet extends HttpServlet {
  
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
  
        String name = request.getParameter("name");
        String password = request.getParameter("password");
  
        String html = null;
  
        if ("admin".equals(name) && "123".equals(password))
            html = "<div style='color:green'>success</div>";
        else
            html = "<div style='color:red'>fail</div>";
  
        PrintWriter pw = response.getWriter();
        pw.println(html);
  
    }
  
}

在这里插入图片描述
注:在response中写html只是为了自己练习

总结:servlet流程梳理

  • 流程图:在这里插入图片描述
  • login.html: 首先访问http://127.0.0.1/login.html:打开一个静态html,这个页面可以通过form,以post的方式提交数据
  • /login路径: 在上一步的login.html中,用form,把账号和密码,提交到/login这个路径,并且附带method=“post”
  • 找到对应的Servlet: tomcat接到一个请求http://localhost/login,路径是/login,就去web.xml中进行匹配,根据标签<servlet-mapping> 会匹配到LoginServlet,接下来根据这个servlet进行
  • 实例化servlet对象: tomcat定位到LoginServlet,发现没有实例存在,于是就调用LoginServlet的构造方法实例化一个对象。
  • 调用doGet或者doPost: Tomcat拿到LoginServlet后,就根据页面login.html提交的信息method="post"去调用对应的doPost方法;<form action="login" method="post">表示这个form会提交到action login
  • request获取参数: doPost方法中
    protected void doPost(HttpServletRequest request, HttpServletResponse response){ ... } 其中通过参数request,取出页面提交的信息:
    String name = request.getParameter("name"); String password = request.getParameter("password");
  • **response响应:**把html字符串通过如下方式,设置在了response对象上。
    PrintWriter pw = response.getWriter(); pw.println(html);
  • tomcat把html传递给浏览器::在Servlet完成工作之后,tomcat拿到被Servlet修改过的response,根据这个response生成html 字符串,然后再通过HTTP协议,这个html字符串,回发给浏览器,浏览器再根据HTTP协议获取这个html字符串,并渲染在界面上。
    这样在效果上,浏览器就可以看到Servlet中生成的字符串了

doService

  • doGet(): 当浏览器使用get方式提交数据的时候,servlet需要提供doGet()方法

    • form默认的提交方式
    • 如果通过一个超链访问某个地址
    • 如果在地址栏直接输入某个地址
    • ajax指定使用get方式的时候
  • doPost(): 当浏览器使用post方式提交数据的时候,servlet需要提供doPost()方法

    • 在form上显示设置 method="post"的时候
    • ajax指定post方式的时候
  • service(): LoginServlet继承了HttpServlet,同时也继承了一个方法:service(HttpServletRequest , HttpServletResponse )

    • 实际上,在执行doGet()或者doPost()之前,都会先执行service(),由service()方法进行判断,到底该调用doGet()还是doPost()
    • service(), doGet(), doPost() 三种方式的参数列表都是一样的。
    • 所以,有时候也会直接重写service()方法,在其中提供相应的服务,就不用区分到底是get还是post了。
    • 比如把前面的登录的LoginServlet,改为提供service方法,也可以达到相同的效果:
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 LoginServlet extends HttpServlet {
 
    protected void service(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
 
        String name = request.getParameter("name");
        String password = request.getParameter("password");
 
        String html = null;
 
        if ("admin".equals(name) && "123".equals(password))
            html = "<div style='color:green'>success</div>";
        else
            html = "<div style='color:red'>fail</div>";
 
        PrintWriter pw = response.getWriter();
        pw.println(html);
 
    }
 
}

获取中文参数

  • login.html 中加上<meta http-equiv="Content-Type" content="text/html;charset="UTF-8">这句话的目的是告诉浏览器,等下发消息给服务器的时候,使用UTF-8编码
  • 在servlet进行编码解码:
    • 当servlet不编码解码直接拿string:
    protected void service(HttpServletRequest request, HttpServletResponse response)
              throws ServletException, IOException {
          //request.setCharacterEncoding("UTF-8");
          String name = request.getParameter("name");
    
           /*byte[] bytes = name.getBytes("ISO-8859-1");
           name = new String(bytes, "UTF-8");*/
    
          String password = request.getParameter("password");
    
          System.out.println("name:" + name);
    
      }```
    

在这里插入图片描述

  • 当添加byte[] bytes = name.getBytes("ISO-8859-1");
    name = new String(bytes, "UTF-8");在这里插入图片描述
    先根据ISO-8859-1解码,然后用UTF-8编码
    这样就可以得到正确的中文参数了

  • 这样需要对每一个提交的数据都进行编码和解码处理,如果觉得麻烦,也可以使用一句话代替:request.setCharacterEncoding("UTF-8");不过这句话要在request.getParameter("name"); 之前,因为如果不setCharactoerEncoding(),servlet会默认使用iso8859-1进行编码,这也是我们上面byte[] bytes = name.getBytes("ISO-8859-1"); name = new String(bytes, "UTF-8");的原理。

  • 返回中文响应:同理,在servlet中加上response.setContentType("text/html; charset=UTF-8");
    所以这段代码:

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 LoginServlet extends HttpServlet {
 
    protected void service(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
 		request.setCharacterEncoding("UTF-8");
        String name = request.getParameter("name");
        String password = request.getParameter("password");
 		//System.out.println("name:" + name);
        String html = null;
 
        if ("admin".equals(name) && "123".equals(password))
            html = "<div style='color:green'>登录成功</div>";
        else
            html = "<div style='color:red'>登录失败</div>";
 
        response.setContentType("text/html; charset=UTF-8");
        PrintWriter pw = response.getWriter();
        pw.println(html);
 
    }
 
}

生命周期

  • 一个Servlet的生命周期由 实例化,初始化,提供服务,销毁,被回收 几个步骤组成
  • 在这里插入图片描述
  • 实例化:当用户通过浏览器输入一个路径,这个路径对应的servlet被调用的时候,该Servlet就会被实例化
    • 无论访问了多少次LoginServlet,LoginServlet构造方法 只会执行一次,所以Servlet是单实例的
    • public LoginServlet(){ System.out.println("LoginServlet 构造方法 被调用"); }
  • 初始化:LoginServlet 继承了HttpServlet,同时也继承了init(ServletConfig) 方法
    • init 方法是一个实例方法,所以会在构造方法执行后执行。
    • 无论访问了多少次LoginSerlvet,init初始化 只会执行一次
    • public void init(ServletConfig config) { System.out.println("init(ServletConfig)"); }
  • do service :在service()中就会编写我们的业务代码,在本例中就是判断用户输入的账号和密码是否正确
  • 销毁 :接着是销毁destroy(),在如下几种情况下,会调用destroy()
    • 该Servlet所在的web应用重新启动,在server.xml中配置该web应用的时候用到了
      • <Context path="/" docBase="e:\\project\\j2ee\\web" debug="0" reloadable="false" />
      • 如果把 reloadable="false" 改为reloadable="true" 就表示有任何类发生的更新,web应用会自动重启,当web应用自动重启的时候,destroy()方法就会被调用
    • 关闭tomcat的时候 destroy()方法会被调用,但是这个一般都发生的很快,不易被发现。
    • public void destroy() { System.out.println("destroy()"); }
  • 回收: 当servlet被销毁,就满足垃圾回收条件;当下次GC来的时候,就可能被回收
  • 下面我们通过一个实例观察生命周期
import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
 
public class LoginServlet extends HttpServlet {
	public LoginServlet(){
        System.out.println("LoginServlet 构造方法 被调用");
    }
	
	public void init(ServletConfig config) {
        System.out.println("init(ServletConfig)");
    }
	
	public void destroy() {
        System.out.println("destroy()!");
    }
	
    protected void service(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
 		request.setCharacterEncoding("UTF-8");
        String name = request.getParameter("name");
        String password = request.getParameter("password");
        System.out.println("name:" + name);
        String html = null;
 
        if ("admin".equals(name) && "123".equals(password))
            html = "<div style='color:green'>登录成功</div>";
        else
            html = "<div style='color:red'>登录失败</div>";
 
        response.setContentType("text/html; charset=UTF-8");
        PrintWriter pw = response.getWriter();
        pw.println(html);
 
    }
 
}

在这里插入图片描述

发布了117 篇原创文章 · 获赞 59 · 访问量 9万+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章