重新学习Servlet

public abstract class HttpServlet extends GenericServlet
package com.xh.test.api;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException; /**
* Created by root on 17-11-12.
*/
public class MyHttpServlet extends HttpServlet {
@Override
protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
super.service(req, resp);
System.out.println(">>>MyHttpServlet.service");
resp.getWriter().write("MyHttpServlet.service");
} @Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//super.doGet(req, resp);
System.out.println(">>>MyHttpServlet.doGet");
resp.getWriter().write("MyHttpServlet.doGet");
} @Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//super.doPost(req, resp);
System.out.println(">>>MyHttpServlet.doPost");
resp.getWriter().write("MyHttpServlet.doPost");
}
}

在restclient插件输入:http://localhost:8080/testservlet/1?id=100 ,切换方法GET,POST

输出是:

MyHttpServlet.doGetMyHttpServlet.service

MyHttpServlet.doPostMyHttpServlet.service

控制台:

>>>MyHttpServlet.doGet
>>>MyHttpServlet.service
>>>MyHttpServlet.doPost
>>>MyHttpServlet.service

PS:浏览器的请求过来,都要经过Servlet的service,如果自己不实现则走HttpServlet的。如果自己实现的,没有调用super.service(req, resp),

则直接返回,如果调用了super.service(req, resp),则HttpServlet会找当前类的对应的方法,如doGet,doPost,如果没有对应的实现,则抛异常,

如果实现了,就执行当前类的doGet,doPost方法,执行完后又返回到service方法,执行完返回给浏览器。

所以我们一般二者选其一实现(当然建议是doGet,doPost)。

    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 = req.getDateHeader(HEADER_IFMODSINCE);
if (ifModifiedSince < lastModified) {
// 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);
}
}
    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(HttpServletResponse.SC_METHOD_NOT_ALLOWED, msg);
} else {
resp.sendError(HttpServletResponse.SC_BAD_REQUEST, msg);
}
}

重新学习Servlet二的更多相关文章

  1. Servlet一(web基础学习笔记二十)

    一.Servlet简介 Servlet是sun公司提供的一门用于开发动态web资源的技术. Sun公司在其API中提供了一个servlet接口,用户若想用发一个动态web资源(即开发一个Java程序向 ...

  2. [转]Spring Security学习总结二

    原文链接: http://www.blogjava.net/redhatlinux/archive/2008/08/20/223148.html http://www.blogjava.net/red ...

  3. (转)SpringMVC学习(十二)——SpringMVC中的拦截器

    http://blog.csdn.net/yerenyuan_pku/article/details/72567761 SpringMVC的处理器拦截器类似于Servlet开发中的过滤器Filter, ...

  4. Alibaba Nacos 学习(二):Spring Cloud Nacos Config

    Alibaba Nacos 学习(一):Nacos介绍与安装 Alibaba Nacos 学习(二):Spring Cloud Nacos Config Alibaba Nacos 学习(三):Spr ...

  5. vue学习笔记(二)vue的生命周期和钩子函数

    前言 通过上一章的学习,我们已经初步的了解了vue到底是什么东西,可以干什么,而这一篇博客主要介绍vue的生命周期和它常用的钩子函数,如果有学过java的园友可能有接触到在学习servlet的时候学过 ...

  6. 这样学习Servlet,会事半功倍!!

    前言 工作已经有一段时间了,如果让我重新学Servlet,我会怎么学呢?下面抛出两个常见的问题,我分开来解答 2020年了,还需要学Servlet吗? Servlet的学习路线(学习重点) 一.202 ...

  7. crawler4j 学习(二)

    crawler4j 学习(二) 实现控制器类以制定抓取的种子(seed).中间数据存储的文件夹.并发线程的数目: public class Controller { public static voi ...

  8. 从零开始学习jQuery (二) 万能的选择器

    本系列文章导航 从零开始学习jQuery (二) 万能的选择器 一.摘要 本章讲解jQuery最重要的选择器部分的知识. 有了jQuery的选择器我们几乎可以获取页面上任意的一个或一组对象, 可以明显 ...

  9. Android Animation学习(二) ApiDemos解析:基本Animators使用

    Android Animation学习(二) ApiDemos解析:基本Animatiors使用 Animator类提供了创建动画的基本结构,但是一般使用的是它的子类: ValueAnimator.O ...

随机推荐

  1. wordpress 页面显示指定分类文章

    首页显示指定分类备份主题文件夹中的 index.php 文件,修改index.php找到如下一行代码:<?php if (have_posts()) : ?>在上面这行代码的前面加上:&l ...

  2. STM32外设地址查询

    问题的提出 DMA传输SDIO驱动的SD卡的数据,其中外设地址的确定 问题的解决 打开数据参考手册,在存储器和总线架构一章存储器映像小节,有一个寄存器组起始地址表,列举所有外设对应的起始地址,再到相应 ...

  3. 【bzoj1797】 Ahoi2009—Mincut 最小割

    http://www.lydsy.com/JudgeOnline/problem.php?id=1797 (题目链接) 题意 求一条边是否可能在一个最小割集中,以及这条边是否一定在最小割集中. Sol ...

  4. Java NIO -- 直接缓冲区与非直接缓冲区

    直接缓冲区与非直接缓冲区: 非直接缓冲区:通过 allocate() 方法分配缓冲区,将缓冲区建立在 JVM 的内存中直接缓冲区:通过 allocateDirect() 方法分配直接缓冲区,将缓冲区建 ...

  5. genetic model

    如果CC表示野生基型,CA因表示杂合型突变基因型,AA表示纯合型突变基因型.Recessive Model(隐性模型 ):AA VS (CA+CC);Dominant Model(显性模型):(CA+ ...

  6. 对信号量Semaphore的理解与运用

    转: java笔记--对信号量Semaphore的理解与运用 java Semaphore 信号量的使用: 在java中,提供了信号量Semaphore的支持. Semaphore类是一个计数信号量, ...

  7. Ubuntu下安装nfs服务器

    安装 NFS 软件包在 ubuntu 终端输入下面命令安装 NFS 服务器:vmuser@Linux-host: ~$ sudo apt-get install nfs-kernel-server # ...

  8. 高效的SQLSERVER分页查询

    Sqlserver数据库分页查询一直是Sqlserver的短板,闲来无事,想出几种方法,假设有表ARTICLE,字段ID.YEAR...(其他省略),数据53210条(客户真实数据,量不大),分页查询 ...

  9. python 正则括号的使用及踩坑

    直接先上结论: 若匹配规则里有1个括号------返回的是括号所匹配到的结果, 若匹配规则里有多个括号------返回多个括号分别匹配到的结果, 若匹配规则里没有括号------就返回整条语句所匹配到 ...

  10. Js中this机制全解

    JavaScript中有很多令人困惑的地方,或者叫做机制. 但是,就是这些东西让JavaScript显得那么美好而与众不同. 比方说函数也是对 象.闭包.原型链继承等等,而这其中就包括颇让人费解的th ...