What are Java Servlets?

来源:互联网 发布:森海塞尔ie80淘宝 编辑:程序博客网 时间:2024/04/24 13:52
   The Java Servlet API is an open standard,created by sun,for creating web applications using the Java programming language. A servlet is an object responsible for receiving a request from a client web browser and returning a response-an HTML page to be displayed in the browser.The Servlet API defines an interface and base class for servlets,as well as interfaces for several other supporting objects,such as the HttpServletRequest (which represents a request and allows the servlet to access query parameters).Vendors,both open source and proprietary,provide the actual server code and implementations of the standard interfaces.
    Servlets are most often paired with JavaServer Pages in order to generate a reponse.JSPs are a standard templating technology for servlets.A JSP is a mix of ordinary,static HTML with additional,specialized tags and directives that provide dynamic output,such as including the current user's name or the contents of an online shopping cart.Under the hood,each JSP is converted into a Java servlet that is compiled and loaded into memory.More information about servlets and JSPs is available online and in print ,including Web Development with JavaServer Pages.
    A servlet operates within a servlet container.The servlet container serves as a bridge between HTTP and the Java servlet code that you, as the developer ,will write.The servlet container is responsible for instantiating and initializing your servlets as needed.Servlet container s may be standalone,,such as Apache Tomcat,or may be one part of an  overall application server,such as BEA  WebLogic,IBM WebSphere ,or the open source JBoss application server.
    The servlet container is responsible for selecting the correct servlet to invoke based on the URL of the request;a single web application will contain many servlets.The web application's deployment descriptor gives the name and Java class for each servlet and identifies which URLs are mapped to which servlets.For example,your application may have a registration page containing an HTML form enabling new nsers to reggister .That form will submit to the addCustomer URL,which is mapped to the addCustomer servlet ,which is instantiated as the AddCustomerServlet class.The deployment descriptor will include the following elements describing the addCustomer servlet's configuration:
    <servlet>
       <servlet-name>addCustomer</servlet-name>
       <servlet-class>com.mycompany.AddCustomerServlet</servlet-classs>
    </servlet>
   
    <servlet-mapping>
       <servlet-name>addCustomer</servlet-name>
       <url-pattern>/addCustomer</url-pattern>
    </servlet-mapping>
    As can be seen from this example,each servlet represents a particular operation within the overall web application.The AddCustomerServlet class will include code that reads the query parameters submitted with form and uses those values to create some kind of Customer object that can be stored into a database.Additional logic must decide what form of response to display-normally a confirmation page,but possibly the originalpage containing the form if an error occurred when creating the customer.

原创粉丝点击