开发Servlet的三种方式续......

二、

//使用继承GenericServlet的方法开发
package com.test;
import javax.servlet.GenericServlet;
import java.io.*;
import javax.servlet.*;

public class HelloGenericServlet extends GenericServlet{

	//重写service方法
	public void service(ServletRequest req, ServletResponse res){
		try{
			PrintWriter pw = res.getWriter();
			pw.println("Hello GenericServlet");
		}catch(Exception ex){
			ex.printStackTrace();
		}
	}
}

 三、开发Servlet最常用的方式

//使用继承HttpServlet的方法开发
package com.test;
import javax.servlet.http.*;
import java.io.*;
import javax.servlet.*;
public class HelloHttpServlet extends HttpServlet {

	//处理Get请求
	public void doGet(HttpServletRequest req, HttpServletResponse res){
		try{  
			res.setContentType("text/html; charset=gb2312"); //处理乱码   
			PrintWriter pw = res.getWriter();
			pw.println("<html><body><p>Hello HttpServlet2你好3</p></body></html>");
		}catch(Exception ex){
			ex.printStackTrace();
		}
		
	}
	//处理post请求
	public void doPost(HttpServletRequest req, HttpServletResponse res){
		doGet(req, res);
	}
    
}
 

你可能感兴趣的:(html,servlet)