当前位置首页 > 中学教育 > 初中课件
搜柄,搜必应! 快速导航 | 使用教程  [会员中心]

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

文档格式:DOC| 6 页|大小 48KB|积分 15|2022-11-04 发布|文档ID:167604131
第1页
下载文档到电脑,查找使用更方便 还剩页未读,继续阅读>>
1 / 6
此文档下载收益归作者所有 下载文档
  • 版权提示
  • 文本预览
  • 常见问题
  • 实验二: 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(" A Servlet"); 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(" Headers Servlet"); out.println(" "); out.println("
    "); out.println("List of all Headers in Servlet Request"); 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

    "); out.println(""); out.println(""); Enumeration headerNames = request.getHeaderNames(); while (headerNames.hasMoreElements()) { String headerName = (String) headerNames.nextElement(); out.println(""); out.println(""); } out.println("
    NameValue
    " + headerName + "" + request.getHeader(headerName) + "
    "); out.println(" "); out.println(""); out.flush(); out.close(); }(2) 运行http://localhost:8080/ServletTest/servlet/HeadersServlet2.3 FormParameterServlet参见视频:FormParameterServlet.rmvb1. 在ServletTest项目的WebRoot下,新建一html文件FormParameter.html,代码如下: FormParameter.html
    Employee Information Form

    This example shows you how the form parameters are handled by servlet. Please enter information and click the submit button to see results.

     
      Last Name
      First Name
     Know Java Know C++
     Working
     
     
     
         

    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(" Form Parameter Servlet"); out.println(" "); out.println(""); out.println("List of all Parameters Sent from the Browser"); out.println(""); out.println("
    "); out.println("Parameter Lists:
    "); out.println(""); out.println(""); Enumeration params = request.getParameterNames(); while (params.hasMoreElements()) { String p = (String) params.nextElement(); String [] paramArr = request.getParameterValues(p); boolean first = true; String val = ""; for (int i = 0; i < paramArr.length; i ++) { if (!first) { val = val + ", "; } else { first = false; } val = val + paramArr[i]; } out.println(""); } out.println("
    NameValue
    " + p + "" + val + "
    "); out.println("
    "); 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的生命周期。

    点击阅读更多内容
    卖家[上传人]:xueyuzhun398
    资质:实名认证