前三章
1、servlet name
<web-app ...> <servlet> <servlet-name>Internal name 1</servlet-name> <servlet-class>foo.Servlet1</servlet-class> </servlet> <servlet> <servlet-name>Internal name 2</servlet-name> <servlet-class>foo.Servlet2</servlet-class> </servlet> <servlet-mapping> <servlet-name>Internal name 1</servlet-name> <url-pattern>/Public1</url-pattern> </servlet-mapping> <servlet-mapping> <servlet-name>Internal name 2</servlet-name> <url-pattern>/Public2</url-pattern> </servlet-mapping> </web-app>
2、P80简单Servlet
package com.example.web; import javax.servlet.*; import javax.servlet.http.*; import java.io.*; public class BeerSelect extends HttpServlet { public void doPost(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException { response.setContentType(“text/html”); PrintWriter out = response.getWriter(); out.println(“Beer Selection Advice<br>”); String c = request.getParameter(“color”); out.println(“<br>Got beer color “ + c); } }
P89简单dispatch至JSP
package com.example.web; import javax.servlet.*; import javax.servlet.http.*; import java.io.*; public class BeerSelect extends HttpServlet { public void doPost(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException { response.setContentType(“text/html”); PrintWriter out = response.getWriter(); request.setAttribute(“styles”, result); RequestDispatcher view = request.getRequestDispatcher(“result.jsp”); view.forward(request, response); } }