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 }
    复制代码
  • 相关阅读:
    让ios支持openssl
    数组
    NSValue
    音频
    NSObject分类
    NSJSONSerialization
    Java并发编程-深入探讨synchronized实现原理
    设计模式-挖掘工厂模式应用场景-全面分析工厂模式原理
    Spring插件安装
    Java并发编程-深入Java同步器AQS原理与应用-线程锁必备知识点
  • 原文地址:https://www.cnblogs.com/ScvQ/p/6957193.html
Copyright © 2011-2022 走看看