日期:2014-05-18 浏览次数:20696 次
import java.io.*; import javax.servlet.*; import javax.servlet.http.*; public class CounterServlet extends HttpServlet { private static final String CONTENT_TYPE = "text/html; charset=GBK"; private int number; public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setContentType (CONTENT_TYPE); PrintWriter out = response.getWriter(); int n = 1; number = Integer.parseInt(request.getParameter("DATA")); for (int i = 1; i <= number; i++) { n *= i; } out.println("<h2 align='center'>" + "计算结果" + "</h2>"); out.println("<html>"); out.println("<head><title>TestServlet</title></head>"); out.println("<body>"); out.println("<P>"+number +"! = " + n + "</P>"); out.println("</body></html>"); out.close(); } }
<servlet> <servlet-name>CounterServlet</servlet-name> <servlet-class>CounterServlet</servlet-class> </servlet> <servlet-mapping> <servlet-name>CounterServlet</servlet-name> <url-pattern>/CounterServlet</url-pattern> </servlet-mapping>
<html> <head> <title>网上购物</title> </head> <body bgcolor="white"> <table border="10" cellpadding="10" align="CENTER"> <tr><td width="275"> <h2>商品信息查询</h2> <br>输入你要查询的信息<br> <form method="post" action="CounterServlet"> <input type= "text" name="DATA" size=30> <p> <input type="SUBMIT" value= "查询"> <input type="RESET" value= "重新输入"> </form> </td></tr> </table> </body> </html>