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 }