J2EE - JSP-Servlet- Container - Components
Upcoming SlideShare
Loading in...5
×
 

J2EE - JSP-Servlet- Container - Components

on

  • 4,907 views

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 ...

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

Statistics

Views

Total Views
4,907
Views on SlideShare
4,906
Embed Views
1

Actions

Likes
5
Downloads
269
Comments
0

1 Embed 1

http://twitter.com 1

Accessibility

Categories

Upload Details

Uploaded via as Microsoft PowerPoint

Usage Rights

© All Rights Reserved

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Processing…
Post Comment
Edit your comment

J2EE - JSP-Servlet- Container - Components J2EE - JSP-Servlet- Container - Components Presentation Transcript

  • Kamalesh Sah http://www.youtube.com/kamleshutube
  • Distributed Multitiered Applications
    • Application logic is divided into components according to function
    • The J2EE Application is made of components
      • Client-tier components run on the client machine.
      • Web-tier components run on the J2EE server.
      • Business-tier components run on the J2EE server.
      • Enterprise information system (EIS)-tier software runs on the EIS server.
  • Multitiered Applications
  • J2EE Components
    • J2EE applications are made up of components.
    • The J2EE specification defines the following J2EE components:
      • Application clients and Applets are components that run on the client.
      • Java Servlet and Java Server Pages ™ (JSP™) technology components are Web components that run on the server.
      • Enterprise JavaBeans ™ (EJB™) components (enterprise beans) are business components that run on the server.
  • J2EE Clients
  • Web Client
    • A Web client consists of two parts:
      • Dynamic Page
        • containing various types of markup language
        • page are generated by Web components running in the Web tier
        • which renders the pages received from the server.
        • Thin clients usually do not do things like query databases, execute complex business rules, or connect to legacy applications.
    Contd……..
  • Web Client
    • Applet
        • An applet is a small client application
        • written in the Java programming language
        • executes in the Java virtual machine installed in the Web browser.
  • Application Clients
    • Richer user interface
    • It typically has a graphical user interface (GUI) created from Swing or Abstract Window Toolkit (AWT) API
    • Application clients directly access enterprise beans running in the business tier.
  • JavaBeans™ Component Architecture
    • Java-Beans component architecture to manage the data flow between an application client or applet and the J2EE server component
    • JavaBeans components have instance variables and get and set methods for accessing the data in the instance variables.
  • J2EE Server Communications
  • Web Tier and J2EE Applications
  • Web, Business and EIS Tiers
  • JSP
  • Point to Discuss
    • JSP introduction
    • JSP elements
    • JSP development practices
  • JSP and Servlet
    • Limitations of servlet
      • It is difficult to write HTML
      • It’s ineffective to design webpages
      • It’s inaccessible to non-programmers
    • JSP is a complement to servlet
      • JSP focuses on user interface and presentation
      • JSP enhances the design capability of servlet
      • JSP pages can be written with any text editor, including HTML editor
      • JSP is a server side technology
  • JSP Pages
    • JSP page file ends with “.jsp” by default
    • JSP pages are organized like any other HTML files using the normal directory/file structure
    • A JSP page is usually composed of regular HTML tags and JSP scripting elements
    • JSP page is implicitly compiled to servlet class and loaded into memory
      • when the page is requested the first time after creation, or
      • when the page is requested the first time after modification
      • Refer to table 10.1 in the textbook and the next slide
  • 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
  • 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
  • JSP Elements
    • Scripting elements
      • Scriptlet
        • Regular Java code
      • Expression
        • Shortcut for output
      • Declaration
        • Declaring variables and methods at the class level
    • Directive
    • JSP action
    • Comments (<%-- … --%>)
  • Expression
    • A shortcut to print out a value or an expression
      • <%= [expression]%>
      • Expression can be a variable, formula, object property, string concatenation, method with return value, or anything that returns a value
  • JSP Output Practices
    • Ways to treat static HTML content
      • Regular/block output (servlet way)
        • Uses “out.println()” or “out.print()” method to generate all content, including static content
      • “ Spaghetti”/mixed output (scripting way)
        • Uses JSP scriptlets or expressions for dynamic content only
        • Mixes scripting elements and static content
  • Regular Output
    • Using “out.print()” or “out.println()” method to generate HTML as a block, even the whole page – Servlet way
    • StringBuffer is often used to construct HTML content first, and then printed out at one time
  • Spaghetti Output
    • Expression elements are often used where dynamic content is needed
    • Use regular HTML for static content; don’t include them in JSP scripting elements
    • How mixed should it be?
      • Depends on your own style
      • Coding should be most convenient and clear
      • Depends on development requirement
  • Declarations
    • Declaration element is used to define member variables and methods
      • <%! … %>
      • Variables not defined in declaration element are local / method level variables
      • Methods can only be defined in the declaration element
    • Like regular class variables and methods, the location where you define these variables and methods is not important
  • JSP Page Directive
    • Directives affects the overall structure of the servlet generated
      • <%@ … %>
    • Use page directive to import classes
      • <%@ page import=“…, …, …”%>
      • This is equivalent to the “import” statement in regular Java classes
  • JSP Include Directive
    • How to reuse code?
    • Use include directive to include source code from another file
      • <%@ include file=“…” %>
      • Inclusion happens at the compilation time
      • What is included is the source, not generated result
      • Often used to include method definitions
  • Redirection, Refreshing and Forwarding
    • Redirection
      • response.sendRedirect()
    • Refreshing
      • response.setHeader(“Refresh”, “10; url=…”)
    • Forwarding <jsp:forward page=“…” />
      • The “page” attribute follows the same rule as that of <jsp:include/>
      • Forwarding does not invoke network traffic
      • The destination URL is hidden; original requested URL does not change in browser address bar after forwarding
    • Compare redirecting and forwarding
  • Request Processing
    • Using implicit object “request”
    • Processing HTTP request headers
      • The same way as servlet
    • Reading URL parameter
      • http://localhost/appcontext/request.jsp?choice=yes
      • Parameter processing is the same way as servlet, using request.getParameter(“…”), request.getPameterValues(“…”)
  • Form Processing with JSP
    • The same way as servlet
      • request.getParameter(“…”)
      • Note: the action attribute of the form should be a JSP file that processes data
        • <form method=“post” action=“studentprofile.jsp”>…</form>
  • Servlet
  • Java on the Web: J2EE
    • Thin clients (minimize download)
    • Java all “server side”
    • THIS IS WHAT YOU’LL BE DOING!!
    Client Server Servlets
  • Where are Servlets? HTTP Web Server File system Servlet Server Static Dynamic
  • CGI vs. Servlet
    • CGI - new process for each request!!!
    • Servlet - Servlet loaded only once
  • How are Servlets?
    • import java.io.*;
    • import javax.servlet. *;
    • import javax.servlet.http. *;
    • public class Hellox extends HttpServlet {
    • public void doGet ( HttpServletRequest request,
    • HttpServletResponse response)
    • throws IOException, ServletException
    • {
    • response. setContentType(&quot;text/html&quot;) ;
    • PrintWriter out = response. getWriter ();
    • out.println(&quot;<html>&quot;);
    • // out.println(&quot;<body>&quot;);
    • out.println(&quot;<head>&quot;);
    • out.println(&quot;<title>Hello World!</title>&quot;);
    • out.println(&quot;</head>&quot;);
    • out.println(&quot;<body>&quot;);
    • out.println(&quot;<h1>Hello World!</h1>&quot;);
    • out.println(&quot;</body>&quot;);
    • out.println(&quot;</html>&quot;);
    • } // doGet
    • } // Hellox
  • Explanation
    • 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. 
    • The HttpServletRequest has methods that let you find out about incoming information such as FORM data, HTTP request headers, and the like
  • Explanation
    • 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. 
    • You have to import classes in java.io ,javax.servlet ,and javax.servlet.http
  • Directory Structure
  • Methods of Servlets
    • init()
    • service()
    • getServletConfig()
    • getServletInfo()
    • destroy()
  • Init()
    • The  init() method  is called only once by the servlet container throughout the life of a servlet.
  • Service()
    • Once the servlet starts getting the requests, the service() method is called by the servlet container to respond. 
  • Destroy()
    • 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 .
  • Life cycle of Servlet
    • Loading and Inatantiation:
    • Initialization
    • Servicing the Request
    • Destroying the Servlet
  • Loading and Inatantiation
    • 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.
  • Initialization
    • 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.
    • The servlet will be available for service if it is loaded successfully otherwise the servlet container unloads the servlet.
  • Servicing the Request
    • 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.
  • Destroying the Servlet
    • 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.
  • Life Cycle of a Servlet