7.1 Skema umum web.xml

File web.xml nyimpen informasi konfigurasi aplikasi. Iku dudu bagean wajib, nanging digunakake banget kanggo ngatur aplikasi web.

Berkas iki kudu ana ing folder WEB-INF . Nalika Tomcat diwiwiti, maca isine lan nggunakake konfigurasi sing ana. Yen file kasebut ngemot kesalahan, Tomcat uga nampilake kesalahan.

Tuladha web.xml:



<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee
  http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd" version="4.0">
  
  <servlet>
       <servlet-name>HelloWorld</servlet-name>
       <servlet-class>HelloServlet</servlet-class>
   </servlet>
    
  <servlet-mapping>
      <servlet-name>HelloWorld</servlet-name>
      <url-pattern>/welcome</url-pattern>
  </servlet-mapping>
 
  <welcome-file-list>
      <welcome-file>index.html</welcome-file>
  </welcome-file-list>
  
</web-app>

"HelloWorld"Pemetaan jeneng servlet lan kelas servlet ditulis ing kene kanthi warna ijo"HelloServlet" . Pemetaan jeneng servlet lan potongan URL ditulis nganggo warna biru"HelloWorld""http://localhost/welcome" . Mangkono, ngandika ing kene nalika ngakses / welcome path, sampeyan kudu nelpon servlet HelloServlet.class.

Werna abang nuduhake file sing kudu diwenehake nalika dijaluk http://localhost/- iki sing diarani kaca sambutan . Yen pangguna mung ngetik ing browser jeneng sing cocog karo ROOT saka aplikasi web kita, banjur isi saka index.html.

7.2 servlet, servlet-pemetaan

Siji servlet bisa nglayani panjalukan ing URL sing beda-beda, mula ing web-xml, servlet lan pemetaan menyang URL ditulis kanthi kapisah. Pisanan, kita njlèntrèhaké servlets, menehi saben jeneng string unik, banjur kita nemtokake carane saben servlet map kanggo url.

Tuladha web.xml:



<web-app>
  
  <servlet>
    <servlet-name>remoting</servlet-name>
    <servlet-class>com.codegym.RemotingServlet</servlet-class>
    <load-on-startup>1</load-on-startup>
  </servlet>
 
  <servlet-mapping>
    <servlet-name>remoting</servlet-name>
    <url-pattern>/remoting/*</url-pattern>
  </servlet-mapping>
 
  <servlet>
    <servlet-name>restapi</servlet-name>
    <servlet-class>com.codegym.RestApiServlet</servlet-class>
    <load-on-startup>2</load-on-startup>
  </servlet>
 
  <servlet-mapping>
    <servlet-name>restapi</servlet-name>
    <url-pattern>/api/*</url-pattern>
  </servlet-mapping>  
 
</web-app>

Ing conto iki, rong servlet diumumake, lan saben dipetakan menyang cithakan url sing beda. Servlet RemotingServletnglayani kabeh panjalukan sing menyang/remoting/* . Servlet RestApiServletnglayani kabeh panjalukan sing menyang/api/* . Servlet uga duwe urutan saka loading - parameter load-on-startup.

7.3 Pilihan Servlet

Kanthi bantuan web.xml, paramèter bisa ditransfer menyang servlet sajrone initialization, bakal kasedhiya liwat antarmuka ServletConfig. Sampeyan uga bisa nyetel paramèter kanggo kabeh aplikasi web, bakal kasedhiya liwat ServletContext.

Tuladha web.xml:



<web-app>
  <context-param>
     <description>Server production mode</description>
     <param-name>productionMode</param-name>
     <param-value>false</param-value>
  </context-param>
 
  <context-param>
     <param-name>appPropertiesConfig</param-name>
     <param-value>
        classpath:local-app.properties
        classpath:web-app.properties
     </param-value>
  </context-param>
 
  <servlet>
     <servlet-name>mainservlet</servlet-name>
     <servlet-class>com.codegym.ApplicationServlet</servlet-class>
     <init-param>
        <param-name>application</param-name>
        <param-value>com.codegym.App</param-value>
     </init-param>
     <init-param>
        <param-name>widgetset</param-name>
        <param-value>com.codegym.WidgetSet</param-value>
     </init-param>
     <init-param>
        <param-name>ui</param-name>
        <param-value>com.codegym.AppUI</param-value>
     </init-param>
  </servlet>
</web-app>

Kode sing disorot ing ijo yaiku ing ngendi kita nyetel paramèter kanggoServletContext . Ana loro:

  • productionModekanthi nilai palsu
  • appPropertiesConfigkanthi susunan rong senar:
    • classpath:local-app.properties
    • classpath:web-app.properties

Parameter kanggo servlet dituduhake kanthi warna biruApplicationServlet , bakal kasedhiya liwat ServletConfig:

  • applicationkaro nilai com.codegym.App
  • widgetsetkaro nilai com.codegym.WidgetSet
  • uikaro nilai com.codegym.AppUI

7.4 saringan, pemetaan saringan

Aplikasi web uga bisa ngemot utility servlets - filters. Dheweke nindakake macem-macem tugas layanan: pangalihan telpon, mriksa wewenang, lsp.

Tuladha web.xml:



<web-app>
 
  <servlet>
      <servlet-name>remoting</servlet-name>
      <servlet-class>RemotingServlet</servlet-class>
      <load-on-startup>1</load-on-startup>
  </servlet>
 
  <servlet-mapping>
      <servlet-name>remoting </servlet-name>
      <url-pattern>/remoting/*</url-pattern>
  </servlet-mapping>
 
  <filter>
      <filter-name>total_filter</filter-name>
      <filter-class>com.javrush.TotalFilter</filter-class>
  </filter>
 
  <filter-mapping>
      <filter-name>total_filter</filter-name>
      <url-pattern>/*</url-pattern>
  </filter-mapping>
 
</web-app>

Sadurunge panyuwunan tekan servlet RemotingServlet, bakal diproses dening panyaring TotalFiler. Filter iki dikonfigurasi kanggo nyegat kabeh panjalukan sing menyang aplikasi web kita. Iki digambarake kanthi jelas dening cithakan url sing dipetakan menyang: /*.

Sampeyan bakal maca liyane babagan servlet lan saringan ing kuliah ing ngisor iki.