package cn.zhao.servlet ; import java.io.IOException ; import java.io.PrintWriter; import javax.servlet.ServletException ; import javax.servlet.http.HttpServlet ; import javax.servlet.http.HttpServletRequest ; import javax.servlet.http.HttpServletResponse ; public class HelloServlet extends HttpServlet { private static final long serialVersionUID = 1L ; protected void doGet( HttpServletRequest req , HttpServletResponse rep ) throws ServletException , IOException { this.doPost( req , rep ) ; } protected void doPost( HttpServletRequest req , HttpServletResponse rep ) throws ServletException , IOException { rep.setContentType( "text/html;charset=gbk" ) ; PrintWriter out = rep.getWriter() ; out.println( "<font color=red ><h1>大家好,这是一个Servlet!!!!!</h1></font>" ) ; out.close() ; } } <?xml version="1.0" encoding="UTF-8"?> <web-app version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"> <welcome-file-list> <welcome-file>index.jsp</welcome-file> </welcome-file-list> <servlet> <servlet-name>HelloServlet</servlet-name> <servlet-class>cn.zhao.servlet.HelloServlet</servlet-class> </servlet> <servlet-mapping> <servlet-name>HelloServlet</servlet-name> <url-pattern>/hello</url-pattern> </servlet-mapping> </web-app>