Servlet+mvc预习


前言

Servlet 本身不能独立运行,需要在一个web应用中运行的
而一个web应用是部署在tomcat中的
开发servlet的步骤为:

  • 创建web应用项目
  • 编写servlet代码
  • 部署到tomcat中

一、Servlet 获取参数

创建 login.html :
<!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取出对应的账号和密码

然后用System.out.println() 打印在控制台

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);
    }
}

在页面提交数据

首先得重启tomcat,然后访问页面,输入账号密码,提交,就可以看到提交的密码和账号了


二、Servlet 调用流程

流程图

在这里插入图片描述

三、Servlet service()

Servlet 需要提供对应的doGet() 与 doPost()方法

doGet()

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

  • form默认的提交方式
  • 如果通过一个超链访问某个地址
  • 如果在地址栏直接输入某个地址
  • ajax指定使用get方式的时候
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 doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
 
    }
 
}
doPost()

当浏览器使用post方式提交数据的时候,servlet需要提供doPost()方法
post方式有如下几种:

  • 在form上显示设置 method="post"的时候
  • ajax指定post方式的时候
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 {
 
    }
 
}
service()

由service()方法进行判断,到底该调用doGet()还是doPost()

可以发现,service(), doGet(), doPost() 三种方式的参数列表都是一样的。

四、Servlet 生命周期

生命周期

一个Servlet的生命周期由 实例化,初始化,提供服务,销毁,被回收 几个步骤组成
在这里插入图片描述

实例化

当用户通过浏览器输入一个路径,这个路径对应的servlet被调用的时候,该Servlet就会被实例化

无论访问了多少次LoginServlet
LoginServlet构造方法 只会执行一次,所以Servlet是单实例的。

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 {
     
    public LoginServlet(){
        System.out.println("LoginServlet 构造方法 被调用");
    }
 
    protected void service(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
    }
}
初始化

LoginServlet 继承了HttpServlet,同时也继承了init(ServletConfig) 方法

init 方法是一个实例方法,所以会在构造方法执行后执行。

import java.io.IOException;
 
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)");
    }
 
    protected void service(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
    } 
}
提供服务

判断用户输入的账号和密码是否正确

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)");
    }
 
    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);
    }
}
销毁
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 {
 
    public void destroy() {
        System.out.println("destroy()");
    }
 
    protected void service(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
    }
}

五、MVC

MVC是一种分层的设计模式。
其中

  • M 代表 模型(Model)
  • V 代表 视图(View)
  • C 代表 控制器(controller)

使用MVC的思想,可以结合Servlet和JSP进行查询操作。

随着数据中记录的增多,网页上显示的数据会越来越多。
当多到一定程度的时候,就会影响用户的体验。
解决办法是通过分页技术,一次只显示数据库中的部分数据,如果要看其他数据,可以通过"下一页" “最后一页” 等翻页操作实现

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值