实验二、Servlet的创建、部署与运行

实验二: Servlet的创建、部署与运行一、 实验目的1. 掌握Servlet创建与配置方法;2. 掌握Servlet分析客户请求的方法;3. 掌握Servlet处理表单数据的方法;4. 掌握Web应用程序部署到Tomcat上的方法;二、 实验内容2.1 SimpleServlet1. 打开MyEclipse, 创建一个Web Project,命名为ServletTest,过程参见视频 simpleServlet.avi(1) 其中SimpleServlet.java的doGet方法源代码如下: /** * The doGet method of the servlet.
* * This method is called when a form has its tag value method equals to get. * * @param request the request send by the client to the server * @param response the response send by the server to the client * @throws ServletException if an error occurred * @throws IOException if an error occurred */ public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setContentType("text/html"); PrintWriter out = response.getWriter(); out.println(""); out.println(""); out.println("
Hello, I'm a simple servlet!
"); out.println(" "); out.println(""); out.flush(); out.close(); }(2) 运行http://localhost:8080/ServletTest/servlet/SimpleServlet2.2 HeadersServlet1. 在ServletTest项目中,新建一Servlet,命名为HeadersServlet,参见视频HeadersServlet.rmvb:(1) 其doPost方法代码如下: /** * The doPost method of the servlet.* * This method is called when a form has its tag value method equals to post. * * @param request the request send by the client to the server * @param response the response send by the server to the client * @throws ServletException if an error occurred * @throws IOException if an error occurred */ public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setContentType("text/html"); PrintWriter out = response.getWriter(); out.println(""); out.println(""); out.println("
"); out.println("
Request Line is:
"); out.println("METHOD: " + request.getMethod() + ""); out.println("URI:" + request.getRequestURI() + "
"); out.println("PROTOCOL:" + request.getProtocol() + "
"); out.println("
Header Name and Values
Name | Value |
---|---|
" + headerName + " | "); out.println("" + request.getHeader(headerName) + " |
This example shows you how the form parameters are handled by servlet. Please enter information and click the submit button to see results.
2. 新建名为FormParameterServlet的Servlet, 其doPost方法代码摘录如下: /** * The doPost method of the servlet.
* * This method is called when a form has its tag value method equals to post. * * @param request the request send by the client to the server * @param response the response send by the server to the client * @throws ServletException if an error occurred * @throws IOException if an error occurred */ public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setContentType("text/html"); PrintWriter out = response.getWriter(); out.println(""); out.println(""); out.println("
"); out.println("Parameter Lists:
"); out.println("
Name | Value |
---|---|
" + p + " | " + val + " |
"); out.println(" "); out.println(""); out.flush(); out.close(); }3. 运行(1) 输入http://localhost:8080/ServletTest/FormParameter.html,输入信息后点击 Submit按钮,查看结果2.4 RequestCounterServlet请参照教材第60页的“程序例4-4”,完成RequestCounterServlet代码。
通过该例理解Servlet的生命周期。