session 9
play

Session 9 Introduction to Servlets Lecture Objectives Understand - PDF document

Internet Programming Session 9 Servlet Intro Session 9 Introduction to Servlets Lecture Objectives Understand the foundations for client/server Web interactions Understand the servlet life cycle 2 Robert Kelly, 2018 1


  1. Internet Programming Session 9 – Servlet Intro Session 9 Introduction to Servlets Lecture Objectives � Understand the foundations for client/server Web interactions � Understand the servlet life cycle 2 � Robert Kelly, 2018 1 10/11/2018 � Robert Kelly, 2018

  2. Internet Programming Session 9 – Servlet Intro Reading & Reference � Reading Use the J2EE 7 Tutorial � Java Servlet as a Reference en.wikipedia.org/wiki/Java_servlet � Java Annotation en.wikipedia.org/wiki/Java_annotation docs.oracle.com/javase/tutorial/java/annotations/ � Excellent tutorial, explaining how to set up a servlet in NetBeans www.studytonight.com/servlet/creating-servlet-in-netbeans.php � Reference � Use the on-line Servlet API documentation at: http://docs.oracle.com/javaee/7/api/ 3 � Robert Kelly, 2018 Java Servlet Releases � Servlet 3.0 – 2009 � Servlet 3.1 – May 2013 (consistent with Java EE 7) � Servlet 4.0 – September 2017 (consistent with Java EE 8 and HTTP/2) Java EE now maintained by the Eclipse foundation, and renamed as Jakarta EE 4 � Robert Kelly, 2018 2 10/11/2018 � Robert Kelly, 2018

  3. Internet Programming Session 9 – Servlet Intro Typical Client/Server Interaction Web Server URL / Query string / HTTP Browser req data, cookies, etc. Web container Web Static Component HTML, CSS, Container HTTP interface MIME Type is also referred HTML, Images, cookies, Display res etc. to as a servlet container JDBC � WWW resources, each named with a URL � Standard content formats (e.g., HTML) Database � Standard network protocols connecting any browser with any server 5 � Robert Kelly, 2018 Server Strategies � Generation of HTML/CSS � Server responds with a dynamically generated page that includes HTML, CSS, and data (inserted in the page) � Data insertion usually performed by a server-side scripting engine � Web services � Server responds with data (no HTML and CSS) � Data structured based on some coordination between client and server (e.g., JSON, XML, text) 6 � Robert Kelly, 2018 3 10/11/2018 � Robert Kelly, 2018

  4. Internet Programming Session 9 – Servlet Intro Servlets � Conforms to the Java Servlet API � Superseded by JAX-RS (Java API for RESTful Web Services) API � A servlet: � Is a Java class that can be loaded dynamically to expand the capability of the Web server � Runs inside the Java Virtual Machine on the server (safe and portable) � Is able to access all Java APIs supported in the server � Does not have a main method 7 � Robert Kelly, 2018 Servlet Implementation � Web Server (Web Container) Market Share 1 of active sites Developer March 2018 Percent Apache 76M 43.0% nginx 37M 21.0% Google 12M 7.7% Microsoft 12M 6.8% 1. Data from NetCraft 8 � Robert Kelly, 2018 4 10/11/2018 � Robert Kelly, 2018

  5. Internet Programming Session 9 – Servlet Intro Servlet / Web Server Interface HttpServlet subclass Service methods (doGet and doPost) http method request Static Interface Web Container MIME files response The Web Server static interface Frequently both servers are primarily serves resources, and packaged together, but it is good passes complex request to the to think of them as separate Web Container systems 9 � Robert Kelly, 2018 Servlet Interface � Objects are used to pass information to the server and to return information from the server Request data include HTTP version, URL, browser software, client MIME � Service methods: type preferences, data, etc. protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, java.io.IOException protected void doPost( HttpServletRequest req, HttpServletResponse resp) throws ServletException, java.io.IOException Response data includes HTTP version, status code, MIME type of data, document size, document 10 � Robert Kelly, 2018 5 10/11/2018 � Robert Kelly, 2018

  6. Internet Programming Session 9 – Servlet Intro Server Generation of an HTML page � The following HTML can be returned to the browser directly by the Web server (static file on the Web Server) – or the same html page can be generated (on the fly) by the Web Container <html> <head> <title>Hello World</title> </head> <body> <p>Hello World</p> </body></html> 11 � Robert Kelly, 2018 Invoking the Hello World Servlet Port 8080 is typical for a The URL in this test http Name of the link maps to the server Web servlet application <!doctype html> <html> <head> <title>Link to Hello World Servlet</title> </head> <body> <p> <a href="http://localhost:8080/CSE336-2017/HelloWorld"> Click here to say hello to the world</a> </p> </body> </html> This is not really a file – it Verify the port number used by your test server 12 maps to your servlet � Robert Kelly, 2018 6 10/11/2018 � Robert Kelly, 2018

  7. Internet Programming Session 9 – Servlet Intro Hello World Servlet Method protected void processRequest( HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setContentType( "text/html;charset=UTF-8"); PrintWriter out = response.getWriter(); String docTyp= "<!DOCTYPE html >"; out.println(docType); out.println("<html>"); out.println("<head><title>" + "Hello World</title></head>"); out.println("<meta charset=\"UTF-8\">"); out.println("<body>"); out.println("<h1>Hello World</h1>"); out.println("</body></html>"); out.close(); } 13 � Robert Kelly, 2018 HelloWorld Servlet Class package lectures; import java.io.*;import java.net.*;import javax.servlet.*; import javax.servlet.http.*; processRequest is a method used by convention in NetBeans public class HelloWorld extends HttpServlet { protected void processRequest(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { } protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { processRequest(request, response); } protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { The Web container calls either doGet or processRequest(request, response); } doPost, which then calls processRequest 14 � Robert Kelly, 2018 7 10/11/2018 � Robert Kelly, 2018

  8. Internet Programming Session 9 – Servlet Intro Web Application � Your Web application contains all the servlets, JSPs, files, etc. associated with your application. � Your Web application is stored in a directory (and deployed as a war file) � Top level directory of the Web application is the document root of the application, containing JSP pages and static Web resources (or subdirectories of JSP, etc.) � Document root contains a sub-directory called WEB-INF, containing � web.xml – the deployment descriptor Take a look at your Web App in your � Sub-directories containing classes NetBeans or Eclipse project pane � Meta data for the Web Application 15 � Robert Kelly, 2018 How to Specify the Servlet in Your HTML � A URL is used to request that the container run your servlet (in an anchor tag or form tag) � URL contains the host name, port (optional), and path � In a servlet container, the path can be mapped (what you see is not always what you get) http://localhost:8080/CSE336-2017/HelloWorld There is no HelloWorld resource 16 � Robert Kelly, 2018 8 10/11/2018 � Robert Kelly, 2018

  9. Internet Programming Session 9 – Servlet Intro How URLs Run Servlets http://localhost:8080/CodeCSE336/helloWorld Context name (Web Application or Project name) � The servlet container evaluates the URL request to see if the first part of the path matches a context name � If the path matches a context name, the context name is mapped to a Web application root directory (using the web.xml deployment descriptor) 17 � Robert Kelly, 2018 Mapping Requests to Servlets � Two approaches � Web.xml (Deployment Descriptor) � Annotation 18 � Robert Kelly, 2018 9 10/11/2018 � Robert Kelly, 2018

  10. Internet Programming Session 9 – Servlet Intro Annotation Recap … � Part of Java language, starting with Java 5 � Annotations are tags that you insert into source code so that some tool can process it (not part of the normal execution of the program) � Proper style places the annotation on a line preceding the statement it relates to @Entity public class Team implements Serializable { You can annotate classes, Think of it as a modifier for methods, fields, and local the declaration variables 19 � Robert Kelly, 2018 … Annotation recap � Annotations can be defined to have elements @ManyToOne(cascade=CascadeType.PERSIST) public Team getTeam() { … } � Examples � Unit testing (JUnit) � Java Persistence API (JPA) � Servlets � Java Beans 20 � Robert Kelly, 2018 10 10/11/2018 � Robert Kelly, 2018

Download Presentation
Download Policy: The content available on the website is offered to you 'AS IS' for your personal information and use only. It cannot be commercialized, licensed, or distributed on other websites without prior consent from the author. To download a presentation, simply click this link. If you encounter any difficulties during the download process, it's possible that the publisher has removed the file from their server.

Recommend


More recommend