0% found this document useful (0 votes)
43 views

Chapter 5 JSP

Java Server Pages (JSP) enables the creation of dynamic web applications using Java. JSP offers advantages over CGI like better performance since dynamic content is embedded in HTML pages rather than separate files, and access to powerful Java APIs. The JSP lifecycle involves compilation of JSP files into servlets, initialization, execution to handle requests, and cleanup.

Uploaded by

kajelchasafe
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
43 views

Chapter 5 JSP

Java Server Pages (JSP) enables the creation of dynamic web applications using Java. JSP offers advantages over CGI like better performance since dynamic content is embedded in HTML pages rather than separate files, and access to powerful Java APIs. The JSP lifecycle involves compilation of JSP files into servlets, initialization, execution to handle requests, and cleanup.

Uploaded by

kajelchasafe
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 27

Java Servlet Pages (JSP)

Chapter 5
Introduction

 Java Server Pages (JSP) is a server-side programming technology.


 It enables the creation of dynamic, platform-independent method for
building Web-based applications.
 JSP have access to the entire family of Java APIs, including the
JDBC API to access enterprise databases
Why to Learn JSP?
Common Gateway Interface (CGI)

 JavaServer Pages often serve the same purpose as programs implemented using the .
 But JSP offers several advantages in comparison with the CGI.
 Performance is significantly better because JSP allows embedding Dynamic Elements in
HTML Pages itself instead of having separate CGI files.
 JSP are always compiled before they are processed by the server unlike CGI/Perl which
requires the server to load an interpreter and the target script each time the page is
requested.
 JavaServer Pages are built on top of the Java Servlets API, so like Servlets, JSP also has
access to all the powerful Enterprise Java APIs, including JDBC, JNDI, EJB, JAXP, etc.
Cont’d

 JSP pages can be used in combination with servlets that handle the business logic, the
model supported by Java servlet template engines
 Finally, JSP is an integral part of Java EE, a complete platform for enterprise class
applications.
 This means that JSP can play a part in the simplest applications to the most complex and
demanding.
Applications of JSP

 JSP vs. Active Server Pages (ASP)


 The advantages of JSP are twofold.
 First, the dynamic part is written in Java, not Visual Basic or other MS specific language,
so it is more powerful and easier to use.
 Second, it is portable to other operating systems and non-Microsoft Web servers.
 JSP vs. Pure Servlets
 It is more convenient to write (and to modify!) regular HTML than to have plenty of
println statements that generate the HTML
JSP vs. Server-Side Includes (SSI)

 SSI is really only intended for simple inclusions, not for "real" programs that use form
data, make database connections, and the like.
 JSP vs. JavaScript
 JavaScript can generate HTML dynamically on the client but can hardly interact with the
web server to perform complex tasks like database access and image processing etc.
 JSP vs. Static HTML
 Regular HTML, of course, cannot contain dynamic information.
JSP - Environment Setup
 A development environment is where you would develop your JSP programs, test them and finally run
them.
 Setting up Java Development Kit
 This step involves downloading an implementation of the Java Software Development Kit (SDK) and
setting up the PATH environment variable appropriately.
 Setting up Web Server: Tomcat
 A number of Web Servers that support JavaServer Pages and Servlets development are available in the
market.
 Apache Tomcat is an open source software implementation of the JavaServer Pages and Servlet
technologies and
 It can act as a standalone server for testing JSP and Servlets, and can be integrated with the Apache Web
Server
 Download the latest version of Tomcat from https://tomcat.apache.org/
JSP - Architecture

 The web server needs a JSP engine, i.e, a container to process JSP pages.
 The JSP container is responsible for intercepting requests for JSP pages.
 A JSP container works with the Web server to provide the runtime environment and other
services a JSP needs.
 It knows how to understand the special elements that are part of JSPs.
Architecture of JSP
JSP Processing

 As with a normal page, your browser sends an HTTP request to the web server.
 the web server recognizes that the HTTP request is for a JSP page and forwards it to a JSP
engine.
 This is done by using the URL or JSP page which ends with .jsp instead of .html.
 The JSP engine loads the JSP page from disk and converts it into a servlet content.
 This conversion is very simple in which all template text is converted to println( )
statements and all JSP elements are converted to Java code.
Cont’d

 The JSP engine compiles the servlet into an executable class and forwards the original
request to a servlet engine.
 A part of the web server called the servlet engine loads the Servlet class and executes it.
 During execution, the servlet produces an output in HTML format.
 The output is furthur passed on to the web server by the servlet engine inside an HTTP
response.
 The web server forwards the HTTP response to your browser in terms of static HTML
content.
 Finally, the web browser handles the dynamically-generated HTML page inside the HTTP
response exactly as if it were a static page.
JSP Processing
JSP Life Cycle

 A JSP life cycle is defined as the process from its creation till the destruction.
 This is similar to a servlet life cycle with an additional step which is required to compile a
JSP into servlet.
 The following are the paths followed by a JSP −
 Compilation
 Initialization
 Execution
 Cleanup
Servlets - Life Cycle

 A servlet life cycle can be defined as the entire process from its
creation till the destruction.
 The following are the paths followed by a servlet.
 The servlet is initialized by calling the init() method.
 The servlet calls service() method to process a client's request.
 The servlet is terminated by calling the destroy() method.
 Finally, servlet is garbage collected by the garbage collector of the
JVM.
The init() Method

 The init method is called only once. It is called only when the servlet is created, and not
called for any user requests afterwards.
 So, it is used for one-time initializations, just as with the init method of applets.
 The servlet is normally created when a user first invokes a URL corresponding to the servlet,
but you can also specify that the servlet be loaded when the server is first started.
 When a user invokes a servlet, a single instance of each servlet gets created, with each user
request resulting in a new thread that is handed off to doGet or doPost as appropriate.
 The init() method simply creates or loads some data that will be used throughout the life of
the servlet.
Init() method

 public void init() throws ServletException {


 // Initialization code...
 }
The service() Method

 The service() method is the main method to perform the actual task.
 The servlet container (i.e. web server) calls the service() method to
handle requests coming from the client( browsers) and to write the
formatted response back to the client.
 Each time the server receives a request for a servlet, the server spawns a
new thread and calls service.
 The service() method checks the HTTP request type (GET, POST, PUT,
DELETE, etc.) and calls doGet, doPost, doPut, doDelete, etc. methods as
appropriate.
Service() method

 public void service(ServletRequest request, ServletResponse response)


 throws ServletException, IOException {
 }
The doGet() Method

 A GET request results from a normal request for a URL or from an HTML form that has no
METHOD specified and it should be handled by doGet() method.
 Java Code
 public void doGet(HttpServletRequest request, HttpServletResponse response)
 throws ServletException, IOException {
 // Servlet code
 }
The doPost() Method

 A POST request results from an HTML form that specifically lists POST as the METHOD
and it should be handled by doPost() method.
 public void doPost(HttpServletRequest request, HttpServletResponse response)
 throws ServletException, IOException {
 // Servlet code
 }
The destroy() Method

 The destroy() method is called only once at the end of the life cycle of a servlet. This
method gives your servlet a chance to close database connections, halt background threads,
write cookie lists or hit counts to disk, and perform other such cleanup activities.
 After the destroy() method is called, the servlet object is marked for garbage collection.
 The destroy method definition looks like this −
 public void destroy() {
 // Finalization code...
 }
Java web ..used for JavaServelet
JSP in Netbeans
Writing HTML in WEB.INF/Webpages
Writing Java code in Source packages
The End!!

 Thank you!!!
 Any question???

You might also like