Download as pdf or txt
Download as pdf or txt
You are on page 1of 55

Department of Applied Computational Science and Engineering

Course Code: KCS-602


Course Name: Web Technology
Faculty Name: Anuj Gupta
Email: anuj.gupta_acse@glbitm.ac.in
Course Outcome

KCS 602 WEB TECHNOLOGY


Course Outcome ( CO) Bloom’s Knowledge Level (KL)
At the end of course , the student will be able to
CO 1 Explain web development Strategies and Protocols governing Web. K1, K2

CO 2 Develop Java programs for window/web-based applications. K2, K3


CO 3 Design web pages using HTML, XML, CSS and JavaScript. K2, K3

CO 4 Creation of client-server environment using socket programming K1, K2,

CO 5 Building enterprise level applications and manipulate web databases using JDBC K3, K4
CO6 Design interactive web applications using Servlets and JSP K2, K3

Subject:Web
Subject: Web Technology
Technology
Syllabus

Unit-5
Servlets: Servlet Overview and Architecture, Interface Servlet and the
Servlet Life Cycle, Handling HTTP get Requests, Handling HTTP post
Requests, Redirecting Requests to Other Resources, Session Tracking,
Cookies, Session Tracking with Http Session

Java Server Pages (JSP): Introduction, Java Server Pages Overview, A First
Java Server Page Example, Implicit Objects, Scripting, Standard Actions,
Directives, Custom Tag Libraries.

Subject:Web
Subject: Web Technology
Technology
Subject: Web Technology
Subject: Web Technology
JSP

 JSP stands for Java Server Pages.

 JSP was developed by Sun Microsystems and is an improved version of Java servlets.

 JSP is translated into Java servlet before being run, and it processes only HTTP requests and
generates responses like any servlet.

 A JSP page consists of HTML tags and JSP tags. The JSP pages are easier to maintain than Servlet
because we can separate designing and development. It provides some additional features such as
Expression Language, Custom Tags, etc.

Subject: Web Technology


Advantages of JSP
• Extension to Servlet: JSP technology is the extension to Servlet technology. We can use all the features of
the Servlet in JSP. In addition to, we can use implicit objects, predefined tags, expression language and
Custom tags in JSP, that makes JSP development easy.
• Quick and easy development
• Easy to maintain: JSP can be easily managed because we can easily separate our business logic with
presentation logic. In Servlet technology, we mix our business logic with the presentation logic.
• Fast Development: No need to recompile and redeploy.If JSP page is modified, we don't need to recompile
and redeploy the project. The Servlet code needs to be updated and recompiled if we have to change the
look and feel of the application.
• Less code than Servlet: In JSP, we can use many tags such as action tags, JSTL, custom tags, etc. that
reduces the code. Moreover, we can use EL, implicit objects, etc.

Subject: Web Technology


Disadvantages of JSP

• Though the database can be accessed with JSP, it is not easy to access the database as most of the servlet
does not provide support.
• Being a servlet, if there’s an issue in the code, it becomes very hard to trace.
• It compilation time required is more than on a server.

Subject: Web Technology


JSP Page Translation
Translation Phase

Hello.jsp
HTTP request [1] Generate
Read
JSP page
helloServlet.java

HTML page

HTTP response properties, JavaBean


Execute helloServlet.class call methods Library

Browser Processing Phase


Web server

DB

Subject: Web Technology


Step 1. The client navigates to a file ending with the .jsp extension and the browser initiates an HTTP
request to the webserver. For example, the user enters the login details and submits the button.
The browser requests a status.jsp page from the webserver.

Step 2. If the compiled version of JSP exists in the web server, it returns the file. Otherwise, the request is
forwarded to the JSP Engine. This is done by recognizing the URL ending with .jsp extension.

Step 3. The JSP Engine loads the JSP file and translates the JSP to Servlet(Java code). This is done by
converting all the template text into println() statements and JSP elements to Java code. This
process is called translation.

Step 4. The JSP engine compiles the Servlet to an executable .class file. It is forwarded to the Servlet
engine. This process is called compilation or request processing phase.

Step 5. The .class file is executed by the Servlet engine which is a part of the Web Server. The output is an
HTML file. The Servlet engine passes the output as an HTTP response to the webserver.

Step 6. The web server forwards the HTML file to the client’s browser.
Subject: Web Technology
Architecture of JSP
JSP architecture is a three-tier architecture consisting of a web server, client, and database.

Web Server: It uses a JSP engine like a container that processes JSP. E.g., Apache Tomcat. This engine intercepts
the request for JSP and provides a runtime environment for the understanding and processing of JSP files. It
reads, parses, builds Java Servlet, Compiles, Executes Java code, and returns the HTML page to the client.

Client: It is the web browser/application on the user’s side.

Database: It is used to store the user’s data. The webserver has access to the database.

Subject: Web Technology


Model 1 - Page Centric Approach

1 2
Java Bean

JSP 3

4
Database

1. Browser sends request for the JSP page


2. JSP accesses Java Bean and invokes business logic
3. Java Bean connects to the database and get/save data
4. Response is sent to the browser which is generated by JSP

Subject: Web Technology


Problems of Model 1 Architecture

• Navigation control is decentralized


• Time consuming
• Hard to extend
• Confusion for developer due to all java code in single file.

Subject: Web Technology


What is MVC?

Controller

View Model

MVC stands for Model View and Controller.


It is a design pattern that separates the business logic, presentation logic and data.
Controller acts as an interface between View and Model.
Model represents the state of the application i.e. data. It can also have business logic.
View represents the presentation i.e. UI(User Interface).
Subject: Web Technology
MVC Flow
Controller
Request
Request

View
View
View
Response
Step
Step
Step
Step 35
42 1
Step
Response
View
Model isprocesses
is passed
Incoming
Controller rendered
transforms Model
to View
request into appropriate
directed
request to forms aoutput
andController. format
data Model

Subject: Web Technology


Model 2 - Dispatchers Approach

Model-View-Controller (MVC)

Java Web Application

View Controller Model

JSP Servlet Bean

Subject: Web Technology


MVC FLOW – IN JSP

CONTROLLER
Request
Browser
Servlet

MODEL
DB
Java Bean

Enterprise Server/
VIEW
Response Data Sources
Jsp

Application Server

Subject: Web Technology


Advantages of MVC
• Navigation control is centralized

• Separating Model from View (that is, separating data representation from
presentation)

• easy to add multiple data presentations for the same data, facilitates adding new
types of data

• Independently enhancing maintainability

• Extensibility

• Testability.

Subject: Web Technology


Creating a simple JSP Page
To create the first JSP page, write some HTML code as given below, and save it by .jsp extension. We have
saved this file as index.jsp. Put it in a folder and paste the folder in the web-apps directory in apache
tomcat to run the JSP page.

index.jsp
Let's see the simple example of JSP where we are using the scriptlet tag to put Java code in the JSP page.
We will learn scriptlet tag later.
1. <html>
2. <body>
3. <% out.print(2*5); %>
4. </body>
5. </html>

It will print 10 on the browser.

Subject: Web Technology


How to run a simple JSP Page?
Follow the following steps to execute this JSP page:
• Start the server
• Put the JSP file in a folder and deploy on the server
• Visit the browser by the URL http://localhost:portno/contextRoot/jspfile, for example,
http://localhost:8888/myapplication/index.jsp

Do I need to follow the directory structure to run a simple JSP?


• No, there is no need of directory structure if you don't have class files or TLD files.
• For example, put JSP files in a folder directly and deploy that folder. It will be running fine.
However, if you are using Bean class, Servlet or TLD file, the directory structure is required.

Subject: Web Technology


The Directory structure of JSP-

The directory structure of JSP page is same as Servlet. We contain the JSP page outside the WEB-INF
folder or in any directory.

Subject: Web Technology


Difference between a Web Server, a Web Container, and an Application Server
Web server: It receives HTTP request, intercepts it. It also processes the HTTP response to the web browser
of the client. E.g., Apache web server.

Web container: A web container is J2EE compliant implementation. Its main job is to run the runtime
environment to JSP and servlets. Request receiver at web browser is forwarded here, and the result
generated is sent back to the web server. E.g., Tomcat.

Application Server: It is regarded as the


complete server as it provides the facility of
both a web server as well as a web container.
It is a combination of both formers. E.g.,
Oracle Application Server, Bea WebLogic.

Subject: Web Technology


JSP Life Cycle

Subject: Web Technology


1) JSP Translation
 When the client sends the request to the web server, it goes to the web container. Now evidently, if
the JSP page is newer and the servlet class is older, it means that the page got modified.
 In that case, the translation is done otherwise, this step is skipped so as to improve the performance
of the system as it takes time.

2) JSP Compilation
 Prior to compilation, JSP engine probes if the page has ever been compiled.
 If the page has some modifications or if it has never been compiled, then JSP engine compiles the
page.

3) Class Loading
 At this stage, the .class file gets loaded by the loader i.e., corresponding servlet class file is loaded.

Subject: Web Technology


4) Instantiation
 As we compiled the servlet, the object of the generated servlet is created. This process takes place
after the class file is loaded in the memory. As soon as the instantiation takes place the objects:
ServletConfig and ServletContext get accessible to the JSP class.
 ServletConfig is a pre-defined interface used to develop flexible servlets. ServletContext is
generated by the web container when the project is deployed. ServletConfig object exists as one
per servlet program, whereas ServletContext object exists as one per web application.

5) Initialization
 After instantiation, container invokes jspInit() method better known as initialization of JSP.
 It basically initializes the instance we created. It is done to initialize resources needed to process
the request i.e. resources like databases, allow JSP pages to read data, create lookup tables, and
manage network connections.
 Though it is invoked by the container only once, it can be overridden by the author.

Subject: Web Technology


6) Request Processing
 After the initialization of servlet instances, a new thread gets created. All the interactions
necessary to process requests occur at this stage. Thus, container invokes _jspService() method.
 This method contains two parameters:
• HTTPServletRequest req
• HTTPServletResponse res

 This service method is responsible for generating a response to the request. Not only this, but it also
generates response to all seven HTTP methods, namely:

a) GET
 GET is a read only request. It tends to get information from the server. GET request posses 3
standard triggers:
• Typing in the address line of the browser and clicking enter or go.
• Pressing submit button in HTML form.
• Clicking on the link of a web page in order to access it.
Subject: Web Technology
b) POST
 This method gets triggered when the form settings is made as a post.
c) HEAD
This method is almost similar to the GET method. Though this method doesn’t return a message body, it
can check if a resource hasn’t been recently updated, valid, and accessible.
d) PUT
Using the request payload, PUT method replaces current representations of the target resource.
e) DELETE
This method deletes the specified resource.
f) CONNECT
This method establishes a path identified by the target resource to reach the server.
g) OPTIONS
This method describes the communication options that we need to communicate with the target resource.

Subject: Web Technology


7) Destruction
 The destruction basically represents cleanup. For the cleanup jspDestroy() method gets invoked.
 This cleanup means that the JSP page after processing gets removed from the container.
 This method destroys the instance of the servlet class code, and all the connections to database and
network are released.Opened files are also closed.
 jspDestroy() is overridden whenever the cleanup needs to be done.
public void jspDestroy ()
{ //Cleanup code }

Conclusion
Finally we have seen JSP Life cycle, various phases of JSP Lifecycle and methods invoked in it:
• Web container translates JSP code into servlet class source (.java)
• It is compiled into java servlet class.
• Class Loader loads Servlet class bytecode.
• Container creates instance of that servlet class
• Initialize servlet can now service requests.
• _jspService() is invoked by web container for each request.
• To remove the servlet instance jspDestroy() is called. It performs cleanup.

Subject: Web Technology


Scope

Subject: Web Technology


JSP Tags

Subject: Web Technology


Declaration Tag <%! %>

Subject: Web Technology


Subject: Web Technology
Expression Tag <%= %>

Subject: Web Technology


Subject: Web Technology
Subject: Web Technology
Directive Tag <%@ %>

Subject: Web Technology


Directive Tag <%@ %>

Subject: Web Technology


Directive Tag <%@ %>
Page Directive
There are several attributes, which are used along with Page Directives and these
are
 language
 info
 import
 errorPage
 Session
 isErrorPage
 Buffer
 extends
 autoflush
 ContentType
 isThreadSafe

Subject: Web Technology


Directive Tag <%@ %>

Subject: Web Technology


Directive Tag <%@ %>

Subject: Web Technology


Directive Tag <%@ %>

Subject: Web Technology


Directive Tag <%@ %>

Subject: Web Technology


Directive Tag <%@ %>

Subject: Web Technology


Directive Tag <%@ %>

Subject: Web Technology


Include Directive Tag

Subject: Web Technology


Include Directive Tag

Subject: Web Technology


Tag lib Directive Tag

Subject: Web Technology


Scriptlet Tag <%@ %>

Subject: Web Technology


Comment Tag <%-- --%>

Subject: Web Technology


JSP Action Tag <%-- --%>

JSP Action Tags Description

jsp:forward forwards the request and response to another resource.


jsp:include includes another resource.
jsp:useBean creates or locates bean object.
jsp:setProperty sets the value of property in bean object.

jsp:getProperty prints the value of property of the bean.

jsp:plugin embeds another components such as applet.

jsp:param sets the parameter value. It is used in forward and include mostly.

jsp:fallback can be used to print the message if plugin is working. It is used in jsp:plugin.

Subject: Web Technology


Syntax

Actions
Predefined tasks that are processed by the JSP container at request time.

<jsp:include> Action
Includes a file at the time the page is requested.
<jsp: include page="banner.html" flush = "true" />

Subject: Web Technology


Syntax

<jsp:useBean> Action
Declares a Java Bean instance for use in the JSP page.

<jsp:useBean id="courseBean"
class="coursepack.CourseListBean" />

Subject: Web Technology


Syntax

<jsp: getProperty> Action


Gets a property in the specified JavaBean instance.
<jsp:getProperty name="courseBean"
property="courseColor" />

Equivalent to expression:
<%= courseBean.getCourseColor(courseNumber) %>

Subject: Web Technology


Syntax

<jsp: setProperty> Action


Sets a property in the specified JavaBean instance.
<jsp:setProperty name="courseBean" property="courseColor"
value="blue" />

Equivalent to expression:
<%= courseBean.setCourseColor("red") %>

Subject: Web Technology


JSP Implicit Objects-

Implicit Usage Classes


Object
1 out Accesses the JSP output stream javax.servlet.jsp.JspWriter
2 request Provides access to the client request javax.servlet.http.HttpServletRequest
3 response Provides access to the JSP response javax.servlet.http.HttpServletResponse
4 session Shares information across client requests javax.servlet.http.HttpSession
5 exception Accesses error status javax.servlet.jsp.JspException
6 application Accesses application-level objects javax.servlet.ServletContext
7 config Provides configuration information javax.servlet.ServletConfig
8 pageContext Accesses the JSP container javax.servlet.jsp.PageContext
9 page Provides a reference to the current JSP java.lang.Object

Subject: Web Technology

You might also like