设为首页 加入收藏

TOP

学习笔记——Servlet(一)
2023-07-25 21:29:39 】 浏览:53
Tags:习笔记 Servlet

2022-10-29

 Servlet

1、 Servlet的作用:

例如:查询数据

(1)浏览器端点击某个查询功能,向服务器端发出请求;服务器端解析请求,创建Servlet对象,并调用特定方法;Servlet对象调用“DAO”方法获取数据;DAO方法查询数据库。

(2)之后将后端的处理数据传递给“前端页面”,进行刷新。数据库返回查询结果;DAO方法返回集合数据;Servlet将数据响应给浏览器;浏览器接收到响应,显示页面。

2、Servlet的含义:

Servlet:Server Applet(就是指服务器端的小程序。主要用于和客户端交互,接收和处理请求)。

3、创建Servlet的“HelloServlet”:

步骤:

(1)新建一个普通类。如在创建的“Java Enterprise”项目的“Web Application”模块中的“src”文件夹下创建一个“HelloServlet”类。

(2)实现接口Servlet。(实现接口使用的是“implements Servlet”)

(3)实现接口的抽象方法(主要实现“service”方法)

package com.haha.servlet;

import javax.servlet.*;
import java.io.IOException;

public class HelloServlet implements Servlet {
    /**
     * 主要功能:处理客户端的请求和响应
     * @param servletRequest 来自客户端的请求
     * @param servletResponse 来自客户端的响应
     * @throws ServletException
     * @throws IOException
     */
    @Override
    public void service(ServletRequest servletRequest, ServletResponse servletResponse) throws ServletException, IOException {
        System.out.println("访问到HelloServlet的service方法...");
    }

    @Override
    public String getServletInfo() {
        return null;
    }

    @Override
    public void destroy() {

    }
    @Override
    public void init(ServletConfig servletConfig) throws ServletException {

    }

    @Override
    public ServletConfig getServletConfig() {
        return null;
    }
}

(4)给刚刚创建的类(HelloServlet)设置访问路径。设置的访问路径在“web.xml”。配置文件的设置:

<?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>
        <!--为servlet起名-->
        <servlet-name>HelloServlet</servlet-name>
        <!--servlet的全类名(就是包名.类名)-->
        <servlet-class>com.haha.servlet.HelloServlet</servlet-class>
    </servlet>
    <servlet-mapping>
        <!--和上面起的名字一样-->
        <servlet-name>HelloServlet</servlet-name>
        <!--访问servlet的路径,注意前面一定要加/,要不然访问不到-->
        <url-pattern>/hello</url-pattern>
    </servlet-mapping>
</web-app>

之后,在该模块下的“src”文件夹下,创建一个“index.html”,设置一个超链接进行访问,如下:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
    <a href="hello">访问HelloServlet</a>
</body>
</html>

运行结果:

在弹出的浏览器中点击“超链接”,页面出现的空白的,在idea中的控制台上出现了运行结果。

 

 

 4、servlet的生命周期

在servlet中的接口中,有“init/service/destory”。默认情况下,在进行请求时,执行init与service方法,如果要执行“destory”方法,那么需要停止“tomcat”服务器。

5、另一种实现servlet的方式

(1)创建一个普通类(MysecondServlet)

(2)继承“HttpServlet”

(3)重写“doget”和“dopost”方法。如下:

package com.haha.servlet;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

public class MySecondServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        System.out.println("访问到
首页 上一页 1 2 下一页 尾页 1/2/2
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
上一篇Java发展到现在,哪些技术可以放.. 下一篇什么是ForkJoin?看这一篇就能掌..

最新文章

热门文章

Hot 文章

Python

C 语言

C++基础

大数据基础

linux编程基础

C/C++面试题目