文档详情

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

xuey****n398
实名认证
店铺
DOC
48KB
约6页
文档ID:167604131
实验二、Servlet的创建、部署与运行_第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的生命周期。

下载提示
相关文档
正为您匹配相似的精品文档