本例以HelloWorld为例。 1.编写HelloWorldServlet.java package jp.co.xiehl.servlet.ch7; 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 HelloWorldServlet extends HttpServlet { /** * */ private static final long serialVersionUID = -5370049563045822665L; @Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { PrintWriter out = resp.getWriter(); out.println("<html>"); out.println("<head>"); out.println("<title>HelloWorld</title>"); out.println("</head>"); out.println("<body bgcolor=/"lightblue/">"); out.println("<hr>"); out.println("HelloWorld"); out.println("</body>"); out.println("</html>"); } @Override protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { doPost(req, resp); } } 2.在web.xml中配置servlet <servlet> <servlet-name>HelloWorld</servlet-name> <servlet-class>jp.co.xiehl.servlet.ch7.HelloWorldServlet</servlet-class> </servlet> <servlet-mapping> <servlet-name>HelloWorld</servlet-name> <url-pattern>/hello</url-pattern> </servlet-mapping> 3.运行 http://127.0.0.1/HibernateStudy/hello