1 / 42

Lecture 8

CS441 CURRENT TOPICS IN PROGRAMMING LANGUAGES. Lecture 8. George Koutsogiannakis/Summer 2011. Topics. Web Applications and the Java Server. HTTP protocol. Servlets. Web Applications. Two Types of Web Applications:

conway
Download Presentation

Lecture 8

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. CS441 CURRENT TOPICS IN PROGRAMMING LANGUAGES Lecture 8 George Koutsogiannakis/Summer 2011

  2. Topics • Web Applications and the Java Server. • HTTP protocol. • Servlets

  3. Web Applications • Two Types of Web Applications: • Presentation oriented: Interactive web applications with dynamic content usually provided with DHTML, XML, servlets or java server pages. • Service oriented: Web applications that utilize presentation oriented applications as clients. They consist of web services, SOAP protocol services, RMI etc.

  4. Web Applications • For example an Aplication server or an RMI server that processes Queries to the databse server. It receives the results of the query formats it and passes it over to the presentation oriented application so that it can be sent to the requestor (client of the presentation application)

  5. Web Applications • Web Components: Servlets, JSP (Java Server Pages). • Web Components are supported by a web container. • For instance: Tomcat has a servlet engine and a JSP engine. Those engines are the web containers.

  6. Web Applications • Web Containers provide to web components: • Activation • Initialization • Configuration • Translation from http to a servlet request object and • Translation from a servlet response object to http.

  7. Web Applications • Web Components can communicate directly with a database (via JDBC) or • They can communicate with a database via Java Beans components or • They can communicate with other service oriented applications such as a RMI server, or web services, or an application server.

  8. Web Applications HTTPServletRequest Object Database HTTP Request Containers WebComponents Client HTTPServletResponse Object HTTP Response Java Bean Components RMI Server Web Services Database

  9. Web Applications • Life Cycle of a Web Application: • Develop web component(s) code (i.e servlet code or jsp code). • Develop the web application deployment descriptor (i.e. the web.xml file in Tomcat). • Compile the web components code. • Package the application into a deployable unit (i.e. a WAR file). • Deploy the application into the container. • Undeploy the application if needed. • Access a URL from a client that references the web application.

  10. Web Applications • Web Resources: • Web Components. • Static web content files. • Web Module: • The smallest deployable web resource. • Same as a web application. • Contains: web resources, java beans, applets other types of files.

  11. Web Applications • A web module can be packaged into a WAR file • WAR files are made with the jar utility and they have a .war extension. • To deploy a WAR file on an Application Server, the file must have a deployment descriptor (i.e. web.xml for Tomcat or sun-web.xml for Java Server). • The deployment descriptor file is placed in the folder: CONTEXT ROOT (ASSEMBLY ROOT) WEB-INF web.xml

  12. Web Applications • Packaging a Web Application: • Use a WAR file or • Use an IDE like NetBeans (possibly a separate download from EE 5) or • Use a utility tool called ants (part of EE 5 download).

  13. Web Applications • Deployment on an Application server: • WAR file can be placed into the <JavaEE_Home>/domains/domain1/autodeploy directory of Java Server (Java Application Server). • Or by using the Administrative Console of Java Server. • Or by running asadmin or ant command line to deploy the WAR file. • Or by using NetBeans. • Note: Deploying details are described in your “Java EE 6 Tutorial” text

  14. HTTP Protocol • Request/ Response packets. • Client initiates a request via a TCP connection to a port (8080 or 80). • Server sends a response packet to client • Protocol provides methods such as: • GET • POST • PUT • DELETE • HEAD • And others

  15. HTTP Protocol • If GET is used by client then: • Data can be part of the URL http://www.cs.iit.edu/index.html?firtsname=“fm”&lastname=“ln” • Data is not cached by the browser application. • Data is URL encoded i.e. spaces are converted to + signs, nonalphanumeric characters are converted to % signs followed by the two hex digits representing the character etc. • Use it when the data is small.

  16. HTTP protocol • If client uses POST: • The data is part of the body of the HTTP packet. • Data is cached. • Use it if data is larger and the interaction with the server changes the state of the resource in the server.

  17. HTTP Protocol • Error messages can be returned by the server in the response packet. • Error messages are coded: • 400 means bad request. • 401 means unauthorized request. • 500 Internal server error. Etc.

  18. Servlets • A servlet is a java program that executes on the server side. • It extends the capabilities of a server. • Two types of servlets: • Generic type • HTTP type Note: nevertheless other protocols can be supported such as FTP, POP, SMTP • We will concentrate on HTTP type.

  19. Servlets • HTTP type servlets • Extend the web server by executing requests received in HTTP (from a client) protocol packets and by returning responses that are translated back to HTTP protocol packets.

  20. Servlets • Clients to Servlets • Client side programs that sent requests can be: • Html files (forms). • Applets. • JavaScript. • XML. • Java application programs. • Active Server pages (ASP) programs. • Other.

  21. Servlets • Execution environment: • Web Server needs the servlet engine (container). • The interface between the servlet engine and the servlet program is defined by the Servlet API.

  22. Servlets • The servlet container is responsible for: • Creating an instance of the servlet program. • Calling the servlet’ s init() method to initialize the servlet. • Configures the servlet according to preset properties. • Calling the servlet’ s service method. • Creating the stream to send to the servlet a request object that encapsulates an y data the client sent as part of the request. • Creating the stream to receive the response object from the servlet that encapsulates the data generated by the servlet as a result of the request. • Can handle multiple service calls that be made simultineously (multiple requests arriving at the same time). • Destroys the servlet by calling destroy() method.

  23. Servlets • Servlet applications provide: • Processing of a Get or Post type of a request via individual methods that need to be overridden. • The servlet engine reads the request type off the arriving HTTP packets, calls the service method of the servlet, the service method will call either a doGet or doPost servlet method. • Handle many requests at the same time. • Provide session capability with a client so that multiple request/response pairs can be serviced without loosingtrack of the particular client. • Can communicate with other servlets forming a chain.

  24. Servlets • Can generate a response in Html tags. The servelt engine will extract the html code from the java code and form send it to the client. • This facilitates the generation of dynamically formed responses based on the request. • Can execute JDBC code to connect to a database and retrieve information. The response object will then encapsulate the data. • Can act as clients to a Remote Method Invocation Servers in a distributed objects network. • Can communicate with Java Beans.

  25. Servlets • Servlet API • javax.servlet • javax.servlet.http Servlet interface Implemented by abstract class Implemented by abstract class GenericServlet HttpServlet

  26. Servlets • The abstract classes provide default implementation for methods: • void init(ServletConfig config) • ServletConfig getServletConfig() • void service(ServletRequest requ, ServletResponse resp) • String getServletInfo() • void destroy() • In addition, the HttpServlet class has methods: • doGet (HTTPServletRequest requ, HttpServletResponse resp) • doPost (HTTPServletRequest requ, HttpServletResponse resp) These methods have to be overridden by the servlet class that the developer provides.

  27. Servlets ServletResponse interface ServletRequest interface HttpServletResponse interface HttpServletRequest interface Passes response object from doGet or doPost method to service method Service method passes response object to servlet engine Passes request object to service method Service method passes request object to servlet ‘s doGet Or doPost method.

  28. Servlets • Several Listener interfaces are there to provide listeners for various event handlers that: • Access information when an event occurs. • Must implement one of the interfaces: • ServletContextListener: initialization and destruction information • ServletContextAttributesListener: web context attribute added, removed, replaced. • HttpSessionListener: information on creation, invalidation of a session, activation of a session, timeout of a session. • HttpSessionAttributesListener: session attribute has been added, removed or replaced • HttpRequestListener and HttpRequestAttributeListener: information on attributes associated with a request.

  29. Servlets • Example: • Client receives an HTML form. • The form in its action tag calls a servlet. • The servlet creates html code and sends back to the client for display by the browser.

  30. Servlets Example-File helloworld,html (simplified) <html> <head> <title>Untitled Document</title> </head> <body bgcolor="#FFFFFF"> <p><a href="servlet/HelloWorldExample"></p> </body> </head> </html>

  31. Servlets Example – file HelloWorld.java import java.io.*; import javax.servlet.*; import javax.servlet.http.*; public class HelloWorld extends HttpServlet { public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException { response.setContentType("text/html"); PrintWriter out = response.getWriter(); out.println("<html>"); out.println("<head>"); out.println("<title>Hello World!</title>"); out.println("</head>"); out.println("<body>"); out.println("<h1>Hello World!</h1>"); out.println("</body>"); out.println("</html>"); } }

  32. Servlets Example – file HelloWorld.java • The Browser receives the html code: <html> <head> <title>Hello World!</title> </head> <body> <h1>Hello World!</h1> </body> </html>

  33. Servlets-Another example <html> <hesd> <body> <Form action=http://localhost:8080/ExampleSite/TechSupport” method=“POST”> <table><tr><td> <Input types=“Text” Name=“firstName”></input></td></tr></table> </form> </body> </head> </html>

  34. Servlets-Another example TechSupport.java file import javax.servlet.*; import javax.servlet.http.* import java.io.*; import java.util.*; public class TechSupport extends HttpServlet { protected void doPost(HttpServletRequest req, HttpServletResponse res) throws ServletException { res.setContextType(“text/html”); String myparam=req.getParameter(“firstName”); PrintWriter out = res.getWriter(); out.println(“<HTML>”); out.println(“<HEAD>”); out.println(“<BODY>”);

  35. Servlets-Another example if(myparam.equals(“MyName”)) { out.println(“<p”>); out.println(“You can now login”); out.println(“</p>”); } else { out.println(“<p”>); out.println(“I don’t recognize you”); out.println(“</p>”); } out.println(“</BODY>”); out.println(“</HEAD>”); out.println(“<HTML>); out.close(); }

  36. Servlets-Another example • The web.xml deployment descriptor file has to be created for the servlet: <web-app> <display-name>Servlet Example</display-name> <description>Simple servlet responding to a form</description> <servlet> <servlet-name>TechSupport</servlet-name> <servlet-class>TechSupport</servlet-class> </servlet> <servlet-mapping> <servlet-name>TechSupport</servlet-name> <url-pattern>/TechSupport</url-pattern> </servlet-mapping> </web-app> //where TechSupport.class is the servlet bytecodes file residing in the classes folder of Tomcat.

  37. Deployment on Tomcat Web Server • To deploy the web examples discussed you can: • Install Tomcat by itself (do not install NetBeans at this time) • Create a war file and deploy the war file in Tomcat. • Or, install NetBeans. Add Tomcat and GlassFish v2 as active servers in NetBeans. • Start Tomcat from NetBeans.

  38. Working with servlets outside of EE • The servlet API is only available on EE. Ther you would find the latest version of servlet API. • I have,however,retained an independent version of servlet API that can be installed and become part of your standard edition. • It is an old API but it would allow us to work with servlets at this level in an easier way.

  39. Enabling SE with the servlet API • Download from the examples page of the course the file : SERVLET.ZIP • Unzip the file and you will find two jar files inside. • Copy the two jar files and paste them in the path of your jdk installation C:\>Program Files\Java\jdk1.6.0_18\jre\lib\ext\ • Now you can compile source code files that include servlet (and java server pages ) libraries. • Note that this an old API that I copied few years back. This is not available anymore and you can’t compile servelts normally with the standard edition (You need Enterprise Edition).

  40. Deployment in Tomcat • The servelt compiled code should reside in the WE_INF /classes folder of the Tomcat Root Context structure. ROOT_CONTEXT .html, applet.class files WEB_INFO web.xml CLASSES myservlet.class

  41. Study Guide • SUN ‘s acquisition by Oracle has been finalized and the web sites for Java Tutorials are beginning to change since February 1st. • The Java EE 6 Tutorial has been changed and the organization of the material has changed the last two weeks (after our course started)

  42. Study Guide • WEB APPLICATIONS WITH SERVLETS WBAD CH8 • Servelts are now covered in Part II chapter 10 of “The Java EE 6 Tutorial” text. This change is effective February 2010. See servlet examples on the course ‘s web site.

More Related