Вы находитесь на странице: 1из 70

Server Side Scripting with JSP

Module 2
Module 2
• JSP and HTTP, JSP Engines, How JSP Works, JSP
and Servlet, Anatomy of a JSP Page
• JSP Syntax, JSP Directives, JSP implicit objects
• JSP form processing, JSP actions
• Sharing Data Between JSP Pages Requests and
Users: Users Passing Control and Data
between Pages
• Sharing Session and Application Data
Advantages of JSP over Servlet
1. Reduced Coding: The objects of PrintWriter, ServletConfig, ServletContext,
HttpSession and RequestDispatcher etc. are created by the Programmer in
Servlets and used. But in JSP, they are builtin and are known as "implicit
objects". That is, in JSP, Programmer never creates these objects and
straightaway use them as they are implicitly created and given by JSP
container. This decreases lot of coding.
2. JSP needs no compilation by the Programmer. Programmer deploys directly a
JSP source code file in server where as incase of Servlets, the Programmer
compiles manually a Servlet file and deploys a .class file in server.
3. With JSP, Programmer can build custom tags that can be called in JavaBeans
directly. Servlets do not have this advantage. Reusability increases with tag
libraries and JavaBean etc.
4. Presentation logic: In Servlets, both static code and dynamic code are put
together. In JSP, they are separated.

3
The JSP Framework
• Idea:
– Java Server Pages (JSP) is a server-side programming technology.
– Servlet adds HTML code inside Java code, while JSP adds Java code
inside HTML using JSP tags. Everything a Servlet can do, a JSP page can
also do it.
– A JSP page consists of HTML tags and JSP tags(to insert java code in
HTML pages). The JSP tags which allow java code to be included into it
are <% ----java code----%>
– It can consist of either HTML or XML (combination of both is also
possible) with JSP actions and commands.
• Uses
– Used to design forms and registration pages with the dynamic content.
– to access JavaBeans objects.
– share information across pages using request and response objects.

4
JSP
• JSP is a server side technology that does all the processing at
server. It is used for creating dynamic web applications, using
java as programming language.

• Basically, any html file can be converted to JSP file by just


changing the file extension from “.html” to “.jsp”, it would
run just fine.

• JSP is an extension of servlets and every JSP page first gets


converted into servlet by JSP container before processing the
client’s request.
JSP – Architecture and JSP Engines

• 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. This tutorial makes
use of Apache which has built-in JSP container to support
JSP pages development.

• 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.
JSP Engine
How JSP Works
How JSP Works
JSP page translation and processing phases
Translation phase

Hello.jsp
Read
Request
helloServlet.java
Generate

Response
Client Server
Execute
helloServlet.class

Processing phase
14
How JSP Works
How JSP works: JSP Processing
How JSP works:JSP Processing
JSP - Lifecycle
JSP - Lifecycle
JSP - Syntax
JSP - Syntax
JSP Directives
• These directives provide directions and
instructions to the container, telling it how to
handle certain aspects of the JSP processing.
• The session Object
• The session object is an instance
of javax.servlet.http.HttpSession and behaves
exactly the same way that session objects
behave under Java Servlets.
• The session object is used to track client
session between client requests.

Вам также может понравиться