What are Java Servlets?

   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.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值