2021-05-16

Servlet--MVC

Servlet

Servlet简单来看就是一个java接口,Servlet是服务连接器,是在web服务器层上的程序,是Web和用户之间的中间层,连接了数据库和应用程序。Servlet能收集来自网页表单的用户输入,并创建动态页。下图就是Servlet在Web应用程序中的位置

Servlet 架构Servlet 架构

和JSP相比Servlet采用了较老的CGI的方式,一句一句输出。就很繁琐,不方便与修改和编写,而不是像JSP一样,在JSP中直接嵌入HTML语句。但是JSP所能实现的功能,Servlet均能实现,JSP本质还是Servlet。从网络层次看Servlet在业务层方面,强大的,JSP面对的主要是表现层。

Servlet生命周期

初始化-调用-销毁-由JVM垃圾回收器进行回收

init()方法

init只能被调用一次,就相当于是一次性的初始化,Servlet每当被用户调用时都会船舰一个用户实例,每一个用户请求都会产生一个新的线程。init方法简单创建或加载一些数据,并作用于整个Servlet的生命周期。

service()方法

service方法是主体部分,是执行任务的主要方法,service方法就是来处理来自客户端的请求,并将响应返回给客户端(jsp)。在运行中,每次收到一个请求的时候都会产生一个新的进程,并且调用服务。doGet和doPost是从用户表单请求服务的方法,在实际操作中不需要对service进行操作,只需要按着需求重写doGet和doPost方法即可。

destory()方法

destory方法,也只会被调用一次,调用即是代表这次的操作,到了尽头,主要是用来关闭数据库连接,停止后台线程,将Cookie或点击计数器地数据写入磁盘。在被destory后,servlet对象就会被视为垃圾,然后被JVM回收程序进行回收。

下图即是一个Servlet生命周期的方案:

Servlet 生命周期 

public void init(ServletConfig servletConfig)throws ServletException{
        System.out.println("初始化进行中...");
    }
//    public ServletConfig getServletConfig(){
//        return null;
//    }
    public void service(ServletRequest request, ServletResponse response)throws ServletException, IOException{
        System.out.println("服务进行中...");
    }
    public void doGet(ServletRequest request, ServletResponse response) throws  ServletException,IOException{
        System.out.println("get方法获取请求...");
    }
    public void doPost(ServletRequest request,ServletResponse response) throws ServletException,IOException{
        System.out.println("post获取请求中...");
    }
    public void deotory(){
        System.out.println("Servlet销毁中...");
    }

Servlet表单处理

Get、Post方法

Post主要是用于传输隐私信息,post不会把欣喜体现在URL中,消息是以标准输入输出地形式传到后台的,get方法就是传输一长串地字符串,会体现在URL中,以“?”为分隔符。

表单数据处理方法

getParameter():常用的调用表单数据的方法

getParameterValues():在参数多次出现时,选择此方法,能够返回多个值

getParameterNames():用于获取完整列表

接口

ServletRequest接口

Servlet容器在处理请求的时候都回创建一个ServletRequest对象,并且会把对对象传给Service方法

ServletResponse接口

response就是对request的响应,这里也是在调用service之前就创建的。在相应的时候大多是返回HTML文件

ServletCofig接口

Servlet服务器回传给init方法一个ServletConfig对象

ServletContext

ServletContext对象是每个web应用程序的标配,ServletContext可以是的应用程序中的资料实现共享。被存在ServletContext中的对象叫做属性。可以通过ServletConfig或者getServletContext获取ServletContext对象

GenericServlet

在Servlet操作中主要是通过接口进行操作,这里,填进去的方法都会被考虑,虽然有的方法不会被调用。GenericServlet就不需要这么麻烦了

下面的代码就是用Generic来简化操作的具体代码(copy来的):

public abstract class GenericServlet implements Servlet, ServletConfig, Serializable {
    private static final String LSTRING_FILE = "javax.servlet.LocalStrings";
    private static ResourceBundle lStrings = ResourceBundle.getBundle("javax.servlet.LocalStrings");
    private transient ServletConfig config;
 
    public GenericServlet() {
    }
 
    public void destroy() {
    }
 
    public String getInitParameter(String name) {
        ServletConfig sc = this.getServletConfig();
        if (sc == null) {
            throw new IllegalStateException(lStrings.getString("err.servlet_config_not_initialized"));
        } else {
            return sc.getInitParameter(name);
        }
    }
 
    public Enumeration<String> getInitParameterNames() {
        ServletConfig sc = this.getServletConfig();
        if (sc == null) {
            throw new IllegalStateException(lStrings.getString("err.servlet_config_not_initialized"));
        } else {
            return sc.getInitParameterNames();
        }
    }
 
    public ServletConfig getServletConfig() {
        return this.config;
    }
 
    public ServletContext getServletContext() {
        ServletConfig sc = this.getServletConfig();
        if (sc == null) {
            throw new IllegalStateException(lStrings.getString("err.servlet_config_not_initialized"));
        } else {
            return sc.getServletContext();
        }
    }
 
    public String getServletInfo() {
        return "";
    }
 
    public void init(ServletConfig config) throws ServletException {
        this.config = config;
        this.init();
    }
 
    public void init() throws ServletException {
    }
 
    public void log(String msg) {
        this.getServletContext().log(this.getServletName() + ": " + msg);
    }
 
    public void log(String message, Throwable t) {
        this.getServletContext().log(this.getServletName() + ": " + message, t);
    }
 
    public abstract void service(ServletRequest var1, ServletResponse var2) throws ServletException, IOException;
 
    public String getServletName() {
        ServletConfig sc = this.getServletConfig();
        if (sc == null) {
            throw new IllegalStateException(lStrings.getString("err.servlet_config_not_initialized"));
        } else {
            return sc.getServletName();
        }
    }
}

HttpServlet

HttpServlet是从GenericServlet继承来的。最常见的就是HttpSeervletRequest和HttpServletResponse接口。

HttpSeervletRequest表示的是Servlet的请求,下面是相较于javax.servlet.ServletRequest多的几个方法

  • String getContextPath(); 返回请求上下文的请求URI部分
  • Cookie[] getCookies();  返回一个cookie对象数组
  • String getHeader(String var1);  返回指定HTTP标题的值
  • String getMethod();  返回生成这个请求HTTP的方法名称
  • String getQueryString();  返回请求URL中的查询字符串
  • HttpSession getSession();  返回与这个请求相关的会话对象

HttpServletResponse继承自ServletResponse接口,就是用来装HTTP响应的

MVC

M(dodel、模型层):接收视图层的数据,然后进行响应,并返回对应的结果,进行所用类的编写,这里的类可以重复使用,即类似于头文件库,用于提供一类问题可能用到的工具

V(View、显示层):前端界面,即是jsp、html、css所编写的,这里只进行数据的采集获取用户的请求,就相当于是是给后台提供材料。

C(Controller、控制层):就是面对用户请求选择怎样的工具,在获取到请求后,后告诉模型层的哪块应该干活去了,控制层没有对数据的操作

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值