Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

J2EE - JSP-Servlet- Container - Components


Published on

Its an distributed enviornment for developing the enterprise application.We can develop multi-tier,three-tier or n-tier architecture using this.In this Java Server Pages and Servlet is the important things

Published in: Design

J2EE - JSP-Servlet- Container - Components

  1. 1. Kamalesh Sah
  2. 2. Distributed Multitiered Applications <ul><li>Application logic is divided into components according to function </li></ul><ul><li>The J2EE Application is made of components </li></ul><ul><ul><li>Client-tier components run on the client machine. </li></ul></ul><ul><ul><li>Web-tier components run on the J2EE server. </li></ul></ul><ul><ul><li>Business-tier components run on the J2EE server. </li></ul></ul><ul><ul><li>Enterprise information system (EIS)-tier software runs on the EIS server. </li></ul></ul>
  3. 3. Multitiered Applications
  4. 4. J2EE Components <ul><li>J2EE applications are made up of components. </li></ul><ul><li>The J2EE specification defines the following J2EE components: </li></ul><ul><ul><li>Application clients and Applets are components that run on the client. </li></ul></ul><ul><ul><li>Java Servlet and Java Server Pages ™ (JSP™) technology components are Web components that run on the server. </li></ul></ul><ul><ul><li>Enterprise JavaBeans ™ (EJB™) components (enterprise beans) are business components that run on the server. </li></ul></ul>
  5. 5. J2EE Clients
  6. 6. Web Client <ul><li>A Web client consists of two parts: </li></ul><ul><ul><li>Dynamic Page </li></ul></ul><ul><ul><ul><li>containing various types of markup language </li></ul></ul></ul><ul><ul><ul><li>page are generated by Web components running in the Web tier </li></ul></ul></ul><ul><ul><ul><li>which renders the pages received from the server. </li></ul></ul></ul><ul><ul><ul><li>Thin clients usually do not do things like query databases, execute complex business rules, or connect to legacy applications. </li></ul></ul></ul>Contd……..
  7. 7. Web Client <ul><li>Applet </li></ul><ul><ul><ul><li>An applet is a small client application </li></ul></ul></ul><ul><ul><ul><li>written in the Java programming language </li></ul></ul></ul><ul><ul><ul><li>executes in the Java virtual machine installed in the Web browser. </li></ul></ul></ul>
  8. 8. Application Clients <ul><li>Richer user interface </li></ul><ul><li>It typically has a graphical user interface (GUI) created from Swing or Abstract Window Toolkit (AWT) API </li></ul><ul><li>Application clients directly access enterprise beans running in the business tier. </li></ul>
  9. 9. JavaBeans™ Component Architecture <ul><li>Java-Beans component architecture to manage the data flow between an application client or applet and the J2EE server component </li></ul><ul><li>JavaBeans components have instance variables and get and set methods for accessing the data in the instance variables. </li></ul>
  10. 10. J2EE Server Communications
  11. 11. Web Tier and J2EE Applications
  12. 12. Web, Business and EIS Tiers
  13. 13. JSP
  14. 14. Point to Discuss <ul><li>JSP introduction </li></ul><ul><li>JSP elements </li></ul><ul><li>JSP development practices </li></ul>
  15. 15. JSP and Servlet <ul><li>Limitations of servlet </li></ul><ul><ul><li>It is difficult to write HTML </li></ul></ul><ul><ul><li>It’s ineffective to design webpages </li></ul></ul><ul><ul><li>It’s inaccessible to non-programmers </li></ul></ul><ul><li>JSP is a complement to servlet </li></ul><ul><ul><li>JSP focuses on user interface and presentation </li></ul></ul><ul><ul><li>JSP enhances the design capability of servlet </li></ul></ul><ul><ul><li>JSP pages can be written with any text editor, including HTML editor </li></ul></ul><ul><ul><li>JSP is a server side technology </li></ul></ul>
  16. 16. JSP Pages <ul><li>JSP page file ends with “.jsp” by default </li></ul><ul><li>JSP pages are organized like any other HTML files using the normal directory/file structure </li></ul><ul><li>A JSP page is usually composed of regular HTML tags and JSP scripting elements </li></ul><ul><li>JSP page is implicitly compiled to servlet class and loaded into memory </li></ul><ul><ul><li>when the page is requested the first time after creation, or </li></ul></ul><ul><ul><li>when the page is requested the first time after modification </li></ul></ul><ul><ul><li>Refer to table 10.1 in the textbook and the next slide </li></ul></ul>
  17. 17. JSP Compilation and Execution JSP Page Compile JSP Servlet Instance in Memory init() service() First request after creation or modification Subsequent Requests (can be from different users and sessions) JSP Servlet Automatic Compilation
  18. 18. Servlet and JSP Servlet JSP Development java classes (.java) scripting file (.jsp) Deployment Manually compiled; Specifically mapped Directly mapped: copy JSP files to intended directories Execution No need of source files Automatic compilation; automatic reloaded; source files (.jsp) are necessary
  19. 19. JSP Elements <ul><li>Scripting elements </li></ul><ul><ul><li>Scriptlet </li></ul></ul><ul><ul><ul><li>Regular Java code </li></ul></ul></ul><ul><ul><li>Expression </li></ul></ul><ul><ul><ul><li>Shortcut for output </li></ul></ul></ul><ul><ul><li>Declaration </li></ul></ul><ul><ul><ul><li>Declaring variables and methods at the class level </li></ul></ul></ul><ul><li>Directive </li></ul><ul><li>JSP action </li></ul><ul><li>Comments (<%-- … --%>) </li></ul>
  20. 20. Expression <ul><li>A shortcut to print out a value or an expression </li></ul><ul><ul><li><%= [expression]%> </li></ul></ul><ul><ul><li>Expression can be a variable, formula, object property, string concatenation, method with return value, or anything that returns a value </li></ul></ul>
  21. 21. JSP Output Practices <ul><li>Ways to treat static HTML content </li></ul><ul><ul><li>Regular/block output (servlet way) </li></ul></ul><ul><ul><ul><li>Uses “out.println()” or “out.print()” method to generate all content, including static content </li></ul></ul></ul><ul><ul><li>“ Spaghetti”/mixed output (scripting way) </li></ul></ul><ul><ul><ul><li>Uses JSP scriptlets or expressions for dynamic content only </li></ul></ul></ul><ul><ul><ul><li>Mixes scripting elements and static content </li></ul></ul></ul>
  22. 22. Regular Output <ul><li>Using “out.print()” or “out.println()” method to generate HTML as a block, even the whole page – Servlet way </li></ul><ul><li>StringBuffer is often used to construct HTML content first, and then printed out at one time </li></ul>
  23. 23. Spaghetti Output <ul><li>Expression elements are often used where dynamic content is needed </li></ul><ul><li>Use regular HTML for static content; don’t include them in JSP scripting elements </li></ul><ul><li>How mixed should it be? </li></ul><ul><ul><li>Depends on your own style </li></ul></ul><ul><ul><li>Coding should be most convenient and clear </li></ul></ul><ul><ul><li>Depends on development requirement </li></ul></ul>
  24. 24. Declarations <ul><li>Declaration element is used to define member variables and methods </li></ul><ul><ul><li><%! … %> </li></ul></ul><ul><ul><li>Variables not defined in declaration element are local / method level variables </li></ul></ul><ul><ul><li>Methods can only be defined in the declaration element </li></ul></ul><ul><li>Like regular class variables and methods, the location where you define these variables and methods is not important </li></ul>
  25. 25. JSP Page Directive <ul><li>Directives affects the overall structure of the servlet generated </li></ul><ul><ul><li><%@ … %> </li></ul></ul><ul><li>Use page directive to import classes </li></ul><ul><ul><li><%@ page import=“…, …, …”%> </li></ul></ul><ul><ul><li>This is equivalent to the “import” statement in regular Java classes </li></ul></ul>
  26. 26. JSP Include Directive <ul><li>How to reuse code? </li></ul><ul><li>Use include directive to include source code from another file </li></ul><ul><ul><li><%@ include file=“…” %> </li></ul></ul><ul><ul><li>Inclusion happens at the compilation time </li></ul></ul><ul><ul><li>What is included is the source, not generated result </li></ul></ul><ul><ul><li>Often used to include method definitions </li></ul></ul>
  27. 27. Redirection, Refreshing and Forwarding <ul><li>Redirection </li></ul><ul><ul><li>response.sendRedirect() </li></ul></ul><ul><li>Refreshing </li></ul><ul><ul><li>response.setHeader(“Refresh”, “10; url=…”) </li></ul></ul><ul><li>Forwarding <jsp:forward page=“…” /> </li></ul><ul><ul><li>The “page” attribute follows the same rule as that of <jsp:include/> </li></ul></ul><ul><ul><li>Forwarding does not invoke network traffic </li></ul></ul><ul><ul><li>The destination URL is hidden; original requested URL does not change in browser address bar after forwarding </li></ul></ul><ul><li>Compare redirecting and forwarding </li></ul>
  28. 28. Request Processing <ul><li>Using implicit object “request” </li></ul><ul><li>Processing HTTP request headers </li></ul><ul><ul><li>The same way as servlet </li></ul></ul><ul><li>Reading URL parameter </li></ul><ul><ul><li>http://localhost/appcontext/request.jsp?choice=yes </li></ul></ul><ul><ul><li>Parameter processing is the same way as servlet, using request.getParameter(“…”), request.getPameterValues(“…”) </li></ul></ul>
  29. 29. Form Processing with JSP <ul><li>The same way as servlet </li></ul><ul><ul><li>request.getParameter(“…”) </li></ul></ul><ul><ul><li>Note: the action attribute of the form should be a JSP file that processes data </li></ul></ul><ul><ul><ul><li><form method=“post” action=“studentprofile.jsp”>…</form> </li></ul></ul></ul>
  30. 30. Servlet
  31. 31. Java on the Web: J2EE <ul><li>Thin clients (minimize download) </li></ul><ul><li>Java all “server side” </li></ul><ul><li>THIS IS WHAT YOU’LL BE DOING!! </li></ul>Client Server Servlets
  32. 32. Where are Servlets? HTTP Web Server File system Servlet Server Static Dynamic
  33. 33. CGI vs. Servlet <ul><li>CGI - new process for each request!!! </li></ul><ul><li>Servlet - Servlet loaded only once </li></ul>
  34. 34. How are Servlets? <ul><li>import*; </li></ul><ul><li>import javax.servlet. *; </li></ul><ul><li>import javax.servlet.http. *; </li></ul><ul><li>public class Hellox extends HttpServlet { </li></ul><ul><li>public void doGet ( HttpServletRequest request, </li></ul><ul><li>HttpServletResponse response) </li></ul><ul><li>throws IOException, ServletException </li></ul><ul><li>{ </li></ul><ul><li>response. setContentType(&quot;text/html&quot;) ; </li></ul><ul><li>PrintWriter out = response. getWriter (); </li></ul><ul><li>out.println(&quot;<html>&quot;); </li></ul><ul><li>// out.println(&quot;<body>&quot;); </li></ul><ul><li>out.println(&quot;<head>&quot;); </li></ul><ul><li>out.println(&quot;<title>Hello World!</title>&quot;); </li></ul><ul><li>out.println(&quot;</head>&quot;); </li></ul><ul><li>out.println(&quot;<body>&quot;); </li></ul><ul><li>out.println(&quot;<h1>Hello World!</h1>&quot;); </li></ul><ul><li>out.println(&quot;</body>&quot;); </li></ul><ul><li>out.println(&quot;</html>&quot;); </li></ul><ul><li>} // doGet </li></ul><ul><li>} // Hellox </li></ul>
  35. 35. Explanation <ul><li>A class should extend  HttpServlet  and override doGet or doPost (or both), depending on whether the data is being sent by GET or by POST.  </li></ul><ul><li>The HttpServletRequest has methods that let you find out about incoming information such as FORM data, HTTP request headers, and the like </li></ul>
  36. 36. Explanation <ul><li>TheHttpServletResponse has methods that lets you specify the HTTP response line, response headers most importantly obtain a method from PrintWriter  used to send output back to the client.  </li></ul><ul><li>You have to import classes in ,javax.servlet ,and javax.servlet.http </li></ul>
  37. 37. Directory Structure
  38. 38. Methods of Servlets <ul><li>init() </li></ul><ul><li>service() </li></ul><ul><li>getServletConfig() </li></ul><ul><li>getServletInfo() </li></ul><ul><li>destroy() </li></ul>
  39. 39. Init() <ul><li>The  init() method  is called only once by the servlet container throughout the life of a servlet. </li></ul>
  40. 40. Service() <ul><li>Once the servlet starts getting the requests, the service() method is called by the servlet container to respond.  </li></ul>
  41. 41. Destroy() <ul><li>That is before removing a servlet instance from service, the servlet container calls the destroy() method. Once the servlet container calls the destroy() method, no service methods will be then called . </li></ul>
  42. 42. Life cycle of Servlet <ul><li>Loading and Inatantiation: </li></ul><ul><li>Initialization </li></ul><ul><li>Servicing the Request </li></ul><ul><li>Destroying the Servlet </li></ul>
  43. 43. Loading and Inatantiation <ul><li>The servlet container loads the servlet during startup or when the first request is made. The loading of the servlet depends on the attribute <load-on-startup> of web.xml file. If the attribute <load-on-startup> has a positive value then the servlet is load with loading of the container otherwise it load when the first request comes for service. After loading of the servlet, the container creates the instances of the servlet. </li></ul>
  44. 44. Initialization <ul><li>After creating the instances, the servlet container calls the init() method and passes the servlet initialization parameters to the init() method. The init() must be called by the servlet container before the servlet can service any request. The initialization parameters persist untill the servlet is destroyed. The init() method is called only once throughout the life cycle of the servlet. </li></ul><ul><li>The servlet will be available for service if it is loaded successfully otherwise the servlet container unloads the servlet. </li></ul>
  45. 45. Servicing the Request <ul><li>After successfully completing the initialization process, the servlet will be available for service. Servlet creates seperate threads for each request. The sevlet container calls the service() method for servicing any request. The service() method determines the kind of request and calls the appropriate method (doGet() or doPost()) for handling the request and sends response to the client using the methods of the response object. </li></ul>
  46. 46. Destroying the Servlet <ul><li>If the servlet is no longer needed for servicing any request, the servlet container calls the destroy() method . Like the init() method this method is also called only once throughout the life cycle of the servlet. Calling the destroy() method indicates to the servlet container not to sent the any request for service and the servlet  releases all the resources associated with it. Java Virtual Machine claims for the memory associated with the resources for garbage collection. </li></ul>
  47. 47. Life Cycle of a Servlet