Servlet–HttpServlet实现doGet和doPost请求的原理

Servlet–HttpServlet实现doGet和doPost请求的原理Servlet–HttpServlet实现doGet和doPost请求的原理更多原创性能测试文章关注十年性能测试专家&7DGroup公众号一、HttpServlet简介1、HttpServlet是GenericServlet的子类,又是在Generi…

大家好,又见面了,我是你们的朋友全栈君。如果您正在找激活码,请点击查看最新教程,关注关注公众号 “全栈程序员社区” 获取激活教程,可能之前旧版本教程已经失效.最新Idea2022.1教程亲测有效,一键激活。

Jetbrains全家桶1年46,售后保障稳定

                                                           Servlet–HttpServlet实现doGet和doPost请求的原理

更多原创性能测试文章关注

十年性能测试专家&7DGroup公众号

 Servlet--HttpServlet实现doGet和doPost请求的原理

一、HttpServlet简介

1、HttpServlet是GenericServlet的子类,又是在GenericServlet的基础上做了增强。

 2、HttpServlet方法

Servlet--HttpServlet实现doGet和doPost请求的原理

 

 

二、HTTP实现doGet或doPost请求项目介绍

1、通过实现doGet请求和doPost请求实例来了解内部的工作原理。

2、doGet请求和doPost请求实例代码介绍:

A:创建一个Servlet类继承HttpServlet类

B:在index.jsp页面创建一个超链接请求

3、doGet请求和doPost请求实例实施介绍:

 

A、创建一个webproject项目。

Servlet--HttpServlet实现doGet和doPost请求的原理

 

 

 

B、创建一个Servlet类的名称为HttpServ继承HttpServlet类同时覆写doGet方法和doPost方法。

1、

Servlet--HttpServlet实现doGet和doPost请求的原理

 

2、

Servlet--HttpServlet实现doGet和doPost请求的原理

 

3、配置web.xml文件

Servlet--HttpServlet实现doGet和doPost请求的原理

4、创建Servlet代码展示

     

package httpserve;

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 HttpServ extends HttpServlet {

	
	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		
		System.out.println("发送get请求。。。。。。。。。。。");
		
	}

	protected void doPost(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		// TODO Auto-generated method stub
		resp.setContentType("text/html;charset=UTF-8");
		System.out.println("发送post方法。。。。。。。。。。");
	}

}

Jetbrains全家桶1年46,售后保障稳定

 

 

 

5、web.xml配置文件代码展示

 

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.0"
    xmlns="http://java.sun.com/xml/ns/javaee"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd">
  <servlet>
    <description>This is the description of my J2EE component</description>
    <display-name>This is the display name of my J2EE component</display-name>
    <servlet-name>HttpServ</servlet-name>
    <servlet-class>httpserve.HttpServ</servlet-class>
  </servlet>

  <servlet-mapping>
    <servlet-name>HttpServ</servlet-name>
    <url-pattern>/http</url-pattern>
  </servlet-mapping>

</web-app>

 

 

 

 

 

C、在index.jsp页面创建一个超链接请求

 

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href="<%=basePath%>">
    
    <title>My JSP 'index.jsp' starting page</title>
	<meta http-equiv="pragma" content="no-cache">
	<meta http-equiv="cache-control" content="no-cache">
	<meta http-equiv="expires" content="0">    
	<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
	<meta http-equiv="description" content="This is my page">
	<!--
	<link rel="stylesheet" type="text/css" href="styles.css">
	-->
  </head>
  
  <body>
    This is my JSP page. <br>
    <a href="http://localhost:8080/test06/http">get请求1</a><br/>
    <!-- 对于一个html页面来说,如果没有以http开始,则默认的前面会加上
    	协议类型://目前这个页面所在的服务器:目前端口/目前项目/你给的这个名称 -->
    <a href="http">get请求2</a><hr/>
    <form method = "post" action="http">
    	<input type="submit" value="提交"/>
    </form>
  </body>
</html>

 

 

 

D、发送doGet请求和doPost请求

a、在浏览器中输入测试地址,http://127.0.0.1:8080/test06/

b、打开项目的首页后分别点击get请求1、get请求2、和 post请求(提交按钮)

c、在控制台可以观察到分别调用了doGet和doPost方法。

Servlet--HttpServlet实现doGet和doPost请求的原理

 

三、HTTP实现doGet或doPost请求原理介绍

    1、浏览器发送请求到HttpSevr类调用HttpServ的service(servletRequest, servletReponse)方法

2、由于没有找到这个方法,去调用父类(HttpServlet) 的同名方法。

3、父类的service方法将ServletRequest req请求转换成HttpServletRequest请求,再去调用service(request, response) 方法。

将ServletRequest req请求转换成HttpServletRequest请求再调用service(request, response) 方法源码如下:

 

 public void service(ServletRequest req, ServletResponse res)
        throws ServletException, IOException {

        HttpServletRequest  request;
        HttpServletResponse response;
        
        try {
            request = (HttpServletRequest) req;
            response = (HttpServletResponse) res;
        } catch (ClassCastException e) {
            throw new ServletException("non-HTTP request or response");
        }
        service(request, response);
    }

 

 

 

4、 调用的service(request, response) 方法功能是判断用户发出是什么请求,如果是get则调用子类(HttpSevr)的doGet方法,如果是post则调用子类(HttpSevr)的doPost方法。

service(request, response) 方法源码如下:

 

  protected void service(HttpServletRequest req, HttpServletResponse resp)
        throws ServletException, IOException {

        String method = req.getMethod();

        if (method.equals(METHOD_GET)) {
            long lastModified = getLastModified(req);
            if (lastModified == -1) {
                // servlet doesn't support if-modified-since, no reason
                // to go through further expensive logic
                doGet(req, resp);
            } else {
                long ifModifiedSince;
                try {
                    ifModifiedSince = req.getDateHeader(HEADER_IFMODSINCE);
                } catch (IllegalArgumentException iae) {
                    // Invalid date header - proceed as if none was set
                    ifModifiedSince = -1;
                }
                if (ifModifiedSince < (lastModified / 1000 * 1000)) {
                    // If the servlet mod time is later, call doGet()
                    // Round down to the nearest second for a proper compare
                    // A ifModifiedSince of -1 will always be less
                    maybeSetLastModified(resp, lastModified);
                    doGet(req, resp);
                } else {
                    resp.setStatus(HttpServletResponse.SC_NOT_MODIFIED);
                }
            }

        } else if (method.equals(METHOD_HEAD)) {
            long lastModified = getLastModified(req);
            maybeSetLastModified(resp, lastModified);
            doHead(req, resp);

        } else if (method.equals(METHOD_POST)) {
            doPost(req, resp);
            
        } else if (method.equals(METHOD_PUT)) {
            doPut(req, resp);        
            
        } else if (method.equals(METHOD_DELETE)) {
            doDelete(req, resp);
            
        } else if (method.equals(METHOD_OPTIONS)) {
            doOptions(req,resp);
            
        } else if (method.equals(METHOD_TRACE)) {
            doTrace(req,resp);
            
        } else {
            //
            // Note that this means NO servlet supports whatever
            // method was requested, anywhere on this server.
            //

            String errMsg = lStrings.getString("http.method_not_implemented");
            Object[] errArgs = new Object[1];
            errArgs[0] = method;
            errMsg = MessageFormat.format(errMsg, errArgs);
            
            resp.sendError(HttpServletResponse.SC_NOT_IMPLEMENTED, errMsg);
        }
    }

 

 

 

5、调用关系图

Servlet--HttpServlet实现doGet和doPost请求的原理

 

 

四、注意事项:

1、如果在HttpServ中覆盖了service(ServletRequest,SerlvetResonse)方法则这个类的所实现的doGet/doPost都不会再执行了。

因为service(ServletRequest,SerlvetResonse)是最高接口Servlet定义规范。在tomcat调用时,一定会在最终的子类中去找这个方法且调用它。

       如果最终的子类没有则会调用父的service(ServletRequest,SerlvetResonse)。

2、如果覆盖了serivce(HttpServletRequest,HtpServletResponse)则会执行httpServlet中的service(ServletRequest,SerlvetResonse),但是由于子类中已经覆盖了serivce(HttpServletRequest,HtpServletResponset)所以,httpServlet中的serivce(HttpServletRequest,HtpServletResponset)就不再执行了,而是直接执行子类中同名同参数方法,且doXxxx也不会执行了,因为子类的serivce(HttpServletRequest,HtpServletResponset)没有调用doXxxx.

3、如果继承了HttpServlet没有实现任何的doXxx方法则会抛出一个异常

 

 

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/210104.html原文链接:https://javaforall.net

(0)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • Kali 2.0 使用 Reaver 的注意事项[通俗易懂]

    Kali 2.0 使用 Reaver 的注意事项[通俗易懂]1、刚一开始使用这条命令airmon-ngstartwlan0就可以开始了,需要注意的是,在Kali2.0里开启的不再是mon0了,而是wlan0mon,所以不要和Kali1.X的版本代码混淆2、Kali1.X的命令无效在Kali2.0中必须自己手动开启网卡的监听模式,所以在执行完上面之后,需要自己手动开启监听模式ifconfigwlan0mondowniwconfig

    2022年5月9日
    154
  • 用python编写猴子吃桃问题_人工智能猴子摘香蕉

    用python编写猴子吃桃问题_人工智能猴子摘香蕉该楼层疑似违规已被系统折叠隐藏此楼查看此楼#include#include#includeintW;/*W-猴子的水平位置*/intx;/*x-当猴子在箱子顶上时取x=1;否则取x=0*/intc;/*c-是香蕉正下方的地板位置*/intY;/*Y-箱子的水平位置*/intz;/*z-当猴子摘到香蕉时取z=1;否则取z=0*/intascll=25;voidGo…

    2022年9月25日
    0
  • 联想笔记本键盘亮了屏幕不亮怎么办_电脑开机显示器和键盘都不亮

    联想笔记本键盘亮了屏幕不亮怎么办_电脑开机显示器和键盘都不亮联想电脑显示器不亮怎么办联想电脑显示器不亮解决方法一:1、开机后,我们先不管显示器是否能正常的亮或显示,我们先再次按主机上的重启键,然后我们按一下键中的“numlock”键,也就是台式键盘右边的数字开关切换键。2、如数字开关键上面的数字锁定灯可以正常的亮或正常的灭,这时就说明电脑主机一般没啥事儿了,基本上可以确定是由显示器本身的问题了。3、如无法显示正常的灯亮和灯灭的话,那么基本可以说明是电脑机…

    2022年9月20日
    0
  • 空洞骑士debug使用教程_debug调试汇编程序

    空洞骑士debug使用教程_debug调试汇编程序

    2022年10月15日
    0
  • python py生成及调用pyc文件「建议收藏」

    python py生成及调用pyc文件「建议收藏」本文仅供学习交流使用,如侵立删!联系方式及demo下载见文末pythonpy文件中调用pyc文件1.生成pyc文件#目录下所有py文件生成pycimportcompileallcompileall.compile_dir(‘D:\python项目\ceshi’)#py生成pyc文件importpy_compilepy_compile.compile(‘proxy.py’)2.py文件调用pyc1.将生成的pyc文件名修改成和py文件一样(如:test_pay

    2022年6月15日
    59
  • navicat激活码mac【2021免费激活】

    (navicat激活码mac)2021最新分享一个能用的的激活码出来,希望能帮到需要激活的朋友。目前这个是能用的,但是用的人多了之后也会失效,会不定时更新的,大家持续关注此网站~IntelliJ2021最新激活注册码,破解教程可免费永久激活,亲测有效,下面是详细链接哦~https://javaforall.net/100143.html…

    2022年3月27日
    459

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号