日期:2014-05-18 浏览次数:20644 次
import java.io.IOException; import java.io.PrintWriter; import javax.servlet.*; import javax.servlet.http.*; public class ColorServlet extends HttpServlet { /** * */ private static final long serialVersionUID = -1545036959630097445L; protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { String color=req.getParameter("color"); String colorName=""; if("red".equals(color)){ colorName="红色"; }else if("yellow".equals(color)){ colorName="黄色"; }else if("blue".equals(color)){ colorName="蓝色"; }else if("white".equals(color)){ colorName="白色"; }else if("black".equals(color)){ colorName="黑色"; } resp.setContentType("test/html/;charset=GBK"); PrintWriter pw=resp.getWriter(); pw.print("<html><body>"); pw.print("你选择了<b>"+colorName+"</b><br>"); pw.print("</body></html>"); //pw.close(); } }
<?xml version="1.0" encoding="ISO-8859-1"?> <web-app xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" version="2.5"> <servlet> <servlet-name>name</servlet-name> <servlet-class>ColorServlet</servlet-class> </servlet> <servlet-mapping> <servlet-name>name</servlet-name> <url-pattern>/ColorServlet/*</url-pattern> </servlet-mapping> </web-app>