servlet中service() doGet() doPost() 方法
HttpServlet 里的三个方法:service(HttpServletRequest req, HttpServletResponse resp) ,doGet(HttpServletRequest req, HttpServletResponse resp), doPost(HttpServletRequest req, HttpServletResponse res)的区别和联系:
- 在servlet中默认情况下,无论你是get还是post 提交过来 都会经过service()方法来处理,然后转向到doGet
- 注意,sun只是定义了servlet接口,而实现servlet接口的就是类似于tomcat的服务器,所以我是在tomcat的安装目录下找到实现的类。
- 从上面可以看出 这里的service是用来转向的,但是如果你在自己的servlet类中覆盖了service方法,比如说你的service是这样的:
- Java代码
- 1. public void service(ServletRequest req, ServletResponse res)
- 2. throws ServletException, IOException {
- 3. res.getOutputStream().print(
- 4. "image is <img src='images/downcoin.gif'></img><br>");
- 5. }
- Java代码
- 1. <%@page contentType="text/html; charset=utf-8"%>
- 2. <html>
- 3. <head><title>选择</title></head>
- 4. <body>
- 5. 请选择你喜欢的水果:<br>
- 6. <form action = "Test" method = "post">
- 7. <input type="checkbox" name="fruit" value ="apple" >苹果<br>
- 8. <input type="checkbox" name="fruit" value ="orange">桔子<br>
- 9. <input type="checkbox" name="fruit" value ="mango">芒果<br>
- 10. <input type="submit" value="提交">
- 11. </form>
- 12. </body>
- 13. </html>
- 14.
- 15. 服务端servlet是:Test类
- 16.
- 17. import java.io.IOException;
- 18.
- 19. import javax.servlet.ServletException;
- 20. import javax.servlet.ServletOutputStream;
- 21. import javax.servlet.ServletRequest;
- 22. import javax.servlet.ServletResponse;
- 23. import javax.servlet.http.HttpServlet;
- 24. import javax.servlet.http.HttpServletRequest;
- 25. import javax.servlet.http.HttpServletResponse;
- 26.
- 27. /**
- 29. */
- 30. public class Test extends HttpServlet {
- 31.
- 32. public void service(ServletRequest req, ServletResponse res)
- 33. throws ServletException, IOException {
- 34. res.getOutputStream().print("This is the service");
- 35.
- 36. }
- 37.
- 38. protected void doGet(HttpServletRequest request,
- 39. HttpServletResponse response) throws ServletException, IOException {
- 40. doPost(request,response);
- 41.
- 42. }
- 43. protected void doPost(HttpServletRequest request,
- 44. HttpServletResponse response) throws ServletException, IOException {
- 45. ServletOutputStream out=response.getOutputStream();
- 46. String[] args=(String[])request.getParameterValues("fruit");
- 47. for(int i=0;i<args.length;i++){
- 48. out.print(args[i]+"<br>");
- 49. }
- 50.
- 51. }
- 52. }
- 所以,我们在写servlet的时候,一般都是重写doGet或doPost方法,不会管service方法。
servlet中service() doGet() doPost() 方法的更多相关文章
- servlet 中 service ,doGet , doPost 关系
web.xml <?xml version="1.0" encoding="UTF-8"?> <web-app version="2 ...
- [转]servlet中的service, doGet, doPost方法的区别和联系
原文地址:http://m.blog.csdn.net/blog/ghyg525/22928567 大家都知道在javax.servlet.Servlet接口中只有init, service, des ...
- servlet中的doGet()与doPost()以及service()的用法
doget和dopost的区别 get和post是http协议的两种方法,另外还有head, delete等 1.这两种方法有本质的区别,get只有一个流,参数附加在url后,大小个数有严格限制且只能 ...
- Java之通过反射机制选择servlet中的对应的方法
此方法用于在对Javaee开发中的通过对应的名称而选择servlet中的对应的方法 注:主要代码如下 protected void doGet(HttpServletRequest req, Http ...
- servlet中service() 和doGet() 、doPost() 学习笔记
Sevlet接口定义如下: 与Sevlet接口相关的结构图: service() 方法是 Servlet 的核心.每当一个客户请求一个HttpServlet 对象,该对象的service() 方法就要 ...
- SERVLET中的doGet与doPost两个方法之间的区别
get和post是http协议的两种方法,另外还有head, delete等 这两种方法有本质的区别,get只有一个流,参数附加在url后,大小个数有严格限制且只能是字符串.post的参数是通过另外的 ...
- Servlet中Service方法
doGet方法只能处理Get方式提交的请求,doPost则可以处理Post方式提交的请求, 一种既可以处理Get方式又可以处理Post方式的提交的请求,它就是Service方法. service方法用 ...
- Servlet中service()方法
在学习Servlet的过程中,我们大多时候编码都是直接继承HttpServlet这个类,并且重写doGet ,doPost,但是查看Api时我们会发现Servlet接口 ,GenericSevlet抽 ...
- Servlet 小试牛刀(doGet,doPost)
实验说明: 通过javax.servlet.http下的HttpServlet,HttpServletRequest,HttpServletResponse来完成一些常用Servlet实例 java代 ...
随机推荐
- 【BZOJ3124】[Sdoi2013]直径 树形DP(不用结论)
[BZOJ3124][Sdoi2013]直径 Description 小Q最近学习了一些图论知识.根据课本,有如下定义.树:无回路且连通的无向图,每条边都有正整数的权值来表示其长度.如果一棵树有N个节 ...
- HashMap和ConcurrentHashMap的区别,HashMap的底层源码。
Hashmap本质是数组加链表.根据key取得hash值,然后计算出数组下标,如果多个key对应到同一个下标,就用链表串起来,新插入的在前面. ConcurrentHashMap:在hashMap的基 ...
- CSS3随意记录
1.注释 注释语法:/* 注解注释内容 */ 2.带有透明度 rgba(255,0,0,0.5);rgba(0,255,0,0.5);rgba(0,0,255,0.5); 0.5就带有透明的,介于0和 ...
- [转载]Apache在windows下的安装配置
Apache在windows下的安装配置 转载自:http://blog.sina.com.cn/s/blog_536f16b00100cfat.html 1 Apache的下载 Apache ...
- AWS入门-1
对于 Amazon Linux AMI,用户名为 ec2-user. 对于 RHEL AMI,用户名称是 ec2-user 或 root. 对于 Ubuntu AMI,用户名称是 ubuntu 或 r ...
- 我的Android进阶之旅------>百度地图学习:BDLocation.getLocType ( )值分析
BDLocation类,封装了定位SDK的定位结果,在BDLocationListener的onReceive方法中获取.通过该类用户可以获取error code,位置的坐标,精度半径等信息.具体方法 ...
- Cocos2d-x3.0游戏实例之《别救我》第八篇——TiledMap实现关卡编辑器
版权声明:本文为博主原创文章,未经博主同意不得转载. https://blog.csdn.net/musicvs/article/details/25368273 好吧.我真心全然搞不懂.我如今仅仅只 ...
- Android之四大组件、六大布局、五大存储
[-] Android六大界面布局方式 1 LinearLayout线性布局 LinearLayout的常用XML属性及相关方法 LinearLayout子元素支持的常用XML属性及方法 2 Tabl ...
- Hexo 使用中搭建博客过程中遇到的坑
本地执行hexo s 时报错: WARN No layout: index.html 原因:theme 没有下载下来,经查,theme文件夹下为空. 新建文章后,执行 hexo g 时报如下错误: ( ...
- PAT 天梯赛 L1-024. 后天 【取余】
题目链接 https://www.patest.cn/contests/gplt/L1-024 题意 给出一个数,表示星期几,输出后天是星期几 思路 取余的时候要小心点 AC代码 #include & ...