• 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 }

     

  • 相关阅读:
    1061 Dating (20 分)
    1112 Stucked Keyboard (20 分)
    1077 Kuchiguse (20 分)
    1065 A+B and C (64bit) (20 分)
    1046 Shortest Distance (20 分)
    1124 Raffle for Weibo Followers (20 分)
    1036 Boys vs Girls (25 分)
    1113 Integer Set Partition (25 分)
    f开头的
    g开头的
  • 原文地址:https://www.cnblogs.com/adaonling/p/3536511.html
Copyright © 2020-2023  润新知