zoukankan      html  css  js  c++  java
  • servlet三种实现方式之三通过继承HttpServlet开发servlet

    servlet有三种实现方式:

    1.实现servlet接口

    2.继承GenericServlet

    3.通过继承HttpServlet开发servlet

     

    第三种:

     

     1 import java.io.*;
     2 
     3 import javax.servlet.ServletException;
     4 import javax.servlet.http.*;
     5 
     6 
     7 public class hellohttp extends HttpServlet {
     8 
     9     //处理get请求
    10     //req用于获取客户端(浏览器)的信息
    11     //res用于向客户端(浏览器)返回信息
    12     public void doGet(HttpServletRequest req, HttpServletResponse res)
    13             throws ServletException, IOException {
    14         
    15         //业务逻辑
    16         try{
    17             PrintWriter pw=res.getWriter();
    18             pw.println("hello,http");
    19         }catch(Exception e){
    20             e.printStackTrace();
    21         }
    22     }
    23     //处理post请求
    24     //req用于获取客户端(浏览器)的信息
    25     //res用于向客户端(浏览器)返回信息
    26     public void doPost(HttpServletRequest req,HttpServletResponse res)
    27             throws ServletException,IOException
    28     {
    29         //合二为一,业务逻辑处理方式都一样
    30         this.doGet(req, res);
    31     }
    32 
    33     public void init() throws ServletException{
    34         // Put your code here
    35     }
    36 
    37     public void destroy() {
    38         super.destroy(); // Just puts "destroy" string in log
    39         // Put your code here
    40     }
    41 }

     

  • 相关阅读:
    bootstrap class sr-only 什么意思?
    PHP 中的文本截取分析之效率
    FastAdmin 升级后出现 is already in use
    FastAdmin 环境变量 env 配置
    Nginx 服务器伪静态配置不当造成 Access denied
    笔记:明确认识
    进程通信(socket)
    进程间通信(了解)
    c++ 继承,组合
    c++ 类初始化
  • 原文地址:https://www.cnblogs.com/adaonling/p/3536511.html
Copyright © 2011-2022 走看看