@_Java_JSP
@_Java_JSP
Introduction
Java Server Pages (JSP) is a technology for developing
Webpages that supports dynamic content. This helps
developers insert java code in HTML pages by making use
of special JSP tags
Java Server Pages are built on top of the Java Servlets API,
so like Servlets, JSP also has access to all the powerful
Enterprise Java APIs
JSP pages can be used in combination with servlets that
handle the business logic
How JSP more advantageous than
Servlet?
They are easy to maintain.
No recompilation or redeployment is
required.
Less coding is required in JSP.
JSP has access to the entire API of JAVA.
JSP are extended version of Servlet.
JSP architecture is a 3-tier architecture that separates a
web application's presentation, logic, and data layers.
The presentation layer, or client side, is responsible for
displaying the user interface and handling user
interaction.
The logic layer, or server-side, is responsible for
processing user requests and handling business logic.
The data layer is responsible for storing and retrieving
data from a database or other storage system.
This separation of concerns allows for better
maintainability and scalability of the application.
JSP architecture flow refers to the sequence of steps a JSP-based
web application goes through to process and execute JSP pages. The
general flow of a JSP architecture can be described as follows:
A client (such as a web browser) sends a request for a JSP page to
a web server.
The web server forwards the request to the JSP engine
responsible for processing JSP pages.
The JSP engine checks if the requested JSP page has been
compiled into a servlet. If not, it compiles the JSP page into a
servlet class. This is done by parsing the JSP page and converting
its elements (such as scriptlets, expressions, and directives) into
Java code.
The JSP engine then compiles the servlet class, which creates a
Java class file that can be executed by the Java Virtual Machine
(JVM).
The JSP engine then creates an instance of the servlet class and
calls the service() method, which generates the dynamic content
for the JSP page. Within the service() method, the JSP engine
generates the HTML code for the response by combining the static
template in the JSP page with the dynamic content generated by
the Java code.
The JSP engine sends the generated HTML code back to the web
server, which then sends it back to the client as a response.
The JSP engine also maintains a cache of the compiled servlet
classes so subsequent requests for the same JSP page can be
handled more efficiently.
The web server needs a JSP engine, i.e., a container to
process JSP pages.
JSP is first converted into a servlet by the JSP container
before processing the client’s request.
We use 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 page
1. JSP declaration
2. JSP Expression
3. JSP Comments
4. Scriptlet
5. JSP Directives
6. JSP Actions
JSP Declaration
A declaration declares one or more variables or methods that you can use in
Java code later in the JSP file.You must declare the variable or method before
you use it in the JSP file.
Syntax
<%! declaration; [ declaration; ]+ ... %>
Example
<%! Int a,b,c=0; %>
Examples
<html>
<body>
<%! int data=50; %>
<%= "Value of the variable is:"+data %>
</body>
</html>
<html>
<body>
<%!
int cube(int n)
{
return n*n*n*;
}
%>
<%= "Cube of 3 is:"+cube(3) %>
</body>
</html>
JSP Expression
JSP expression element contains a scripting language expression that is evaluated,
converted to a String, and inserted where the expression appears in the JSP file
The expression element can contain any expression that is valid according to the Java
Language Specification but you cannot use a semicolon to end an expression.
Syntax
<%= expression %>
Example
<%= (new java.util.Date()).toLocaleString()%>
Example
<html>
<body>
<%= "welcome to jsp" %>
</body>
</html>
JSP Comments
JSP comment marks the text or the statements that the JSP
container should ignore. A JSP comment is useful when you want
to hide or "comment out", a part of your JSP page
Syntax
<%--This is JSP comment --%>
Example
<%-- This comment will not be visible in the page source --%>
Scriptlets
File: welcome.jsp
<html>
<body>
<%
String name=request.getParameter("uname");
out.print("welcome "+name);
%>
</body>
</html>
JSP Directives
A JSP directive affects the overall structure of the servlet class
Syntax:
<%@ directive attribute="value" %>
There are three types of directive tag:
1. Page Directive : This directive is used to provide instructions
to the container pertain to the current JSP page
Syntax:
<%@ page attribute="value" %>
Example
<%@ page import="java.sql.*" %>
The Page directive provides many other attributes
Attributes of JSP page directive
import
contentType
extends
info
buffer
language
isELIgnored
isThreadSafe
autoFlush
session
pageEncoding
errorPage
isErrorPage
1)import
The import attribute is used to import class,interface or all the
members of a package.It is similar to import keyword in java class
or interface.
Example of import attribute
<html>
<body>
</body>
</html>
2)contentType
The contentType attribute defines the MIME(Multipurpose Internet
Mail Extension) type of the HTTP response.The default value is
"text/html;charset=ISO-8859-1".
Example of contentType attribute
<html>
<body>
</body>
</html>
3)extends
The extends attribute defines the parent class that will be inherited by the
generated servlet.It is rarely used.
4)info
This attribute simply sets the information of the JSP page which is
retrieved later by using getServletInfo() method of Servlet interface.
Example of info attribute
<html>
<body>
</body>
</html>
5)buffer
The buffer attribute sets the buffer size in kilobytes to handle output
generated by the JSP page.The default size of the buffer is 8Kb.
6)language
The language attribute specifies the scripting language used in the JSP page.
The default value is "java".
isThreadSafe
By default, servlet engines load a single instance of a servlet and use a
pool of threads to service individual requests.
This means two or more threads can be executing the same servlet
methods simultaneously. If the servlet has instance variables, and if no
provision is made to synchronize access, the threads can collide and
interfere with each others’ access to the variables.
Let's understand this with an example. Suppose you have created a JSP
page and mentioned isThreadSafe as true, it means that the JSP page
supports multithreading (more than one thread can execute the JSP page
simultaneously).
On the other hand, if it is set to false then JSP engine won’t allow
multithreading which means the only single thread will execute the page
code.
9)errorPage
The errorPage attribute is used to define the error page, if exception
occurs in the current page, it will be redirected to the error page.
</body>
</html>
10)isErrorPage
The isErrorPage attribute is used to declare that the current page is the error page.
Note: The exception object can only be used in the error page.
</body>
</html>
JSP Directives Contd…
2. Include Directive:
Syntax:
<%@ include file="relative url" >
Example
<%@ include file="header.jsp" %>
JSP Directives Contd…
Taglib Directive: The JavaServer Pages API allow you to define
custom JSP tags that look like HTML or XML tags and a tag
library is a set of user-defined tags that implement custom
behavior.
Syntax:
<%@ taglib uri="uri" prefix="prefixOfTag" >
Example
<%@ taglib uri="http://www.example.com/custlib"
prefix="mytag" %>
JSP Actions
These actions use constructs in XML syntax to control the behavior
of the servlet engine.
You can dynamically insert a file, reuse JavaBeans components,
forward the user to another page, or generate HTML for the Java
plugin.
Syntax:
<jsp:action_name attribute="value" />
Example
<jsp:forward page=“home.jsp" />
Implicit Objects
These objecrs are the Java objects that the JSP Container
makes available to the developers in each page and the
developer can call them directly without being explicitly
declared. JSP Implicit Objects are also called pre-defined
variables.
response
This is the HttpServletResponse object associated with the response to the
client.
Send information such as cookies and HTTP response header
out
This is the PrintWriter object used to send output to the client.
session
This is the HttpSession object associated with the request.
Access session related info such as creation time and id associated with session
Application
pageContext
This encapsulates use of server-specific features like higher
performance JspWriters.
page
This is simply a synonym for this, and is used to call the methods defined
by the translated servlet class.
Exception
The Exception object allows the exception data to be accessed by
designated JSP.
A simple JSP Code
<html>
<body>
<% out.print(2*5); %>
</body>
</html>
Exception Handling in JSP
The exception is normally an object that is thrown at runtime.
Exception Handling is the process to handle the runtime errors.
There may occur exception any time in your web application.
<form action="process.jsp">
No1:<input type="text" name="n1" /><br/><br/>
No12:<input type="text" name="n2" /><br/><br/>
<%
String num1=request.getParameter("n1");
String num2=request.getParameter("n2");
int a=Integer.parseInt(num1);
int b=Integer.parseInt(num2);
int c=a/b;
out.print("division of numbers is: "+c);
%>
error.jsp