+ All Categories
Home > Design > J2EE - JSP-Servlet- Container - Components

J2EE - JSP-Servlet- Container - Components

Date post: 21-Jan-2015
Category:
Upload: kaml-sah
View: 4,455 times
Download: 1 times
Share this document with a friend
Description:
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
Popular Tags:
47
Kamalesh Sah http://www.youtube.com/kamlesh utube
Transcript
Page 1: J2EE - JSP-Servlet- Container - Components

Kamalesh Sahhttp://www.youtube.com/kamleshutube

Page 2: J2EE - JSP-Servlet- Container - Components

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.

Page 3: J2EE - JSP-Servlet- Container - Components

Multitiered Applications

Page 4: J2EE - JSP-Servlet- Container - Components

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.

Page 5: J2EE - JSP-Servlet- Container - Components

J2EE Clients

Page 6: J2EE - JSP-Servlet- Container - Components

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

Page 7: J2EE - JSP-Servlet- Container - Components

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.

Page 8: J2EE - JSP-Servlet- Container - Components

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.

Page 9: J2EE - JSP-Servlet- Container - Components

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.

Page 10: J2EE - JSP-Servlet- Container - Components

J2EE Server Communications

Page 11: J2EE - JSP-Servlet- Container - Components

Web Tier and J2EE Applications

Page 12: J2EE - JSP-Servlet- Container - Components

Web, Business and EIS Tiers

Page 13: J2EE - JSP-Servlet- Container - Components

JSP

Page 14: J2EE - JSP-Servlet- Container - Components

Point to Discuss

• JSP introduction• JSP elements• JSP development practices

Page 15: J2EE - JSP-Servlet- Container - Components

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

Page 16: J2EE - JSP-Servlet- Container - Components

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

Page 17: J2EE - JSP-Servlet- Container - Components

JSP Compilation and Execution

17

JSP Page

Compile JSP Servlet Instance in Memory

init()

service()

First requestafter creation or

modification

Subsequent Requests (can

be from different users and sessions)

JSP Servlet

Automatic Compilation

Page 18: J2EE - JSP-Servlet- Container - Components

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

Page 19: J2EE - JSP-Servlet- Container - Components

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 (<%-- … --%>)

Page 20: J2EE - JSP-Servlet- Container - Components

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

Page 21: J2EE - JSP-Servlet- Container - Components

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

Page 22: J2EE - JSP-Servlet- Container - Components

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

Page 23: J2EE - JSP-Servlet- Container - Components

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

Page 24: J2EE - JSP-Servlet- Container - Components

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

Page 25: J2EE - JSP-Servlet- Container - Components

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

Page 26: J2EE - JSP-Servlet- Container - Components

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

Page 27: J2EE - JSP-Servlet- Container - Components

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

Page 28: J2EE - JSP-Servlet- Container - Components

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(“…”)

Page 29: J2EE - JSP-Servlet- Container - Components

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>

Page 30: J2EE - JSP-Servlet- Container - Components

Servlet

Page 31: J2EE - JSP-Servlet- Container - Components

Java on the Web: J2EE• Thin clients (minimize download)• Java all “server side”

THIS IS WHAT YOU’LL BE DOING!!

Client Server

Servlets

Page 32: J2EE - JSP-Servlet- Container - Components

Where are Servlets?

HTTP WebServer

File system

ServletServer

Static

Dynamic

Page 33: J2EE - JSP-Servlet- Container - Components

CGI vs. Servlet

• CGI - new process for each request!!!

• Servlet - Servlet loaded only once

Page 34: J2EE - JSP-Servlet- Container - Components

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("text/html"); PrintWriter out = response.getWriter(); out.println("<html>");// out.println("<body>"); 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>");} // doGet} // Hellox

Page 35: J2EE - JSP-Servlet- Container - Components

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

Page 36: J2EE - JSP-Servlet- Container - Components

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

Page 37: J2EE - JSP-Servlet- Container - Components

Directory Structure

Page 38: J2EE - JSP-Servlet- Container - Components

Methods of Servlets

• init()• service()• getServletConfig()• getServletInfo()• destroy()

Page 39: J2EE - JSP-Servlet- Container - Components

Init()

• The init() method is called only once by the servlet container throughout the life of a servlet.

Page 40: J2EE - JSP-Servlet- Container - Components

Service()

• Once the servlet starts getting the requests, the service() method is called by the servlet container to respond.

Page 41: J2EE - JSP-Servlet- Container - Components

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 .

Page 42: J2EE - JSP-Servlet- Container - Components

Life cycle of Servlet

• Loading and Inatantiation:• Initialization• Servicing the Request• Destroying the Servlet

Page 43: J2EE - JSP-Servlet- Container - Components

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.

Page 44: J2EE - JSP-Servlet- Container - Components

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.

Page 45: J2EE - JSP-Servlet- Container - Components

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.

Page 46: J2EE - JSP-Servlet- Container - Components

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.

Page 47: J2EE - JSP-Servlet- Container - Components

Life Cycle of a Servlet


Recommended