zoukankan      html  css  js  c++  java
  • Servlet创建、编译、部署、运行

              最近在做一个通过Servlet实现后台批量接收上传文件的东西,现将Servlet的开发、运行配置、调用记录下来。我们以最简单的FileUpload为例,目前所有的http协议相关的Servlet均继承于HttpServlet。

    •     Servlet开发


                 Servlet的开发都必须将Servlet-api.jar引用到项目的lib目录中,具体如何引用,不详细描述。开发HttpServer必须扩展HttpServer类。HttpServlet 类包含

                 init():在 Servlet 的生命期中,仅执行一次 init() 方法。它是在服务器装入 Servlet 时执行的。 可以配置服务器,以在启动服务器或客户机首次访问 Servlet 时装入 Servlet。 无论有多少客户机访问 Servlet,都不会重复执行 init() 。缺省的 init() 方法通常是符合要求的,但也可以用定制 init() 方法来覆盖它。

                destroy():destroy() 方法仅执行一次,即在服务器停止且卸装Servlet 时执行该方法。

                service() :service() 方法是 Servlet 的核心。每当一个客户请求一个HttpServlet 对象,该对象的service() 方法就要被调用,而且传递给这个方法一个"请求"(ServletRequest)对象和一个"响应"(ServletResponse)对象作为参数。 在 HttpServlet 中已存在 service() 方法。缺省的服务功能是调用与 HTTP 请求的方法相应的 do 功能。
    Servlet的响应可以是下列几种类型:一个输出流,浏览器根据它的内容类型(如text/HTML)进行解释。一个HTTP错误响应, 重定向到另一个URL、servlet、JSP。

                doGet():当一个客户通过HTML 表单发出一个HTTP GET请求或直接请求一个URL时,doGet()方法被调用。与GET请求相关的参数添加到URL的后面,并与这个请求一起发送。当不会修改服务器端的数据时,应该使用doGet()方法。

                doPost():当一个客户通过HTML 表单发出一个HTTP POST请求时,doPost()方法被调用。与POST请求相关的参数作为一个单独的HTTP 请求从浏览器发送到服务器。当需要修改服务器端的数据时,应该使用doPost()方法。

                getServletConfig():GetServletConfig()方法返回一个 ServletConfig 对象,该对象用来返回初始化参数和ServletContext。ServletContext 接口提供有关servlet 的环境信息。

                getServletInfo():它提供有关servlet 的信息,如作者、版本、版权。

    serlet代码简单示例

    import java.io.*;
    import javax.servlet.*;
    import javax.servlet.http.*;
    
    public class UploadFiles extends HttpServlet {
        protected void processRequest(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
        {
            response.setContentType("text/html");
            PrintWriter out = response.getWriter();
          
            String title = "Using GET Method to Read Form Data";
            String docType = "<!doctype html public "-//w3c//dtd html 4.0 " +
                             "transitional//en">
    ";
                           
            out.println(docType +
                        "<html>
    " +
                        "<head><title>" + title + "</title></head>
    " +
                        "<body bgcolor="#f0f0f0">
    " +
                        "<h1 align="center">" + title + "</h1>
    " +
                        "<ul>
    " +
                        "  <li><b>First Name</b>: "+ request.getParameter("first_name") + "
    " +
                        "  <li><b>Last Name</b>: "+ request.getParameter("last_name") + "
    " +
                        "</ul>
    " +
                        "</body></html>");
      } 
    
        @Override
        public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
            processRequest(request, response);
        }
    
        @Override
        public void doPost(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException {
            processRequest(request, response);
        }
    
        @Override
        public String getServletInfo() {
            return "Hellow welcome!";
        }// 
    
    
    }
    • Servlet运行配置


                  在项目中的创建一个web.xml配置文件,存放在webcontent/WEB-INF/web.xml下。web.xml内容如下:

    <?xml version="1.0" encoding="UTF-8"?>
    <web-app>
      <servlet>
        <servlet-name>FileUpLoad</servlet-name>        
        <servlet-class>cn/com/Convert/Document/FileUpLoad</servlet-class>
      </servlet>
      <servlet-mapping>
        <servlet-name>FileUpLoad</servlet-name>
        <url-pattern>/FileUpLoad</url-pattern>
      </servlet-mapping>
    </web-app>
    



    配置说明:

            1、servlet-name  节点为Servlet名称,可以自行任意定义,但必须保持<servlet>和<servlet-mapping>两个节点中的子节点<servlet-name>的节点值保持一致。

            2、<servlet-class>节点中是配置servlet类的路径,即包 + 类名,如cn.com.Convert.Document/FileUpLoad 。

            3、<url-pattern>节点中是配置servlet的虚拟路径,一般保持与servlet名称一致,在实际访问的地址为http://localhost:8080/项目名称/servlet名称

            4、要使servlet生效必须重启tomcat,所有web.xml的修改都必须重启tomcat

    • Servlet3新特性

                 在Servlet3的新特性中,我们配置Servlet更加简单方便,只需要在Servlet类代码前面加上注解就可以,无需在进行web.xml配置。注解示例

                @WebServlet(name="FileUpLoad", urlPatterns="/FileUpLoad")     其中name为servlet名称, urlPatterns为servlet访问路径

                 servlet还可以进行其他参数配置,诸如异步返回、设置

    • Servlet调用
    <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
    <html>
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
    <title>Insert title here</title>
    </head>
    <body>
    <form id="frmPost" name="frmPost" action="http://localhost:8080/DocumentConvert/FileUpLoad" method="post" enctype="multipart/form-data" >
      <input type="file" id="file1" name="file">
      <input type="file" id="file2" name="file">
      <input type="file" id="file3" name="file">
      <input type="file" id="file4" name="file">
      <input type="submit" name="Submit" value="Submit">
    </form>
    </body>
    </html>


    • Servlet参考资料


    1·、https://www.ibm.com/developerworks/cn/java/j-lo-servlet/

    2、http://www.yiibai.com/servlets/servlets_form_data.html

    3、http://www.oschina.net/question/12_52027

    4、http://baike.baidu.com/view/25169.htm






  • 相关阅读:
    Terminologies in MVC: Part 2 (Razor Engine Syntax vs Web Form)
    what is diff. b/w app state & session state
    ASP.NET Web Pages (Razor) FAQ
    _AppStart.cshtml 和 _PageStart.cshtml的妙用
    系统编程--信号
    系统编程--进程间通信
    系统编程--进程
    系统编程--标准IO
    系统编程--文件IO
    网络--路由表&IP选路
  • 原文地址:https://www.cnblogs.com/wala-wo/p/5119254.html
Copyright © 2011-2022 走看看