1 / 18

Servlet

Servlet. Mini tutorial su creazione e gestione di HttpServlet in Eclipse. Creare un file Index.html in WebContent. <html> <head> <title>Test Servlet</title> </head> <body> <center> <h3> Inserisci un testo</h3> <form action= "TestServlet">

tejano
Télécharger la présentation

Servlet

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Servlet Mini tutorial su creazione e gestione di HttpServlet in Eclipse

  2. Creare un file Index.html in WebContent <html> <head> <title>Test Servlet</title> </head> <body> <center> <h3>Inserisci un testo</h3> <form action="TestServlet"> <input type="text" id="testo" name="testo"><br> <input type="submit" value="INVIA!"> </form> </center> </body> </html>

  3. Deploy and Run

  4. Creare una servlet

  5. Creare una servlet (2)

  6. Creare una Servlet (3)

  7. Creare una Servlet 1) Cambiare il contenuto del metodo get protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.getWriter().println("TEST SERVLET !"); } 2) Deploy sul container. 3) Accedere all’url http://localhost:8080/EsercitazioneEJB/TestServlet

  8. Struttura di una Web Application • La struttura della Web Application e’ contenuta all’interno della cartella WebContent • WAR (Web Archive) WEB-INF/lib /classes/it/unige/dist/servlet/TestServlet.class /web.xml Index.html

  9. web.xml <?xml version="1.0" encoding="UTF-8"?> <web-app …> <display-name>EsercitazioneEJB</display- name> <welcome-file-list> <welcome-file>index.html</welcome-file> <welcome-file>index.htm</welcome-file> <welcome-file>index.jsp</welcome-file> <welcome-file>default.html</welcome-file> <welcome-file>default.htm</welcome-file> <welcome-file>default.jsp</welcome-file> </welcome-file-list> <servlet> <description></description> <display-name>TestServlet</display-name> <servlet-name>TestServlet</servlet-name> <servlet-class>it.unige.dist.servlet.TestServlet</servlet-class> </servlet> <servlet-mapping> <servlet-name>TestServlet</servlet-name> <url-pattern>/TestServlet</url-pattern> </servlet-mapping> </web-app>

  10. Invocazione di una Servlet

  11. Servlet Lifecycle • If an instance of the servlet does not exist, the Web container • Carica la classe della servlet. • Crea un’instanza della classe della servlet. • Inizializza la servlet chiamando il metodo init. • Invoca il metodo service() passando l’oggetto Request e Response. Il metodo service delega ai metodi doGet, doPost.

  12. ServletContext • Il Servlet Context e’ un contesto condiviso tra tutte le servlet che fanno parte della stessa web application. • Le servlet possono comunicare all’interno della stessa web application utilizzando attributi savati nel Servlet Context. • Per accedere al Servlet Context all’interno di una servlet si deve invocare this.getServletContext WebApplicationContext getServletContext getServletContext TestServlet ProvaServlet

  13. Context and Session Listeners

  14. Listener nelle Servlet

  15. HttpSession • Una HttpSession e’ una sessione stabilita’ tra la server ed un browser. • Client diversi che inviano richieste alla stessa servlet avranno HttpSession diverse. • Per ottenere la HttpSession si deve invocare HttpRequest.getSession

  16. Esercizio • Inserire un attributo “prova” con un valore intero nel ServletContext usando this.getServletContext() nella servlet TestServlet. • Creare una nuova Servlet: ProvaServlet • La servlet gestisce solo richieste GET. • La servlet deve leggere un dato salvato dalla servlet TestServlet. • La servlet deve leggere un dato salvato dal ContextListener • Salvare un contatore all’interno di ProvaServlet che deve essere diverso per ogni client che si collega. • Il dato deve essere inizializzato dal SessionListener.

More Related