Java web基础知识

news2025/2/22 16:29:02

Servlet

Servlet是sun公司开发的动态web技术
sun在API中提供了一个接口叫做 Servlet ,一个简单的Servlet 程序只需要完成两个步骤
  • 编写一个实现了Servlet接口的类
  • 把这个Java部署到web服务器中

一般来说把实现了Servlet接口的java程序叫做,Servlet

初步认识Servlet

Servlet接口sun公司有两个默认的实现抽象类:HttpServlet,GenericServlet

看看Serlvet接口是怎么在这两个类中实现的

Servlet接口

package javax.servlet;

import java.io.IOException;

public interface Servlet {
    void init(ServletConfig var1) throws ServletException;

    ServletConfig getServletConfig();

    void service(ServletRequest var1, ServletResponse var2) throws ServletException, IOException;

    String getServletInfo();

    void destroy();
}

这个接口里有五个抽象方法

  • init(ServletConfig var1)
  • getServletConfig()
  • service(ServletRequest var1, ServletResponse var2)
  • getServletInfo()
  • destroy()

这五个方法建立起了Servlet的基本框架:初始化、获取配置、服务功能、获取状态信息、销毁

也体现了servlet的生命周期

  • Servlet 初始化后调用 init () 方法。
  • Servlet 调用 service() 方法来处理客户端的请求。
  • Servlet 销毁前调用 destroy() 方法。
  • 最后,Servlet 是由 JVM 的垃圾回收器进行垃圾回收的。

其中比较值得观察的是service方法

GenericServlet

package javax.servlet;

import java.io.IOException;
import java.io.Serializable;
import java.util.Enumeration;

public abstract class GenericServlet implements Servlet, ServletConfig, Serializable {
    private static final long serialVersionUID = 1L;
    private transient ServletConfig config;

    public GenericServlet() {
    }

    public void destroy() {
    }

    public String getInitParameter(String name) {
        return this.getServletConfig().getInitParameter(name);
    }

    public Enumeration<String> getInitParameterNames() {
        return this.getServletConfig().getInitParameterNames();
    }

    public ServletConfig getServletConfig() {
        return this.config;
    }

    public ServletContext getServletContext() {
        return this.getServletConfig().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 message) {
        this.getServletContext().log(this.getServletName() + ": " + message);
    }

    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() {
        return this.config.getServletName();
    }
}

还是那五个抽象方法

  • init(ServletConfig var1)
  • getServletConfig()
  • service(ServletRequest var1, ServletResponse var2)
  • getServletInfo()
  • destroy()

可以看到对于service方法这个类并没有做任何的改变,只是对初始化和状态信息获取做出了改变

HttpServlet


package javax.servlet.http;

import java.io.IOException;
import java.lang.reflect.Method;
import java.text.MessageFormat;
import java.util.Enumeration;
import java.util.ResourceBundle;
import javax.servlet.GenericServlet;
import javax.servlet.ServletException;
import javax.servlet.ServletOutputStream;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;

public abstract class HttpServlet extends GenericServlet {
    private static final String METHOD_DELETE = "DELETE";
    private static final String METHOD_HEAD = "HEAD";
    private static final String METHOD_GET = "GET";
    private static final String METHOD_OPTIONS = "OPTIONS";
    private static final String METHOD_POST = "POST";
    private static final String METHOD_PUT = "PUT";
    private static final String METHOD_TRACE = "TRACE";
    private static final String HEADER_IFMODSINCE = "If-Modified-Since";
    private static final String HEADER_LASTMOD = "Last-Modified";
    private static final String LSTRING_FILE = "javax.servlet.http.LocalStrings";
    private static ResourceBundle lStrings = ResourceBundle.getBundle("javax.servlet.http.LocalStrings");

    public HttpServlet() {
    }

    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String protocol = req.getProtocol();
        String msg = lStrings.getString("http.method_get_not_supported");
        if (protocol.endsWith("1.1")) {
            resp.sendError(405, msg);
        } else {
            resp.sendError(400, msg);
        }

    }

    protected long getLastModified(HttpServletRequest req) {
        return -1L;
    }

    protected void doHead(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        NoBodyResponse response = new NoBodyResponse(resp);
        this.doGet(req, response);
        response.setContentLength();
    }

    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String protocol = req.getProtocol();
        String msg = lStrings.getString("http.method_post_not_supported");
        if (protocol.endsWith("1.1")) {
            resp.sendError(405, msg);
        } else {
            resp.sendError(400, msg);
        }

    }

    protected void doPut(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String protocol = req.getProtocol();
        String msg = lStrings.getString("http.method_put_not_supported");
        if (protocol.endsWith("1.1")) {
            resp.sendError(405, msg);
        } else {
            resp.sendError(400, msg);
        }

    }

    protected void doDelete(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String protocol = req.getProtocol();
        String msg = lStrings.getString("http.method_delete_not_supported");
        if (protocol.endsWith("1.1")) {
            resp.sendError(405, msg);
        } else {
            resp.sendError(400, msg);
        }

    }

    private Method[] getAllDeclaredMethods(Class<? extends HttpServlet> c) {
        Class<?> clazz = c;

        Method[] allMethods;
        for(allMethods = null; !clazz.equals(HttpServlet.class); clazz = clazz.getSuperclass()) {
            Method[] thisMethods = clazz.getDeclaredMethods();
            if (allMethods != null && allMethods.length > 0) {
                Method[] subClassMethods = allMethods;
                allMethods = new Method[thisMethods.length + allMethods.length];
                System.arraycopy(thisMethods, 0, allMethods, 0, thisMethods.length);
                System.arraycopy(subClassMethods, 0, allMethods, thisMethods.length, subClassMethods.length);
            } else {
                allMethods = thisMethods;
            }
        }

        return allMethods != null ? allMethods : new Method[0];
    }

    protected void doOptions(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        Method[] methods = this.getAllDeclaredMethods(this.getClass());
        boolean ALLOW_GET = false;
        boolean ALLOW_HEAD = false;
        boolean ALLOW_POST = false;
        boolean ALLOW_PUT = false;
        boolean ALLOW_DELETE = false;
        boolean ALLOW_TRACE = true;
        boolean ALLOW_OPTIONS = true;

        for(int i = 0; i < methods.length; ++i) {
            String methodName = methods[i].getName();
            if (methodName.equals("doGet")) {
                ALLOW_GET = true;
                ALLOW_HEAD = true;
            } else if (methodName.equals("doPost")) {
                ALLOW_POST = true;
            } else if (methodName.equals("doPut")) {
                ALLOW_PUT = true;
            } else if (methodName.equals("doDelete")) {
                ALLOW_DELETE = true;
            }
        }

        StringBuilder allow = new StringBuilder();
        if (ALLOW_GET) {
            allow.append("GET");
        }

        if (ALLOW_HEAD) {
            if (allow.length() > 0) {
                allow.append(", ");
            }

            allow.append("HEAD");
        }

        if (ALLOW_POST) {
            if (allow.length() > 0) {
                allow.append(", ");
            }

            allow.append("POST");
        }

        if (ALLOW_PUT) {
            if (allow.length() > 0) {
                allow.append(", ");
            }

            allow.append("PUT");
        }

        if (ALLOW_DELETE) {
            if (allow.length() > 0) {
                allow.append(", ");
            }

            allow.append("DELETE");
        }

        if (ALLOW_TRACE) {
            if (allow.length() > 0) {
                allow.append(", ");
            }

            allow.append("TRACE");
        }

        if (ALLOW_OPTIONS) {
            if (allow.length() > 0) {
                allow.append(", ");
            }

            allow.append("OPTIONS");
        }

        resp.setHeader("Allow", allow.toString());
    }

    protected void doTrace(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String CRLF = "\r\n";
        StringBuilder buffer = (new StringBuilder("TRACE ")).append(req.getRequestURI()).append(" ").append(req.getProtocol());
        Enumeration reqHeaderEnum = req.getHeaderNames();

        while(reqHeaderEnum.hasMoreElements()) {
            String headerName = (String)reqHeaderEnum.nextElement();
            buffer.append(CRLF).append(headerName).append(": ").append(req.getHeader(headerName));
        }

        buffer.append(CRLF);
        int responseLength = buffer.length();
        resp.setContentType("message/http");
        resp.setContentLength(responseLength);
        ServletOutputStream out = resp.getOutputStream();
        out.print(buffer.toString());
    }

    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String method = req.getMethod();
        long lastModified;
        if (method.equals("GET")) {
            lastModified = this.getLastModified(req);
            if (lastModified == -1L) {
                this.doGet(req, resp);
            } else {
                long ifModifiedSince = req.getDateHeader("If-Modified-Since");
                if (ifModifiedSince < lastModified) {
                    this.maybeSetLastModified(resp, lastModified);
                    this.doGet(req, resp);
                } else {
                    resp.setStatus(304);
                }
            }
        } else if (method.equals("HEAD")) {
            lastModified = this.getLastModified(req);
            this.maybeSetLastModified(resp, lastModified);
            this.doHead(req, resp);
        } else if (method.equals("POST")) {
            this.doPost(req, resp);
        } else if (method.equals("PUT")) {
            this.doPut(req, resp);
        } else if (method.equals("DELETE")) {
            this.doDelete(req, resp);
        } else if (method.equals("OPTIONS")) {
            this.doOptions(req, resp);
        } else if (method.equals("TRACE")) {
            this.doTrace(req, resp);
        } else {
            String errMsg = lStrings.getString("http.method_not_implemented");
            Object[] errArgs = new Object[]{method};
            errMsg = MessageFormat.format(errMsg, errArgs);
            resp.sendError(501, errMsg);
        }

    }

    private void maybeSetLastModified(HttpServletResponse resp, long lastModified) {
        if (!resp.containsHeader("Last-Modified")) {
            if (lastModified >= 0L) {
                resp.setDateHeader("Last-Modified", lastModified);
            }

        }
    }

    public void service(ServletRequest req, ServletResponse res) throws ServletException, IOException {
        if (req instanceof HttpServletRequest && res instanceof HttpServletResponse) {
            HttpServletRequest request = (HttpServletRequest)req;
            HttpServletResponse response = (HttpServletResponse)res;
            this.service(request, response);
        } else {
            throw new ServletException("non-HTTP request or response");
        }
    }
}

可以看到这层的类实现了service方法的处理,每次服务器接收到一个 Servlet 请求时,服务器会产生一个新的线程并调用服务。service() 方法检查 HTTP 请求类型(GET、POST、PUT、DELETE 等),并在适当的时候调用 doGet、doPost、doPut,doDelete 等方法。

但是它默认处理并不是我们所需要的,所以我们自己写的java类就是要重写这些方法。

现在,这个简单的Servlet基础使用链就明了了

RMI

RMI (Java Remote Method Invocation) Java 远程方法调用,是一种允许一个 JVM 上的 object 调用另一个 JVM 上 object 方法的机制

⼀个RMI Server分为三部分:

  • ⼀个继承了 java.rmi.Remote 的接⼝,其中定义我们要远程调⽤的函数
  • ⼀个实现了此接⼝的类
  • ⼀个主类,⽤来创建Registry,并将上⾯的类实例化后绑定到⼀个地址

来分析一下这个RMI设计的意图

一个RMI服务器的三个部分:一个接口、两个类,我理解的它们的作用如下:

  • 接口:作为远程方法调用的调用目标
  • 实现接口的类:用来承载接口方法
为什么要有这样一个类?
有类就可以序列化,序列化之后就可以把一个难以传输的目标变成一个便于传输的流
  • 一个主类:作为Registry存在。
打个比方,它就像一个车站的售票处一样,你来坐车你就要先去它哪里找他买票,你有了对应的票才能找到、乘坐对应的车。
在RMI整体流程它也差不多扮演这个角色,客户端去向Registry请求某个方法,Registry返回一个stub给客户端,客户端在通过stub种的信息找到对应的地址端口建立TCP连接

RMI简单流程

一个简单的demo

服务端

package rmilearn;

import java.net.MalformedURLException;
import java.rmi.Naming;
import java.rmi.RemoteException;
import java.rmi.registry.LocateRegistry;
import java.rmi.server.UnicastRemoteObject;

public class Learn01 {
    public static void main(String[] args) throws RemoteException, MalformedURLException {
        HelloRmi rmi = new HelloRmi();
        LocateRegistry.createRegistry(1099);
        Naming.rebind("rmi://127.0.0.1:1099/hello",rmi);
    }
}

class HelloRmi extends  UnicastRemoteObject implements Hello {
    protected HelloRmi() throws RemoteException {
        super();
    }

    @Override
    public String HelloRm() throws RemoteException {
       return "hello rmi";
    }
}
--------------------------------------------------------------------------
package rmilearn;

import java.rmi.Remote;
import java.rmi.RemoteException;

public interface Hello extends Remote {
    String HelloRm() throws RemoteException;
}

客户端

public class client {

    public static void main(String[] args) throws RemoteException, NotBoundException {
      Registry re = LocateRegistry.getRegistry("localhost",1099);
      Hello h = (Hello) re.lookup("hello");
      String s = h.HelloRm();
      System.out.println(s);
    }
}

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/1022086.html

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

成功的海外网红营销:文化和价值观冲突的应对策略

随着全球数字化和社交媒体的崛起&#xff0c;海外网红营销已经成为企业推广产品和服务的一种重要方式。然而&#xff0c;这种全球性的营销活动也伴随着文化和价值观的多样性&#xff0c;容易导致潜在的冲突和误解。为了取得成功并避免不必要的争议&#xff0c;企业需要深入了解…

从B-21轰炸机看美空军作战战略趋势

源自&#xff1a;北京蓝德信息科技有限公司 图注&#xff1a;The B-21 Raider was unveiled to the public at a ceremony Dec. 2, 2022, in Palmdale, Calif. (U.S. Air Force photo) (一)B-21开发进展 (二)B-21性能研判 声明:公众号转载的文章及图片出于非商业性的教育和科研…

2023 RISC-V 中国峰会 演讲幻灯片和视频回放 均已公开

安全之安全(security)博客目录导读 目录 一、幻灯片地址 二、演讲视频 一、幻灯片地址&#xff1a; GitHub - cnrv/RVSC2023: RISC-V Summit China 2023 二、演讲视频&#xff1a; RISCV国际基金会的个人空间-RISCV国际基金会个人主页-哔哩哔哩视频 参考&#xff1a;CNRV …

python+django社区医院诊所医疗管理系统_6t4o8

技术栈 后端&#xff1a;pythondjango 前端&#xff1a;vueCSSpythonScriptjQueryelementui 开发语言&#xff1a;Python 框架&#xff1a;django/flask Python版本&#xff1a;python3.7.7 数据库&#xff1a;mysql 数据库工具&#xff1a;Navicat 开发软件&#xff1a;PyCh…

计算机专业毕业设计项目推荐05-共享汽车管理系统(SpringBoot+Js+Mysql)

共享汽车管理系统&#xff08;SpringBootJsMysql&#xff09; **介绍****系统总体开发情况-功能模块****各部分模块实现** 介绍 本系列(后期可能博主会统一为专栏)博文献给即将毕业的计算机专业同学们,因为博主自身本科和硕士也是科班出生,所以也比较了解计算机专业的毕业设计…

解码知识图谱:从核心概念到技术实战

目录 1. 概述什么是知识图谱知识图谱与自然语言处理的关系 2. 发展历程语义网络本体论大数据时代的知识图谱知识图谱与深度学习的融合 3. 研究内容知识图谱的建模与表示知识抽取知识图谱的融合与对齐知识图谱的推理知识图谱的评估与验证 4. 知识图谱表示与存储RDF&#xff1a;一…

基于java SpringBoot和HTML实验室预约管理系统设计

摘要 实验室信息管理系统是利用计算机网络技术、数据存储技术、快速数据处理技术对实验室进行全方位管理的计算机软件系统。实验室信息管理系统从最初仅完成数据存储和有限的网络功能&#xff0c;发展到现在可以处理海量数据&#xff0c;具备完善的管理职能&#xff0c;并且能够…

JavaScript中的浅拷贝与深拷贝

前言 JavaScript中的浅拷贝和深拷贝是非常重要的概念&#xff0c;它们在处理对象和数组时具有不同的作用。在编程中&#xff0c;经常需要复制数据以便进行各种操作&#xff0c;但必须注意拷贝的方式&#xff0c;以确保得到预期的结果。 浅拷贝是创建一个新对象或数组&#xf…

D*算法图文详解

前面学习了Dijkstra以及A* 算法的基本原理&#xff0c;对于这两种算法而言&#xff0c;我们都能在有解的情况下找到一条沿着起点到达终点的路径。然而&#xff0c;这两个算法本身都是基于静态地图的&#xff0c;也就是说&#xff1a;当机器人找到路径后开始沿着起点向终点运动的…

不断探索创新 促进中国信息技术发展——南京宏控科技有限公司董事长应富忠

应富忠&#xff0c;男&#xff0c;现任南京宏控科技有限公司董事长、电子系统工程高级工程师&#xff08;技术五级&#xff09;、自动化系统注册工程师&#xff0c;先后被评为“研究所级青年突击手”、“研究所级先进工作者”、“研究所级优秀共产党员”、“南京市级考级优秀”…

微服务保护-授权规则/规则持久化

授权规则 基本规则 授权规则可以对调用方的来源做控制&#xff0c;有白名单和黑名单两种方式。 白名单&#xff1a;来源&#xff08;origin&#xff09;在白名单内的调用者允许访问 黑名单&#xff1a;来源&#xff08;origin&#xff09;在黑名单内的调用者不允许访问 点…

未解之迷——晶振问题导致SWD烧录时芯片no target connected,切换内部晶振后解决了

我所讲的情况是网上总结之外的另一种情况。不是Reset 后卡时间烧录&#xff0c;也不是烧录器问题&#xff0c;引脚问题等。而是STM32CubeMX软件生成问题。 芯片&#xff1a;STM32F103C8T6 某天我做了一块板子&#xff0c;按正常流程烧录&#xff0c;第一次可以烧录&#xff0c…

让Pegasus天马座开发板吃上STM8S标准库

WeCanStudio官方仓库的示例工程,只提供基于STM8S003寄存器方式来开发Pegasus天马座开发板。在此,我将基于官方的工程示例&#xff0c;将STM8S标准库移植到工程中。 先上图&#xff0c;看运行结果: main.c文件 #include "config.h" #include "delay.h"#de…

灯具欧盟CE认证,EMC/LVD/MD等欧盟指令介绍

CE认证产品范围 ​1.灯具类产品; ​2.家用电器设备、电动工具; ​3.个人电脑及其周边设备; ​4.音视频产品; ​5.无线产品; ​6.通讯类类产品; ​7.玩具类产品; ​8.安防产品; ​9.工业机械。 CE认证所需资料 ​1.一般2-3个测试样品; ​2.电路原理图; ​3.产品说明…

基于哈希表对unordered_map和unordered_set的封装

本章完整代码gitee仓库&#xff1a;对unordered_map和unordered_set的封装、unordered_map和unordered_set源码 文章目录 &#x1f36d;1. 哈希表的改造&#x1f36c;1.1 模板参数的改造&#x1f36c;1.2 增加迭代器&#x1f36c;1.3 返回值的修改 &#x1f37c;2. 对unordered…

[计算机入门] 电源选项设置

3.10 电源选项设置 有时候我们的电脑一段时间没有用&#xff0c;会自己关掉屏幕或者直接睡眠&#xff0c;这是电源选项没有设置好导致的。 1、打开控制面板&#xff0c;打开其中的电源选项 2、点击左侧上方的选择关闭显示器的时间 3、进入到编辑计划设置界面&#xff0c;在…

听GPT 讲Istio源代码--pilot(6)

在 Istio 中&#xff0c;Pilot 是 Istio 控制平面的一个重要组件&#xff0c;它具有以下作用&#xff1a; 流量管理: Pilot 负责管理和配置服务之间的网络流量。它通过与底层的服务发现机制&#xff08;如 Kubernetes 或 Consul&#xff09;集成&#xff0c;监测服务注册和注销…

C数据结构二.练习题

一.求级数和 2.求最大子序列问题:设给定一个整数序列 ai.az..,a,(可能有负数).设计一个穷举算法,求a 的最大值。例如,对于序列 A {1,-1,1,-1,-1,1,1,1,1.1,-1,-1.1,-1,1,-1},子序列 A[5..9](1,1,1,1,1)具有最大值5 3.设有两个正整数 m 和n,编写一个算法 gcd(m,n),求它们的最大公…

WhatsApp无法收到验证码怎么办?别急,我来教你

最近收到好多小伙伴的问题咨询&#xff0c;而且大多是同一个问题&#xff1a;“WhatsApp无法验证手机号&#xff0c;也就是手机接收不到6位数字的短信验证码&#xff0c;这可如何是好&#xff1f;” 短信验证码收不到&#xff0c;连点几次重复短信后等待时间越来越久点击致电给…

B-小美的子序列(贪心)-- 牛客周赛 Round 12

示例1 输入 3 3 abc def ghi 输出 NO 示例2 输入 8 2 nm ex it td ul qu ac nt 输出 YES 说明 第1行选择第2个字母。 第2行选择第1个字母。 第3行选择第1个字母。 第4行选择第1个字母。 第5行选择第2个字母。 第6行选择第2个字母。 第7行选择第1个字母。 第8行选择第…