博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
servlet中避免405错误的产生
阅读量:7071 次
发布时间:2019-06-28

本文共 1424 字,大约阅读时间需要 4 分钟。

 父类Parent(相当于HttpServlet):service方法,用于处理任务分发,doGet、doPost方法用于报错

  关注的是子类Son(servlet)
     目的:杜绝错误的产生
 方式:
 第一种:重写父类的service方法,必须去掉super.service(req, resp);
 第二种:重写父类的doGet(去掉super.doGet();)、doPost(去掉super.doPost();)方法,调用父类的service方法

 

例如:

package com.bjsxt.second.servlet;

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class SecondServlet extends HttpServlet {
    
    /*@Override
    //第一种避免405错误的方法,重写servcie方法,去掉super.service(req, resp);
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        // TODO Auto-generated method stub
        //super.service(req, resp);
        System.out.println("SecondServlet.service()");
    }*/
    
    
    @Override
    //第二种避免405错误的方法,重写doGet方法,去掉super.doGet(req, resp);
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        // TODO Auto-generated method stub
        //super.doGet(req, resp);
        System.out.println("SecondServlet.doGet()");
    }
    
    @Override
    //第二种避免405错误的方法,重写doPost方法,去掉super.doPost(req, resp);
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        // TODO Auto-generated method stub
        //super.doPost(req, resp);
        System.out.println("SecondServlet.doPost()");
    }
}

转载于:https://www.cnblogs.com/z0228-0322x/p/6155585.html

你可能感兴趣的文章
npm打包
查看>>
为Sublime Text 2安装Emmet(原Zen Coding)
查看>>
使用 IBM Data Studio 开发调试 DB2 存储过程
查看>>
Hyperledger Fabric 客户端开发二
查看>>
hibernate插入CLOB大数据类型
查看>>
4、ES5对Object对象的扩展。
查看>>
block的回调作用
查看>>
如何实现类似微信朋友圈的feed功能(第一版)
查看>>
安装NODEJS的三种方法
查看>>
如何让Mac完全读写NTFS格式分区
查看>>
百万级很快的分页联合
查看>>
手机内存卡修复工具软件大师免费试用版
查看>>
获取屏蔽符号<!-- -->屏蔽的字符串的代码
查看>>
struct和typedef struct
查看>>
Notification启动Activity, 恢复任务栈
查看>>
使用Python进行并发编程
查看>>
自动机器学习简述(AutoML)
查看>>
iPhone X适配
查看>>
虚拟化笔记
查看>>
[vim]-vim基础
查看>>