servlet中service() doGet() doPost() 方法

2024-04-23 09:38

本文主要是介绍servlet中service() doGet() doPost() 方法,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

HttpServlet里的三个方法:service(HttpServletRequest req, HttpServletResponse resp) ,doGet(HttpServletRequest req, HttpServletResponse resp), doPost(HttpServletRequest req, HttpServletResponse res)的区别和联系:
  1. 在servlet中默认情况下,无论你是get还是post 提交过来 都会经过service()方法来处理,然后转向到doGet 
  2. 或是doPost方法,我们可以查看HttpServlet 类的service方法:  我在tomcat的lib目录下,解压servlet-api.jar,然后用反编译软件把lib\javax\servlet\http下的HttpServlet.class反编译,看里头的service()方法的原代码:
  3. 注意,sun只是定义了servlet接口,而实现servlet接口的就是类似于tomcat的服务器,所以我是在tomcat的安装目录下找到实现的类。
protected void service(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException
{
String method = req.getMethod();
if(method.equals("GET"))
{
long lastModified = getLastModified(req);
if(lastModified == -1L)
{
doGet(req, resp);
} else
{
long ifModifiedSince = req.getDateHeader("If-Modified-Since");
if(ifModifiedSince < (lastModified / 1000L) * 1000L)
{
maybeSetLastModified(resp, lastModified);
doGet(req, resp);
} else
{
resp.setStatus(304);
}
}
} else
if(method.equals("HEAD"))
{
long lastModified = getLastModified(req);
maybeSetLastModified(resp, lastModified);
doHead(req, resp);
} else
if(method.equals("POST"))
doPost(req, resp);
else
if(method.equals("PUT"))
doPut(req, resp);
else
if(method.equals("DELETE"))
doDelete(req, resp);
else
if(method.equals("OPTIONS"))
doOptions(req, resp);
else
if(method.equals("TRACE"))
{
doTrace(req, resp);
} else
{
String errMsg = lStrings.getString("http.method_not_implemented");
Object errArgs[] = new Object[1];
errArgs[0] = method;
errMsg = MessageFormat.format(errMsg, errArgs);
resp.sendError(501, errMsg);
}
}
  1. 从上面可以看出 这里的service是用来转向的但是如果你在自己的servlet类中覆盖了service方法,比如说你的service是这样的: 
  2.  
  3. Java代码 
  4.  
  5.    1.publicvoid service(ServletRequest req, ServletResponse res)    
  6.    2.                   throws ServletException, IOException {    
  7.    3.          res.getOutputStream().print(    
  8.    4.         "image is <img src='images/downcoin.gif'></img><br>");    
  9.    5.      }   
    1. 那么这时service就不是用来转向的,而是用来处理业务的,现在不论你的客户端是用pos还是get来请求此servlet 
    2.  
    3. 都会执行service方法也只能执行servlet方法,不会去执行doPost或是doGet方法。 
    4.  
    5. 比如说:你的客户端代码是: 
    6. Java代码 
    7.  
    8.    1. <%@page contentType="text/html; charset=utf-8"%>    
    9.    2. <html>    
    10.    3. <head><title>选择</title></head>    
    11.    4. <body>    
    12.    5. 请选择你喜欢的水果:<br>    
    13.    6. <form action ="Test" method = "post">    
    14.    7. <input type="checkbox" name="fruit" value ="apple" >苹果<br>    
    15.    8. <input type="checkbox" name="fruit" value ="orange">桔子<br>    
    16.    9. <input type="checkbox" name="fruit" value ="mango">芒果<br>    
    17.   10. <input type="submit" value="提交">    
    18.   11. </form>    
    19.   12. </body>    
    20.   13. </html>    
    21.   14.    
    22.   15. 服务端servlet是:Test类    
    23.   16.    
    24.   17.import java.io.IOException;    
    25.   18.    
    26.   19.import javax.servlet.ServletException;    
    27.   20.import javax.servlet.ServletOutputStream;    
    28.   21.import javax.servlet.ServletRequest;    
    29.   22.import javax.servlet.ServletResponse;    
    30.   23.import javax.servlet.http.HttpServlet;    
    31.   24.import javax.servlet.http.HttpServletRequest;    
    32.   25.import javax.servlet.http.HttpServletResponse;    
    33.   26.    
    34.   27./**
    35.   28. * 演示service方法
    36.   29. */   
    37.   30.publicclass Testextends HttpServlet {    
    38.   31.    
    39.   32.publicvoid service(ServletRequest req, ServletResponse res)    
    40.   33.   throws ServletException, IOException {    
    41.   34.             res.getOutputStream().print("This is the service");    
    42.   35.    
    43.   36. }    
    44.   37.    
    45.   38.protectedvoid doGet(HttpServletRequest request,    
    46.   39.     HttpServletResponse response)throws ServletException, IOException {    
    47.   40.    doPost(request,response);    
    48.   41.    
    49.   42. }    
    50.   43.protectedvoid doPost(HttpServletRequest request,    
    51.   44.     HttpServletResponse response)throws ServletException, IOException {    
    52.   45.    ServletOutputStream out=response.getOutputStream();    
    53.   46.    String[] args=(String[])request.getParameterValues("fruit");    
    54.   47.  for(int i=0;i<args.length;i++){    
    55.   48.     out.print(args[i]+"<br>");    
    56.   49.    }    
    57.   50.       
    58.   51. }    
    59.   52. }   
    60. 点击提交后:页面输出结果为“This is the service“;

    61. 所以,我们在写servlet的时候,一般都是重写doGet或doPost方法,不会管service方法。
    62. 欢迎加我的qq技术群425783133

这篇关于servlet中service() doGet() doPost() 方法的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/928441

相关文章

python获取指定名字的程序的文件路径的两种方法

《python获取指定名字的程序的文件路径的两种方法》本文主要介绍了python获取指定名字的程序的文件路径的两种方法,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要... 最近在做项目,需要用到给定一个程序名字就可以自动获取到这个程序在Windows系统下的绝对路径,以下

JavaScript中的高级调试方法全攻略指南

《JavaScript中的高级调试方法全攻略指南》什么是高级JavaScript调试技巧,它比console.log有何优势,如何使用断点调试定位问题,通过本文,我们将深入解答这些问题,带您从理论到实... 目录观点与案例结合观点1观点2观点3观点4观点5高级调试技巧详解实战案例断点调试:定位变量错误性能分

Python中 try / except / else / finally 异常处理方法详解

《Python中try/except/else/finally异常处理方法详解》:本文主要介绍Python中try/except/else/finally异常处理方法的相关资料,涵... 目录1. 基本结构2. 各部分的作用tryexceptelsefinally3. 执行流程总结4. 常见用法(1)多个e

JavaScript中比较两个数组是否有相同元素(交集)的三种常用方法

《JavaScript中比较两个数组是否有相同元素(交集)的三种常用方法》:本文主要介绍JavaScript中比较两个数组是否有相同元素(交集)的三种常用方法,每种方法结合实例代码给大家介绍的非常... 目录引言:为什么"相等"判断如此重要?方法1:使用some()+includes()(适合小数组)方法2

504 Gateway Timeout网关超时的根源及完美解决方法

《504GatewayTimeout网关超时的根源及完美解决方法》在日常开发和运维过程中,504GatewayTimeout错误是常见的网络问题之一,尤其是在使用反向代理(如Nginx)或... 目录引言为什么会出现 504 错误?1. 探索 504 Gateway Timeout 错误的根源 1.1 后端

MySQL 表空却 ibd 文件过大的问题及解决方法

《MySQL表空却ibd文件过大的问题及解决方法》本文给大家介绍MySQL表空却ibd文件过大的问题及解决方法,本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友参考... 目录一、问题背景:表空却 “吃满” 磁盘的怪事二、问题复现:一步步编程还原异常场景1. 准备测试源表与数据

python 线程池顺序执行的方法实现

《python线程池顺序执行的方法实现》在Python中,线程池默认是并发执行任务的,但若需要实现任务的顺序执行,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋... 目录方案一:强制单线程(伪顺序执行)方案二:按提交顺序获取结果方案三:任务间依赖控制方案四:队列顺序消

SpringBoot通过main方法启动web项目实践

《SpringBoot通过main方法启动web项目实践》SpringBoot通过SpringApplication.run()启动Web项目,自动推断应用类型,加载初始化器与监听器,配置Spring... 目录1. 启动入口:SpringApplication.run()2. SpringApplicat

解决Nginx启动报错Job for nginx.service failed because the control process exited with error code问题

《解决Nginx启动报错Jobfornginx.servicefailedbecausethecontrolprocessexitedwitherrorcode问题》Nginx启... 目录一、报错如下二、解决原因三、解决方式总结一、报错如下Job for nginx.service failed bec

使用Java读取本地文件并转换为MultipartFile对象的方法

《使用Java读取本地文件并转换为MultipartFile对象的方法》在许多JavaWeb应用中,我们经常会遇到将本地文件上传至服务器或其他系统的需求,在这种场景下,MultipartFile对象非... 目录1. 基本需求2. 自定义 MultipartFile 类3. 实现代码4. 代码解析5. 自定