JSP Introduction
JSP Introduction
JSP
JSP stands for Java Server Pages.
JSP technology is used to create web application just like Servlet.
A JSP page consists of HTML tags and JSP tags. The JSP pages are easier to maintain than Servlet because we
can separate Business Logic and Presentation Logic.
JSP technology has facilitated the segregation of the work of a Web designer and a Web developer.
A Web designer can design and formulate the layout for the Web page by using HTML.
On the other hand, a Web developer working independently can use java code and other JSP specific tags to
code the business logic.
A JSP page , after compilation , generates a servlet and therefore incorporates all servlet functionalities.
It is a server-side programming technology.
JSP is a platform independent language used to create database driven web applications
In translation phase JSP page is compiled into a servlet called JSP Page.
There are five basic steps and these step are as follows:
1. The user goes to a JSP page and makes the request via network in user’s web browser.
3. Web server accepts the requested .jsp file and passes the JSP file to the JSP Engine.
4. If the JSP file has been called the first time then the JSP file is parsed into servlet otherwise servlet is
instantiated. The generated servlet output is sent via the network form web server to users web browser.
5. Now in last step, HTML results are displayed on the users web browser.
Advantages of JSP over Servlet
1. In JSP, we can easily separate our business logic with presentation logic. In Servlet technology, we mix our
business logic with the presentation logic.
2. If JSP page is modified, we don't need to recompile the project. But if Servlet page is modified, we need to
recompile the project.
3. In JSP, we can use many tags such as action tags, custom tags, etc. that reduces the code. Moreover, we can use
implicit objects, etc.
Assignment:
Q1. Write down the disadvantages of JSP.
Q2. Write down the applications of JSP.
Q3. Which technology is better JSP or Servlet and why?
The Lifecycle of a JSP Page
The JSP pages follow these phases:
1. Translation of JSP Page
2. Compilation of JSP Page
3. Classloading (the classloader loads class file)
4. Instantiation (Object of the Generated Servlet is created).
5. Initialization (the container invokes jspInit () method).
6. Request processing (the container invokes _jspService () method).
7. Destroy (the container invokes jspDestroy () method).
JSP page is translated into Servlet by the help of JSP translator. The JSP translator is a part of the web server which is
responsible for translating the JSP page into Servlet.
After that, Servlet page is compiled by the compiler and gets converted into the class file.
Once JSP class is loaded into memory, its object is instantiated by the container.
During this phase the JSP class is initialized. Once initialization is over, jspInit() invoked.
_jspService() method invoked when request for the jsp page is received.
Destroy() is the last phase and this method is called when the container decides to unload JSP from memory.
JSP Scripting elements
The scripting elements are used to provides the ability to insert java code inside the jsp. There are three types of scripting
elements:
1. Scriptlet tag
2. expression tag
3. declaration tag
Syntax:
<% java source code %>
Example1:
<html>
<body>
<% out.print("Hello"); %>
</body>
</html>
Example2:
<html>
<body>
<form action="hello.jsp">
<input type="text" name="uname">
<input type="submit" value=“Click"><br/>
</form>
</body>
</html>
hello.jsp
<html>
<body>
<%
String name=request.getParameter("uname");
out.print("Hello "+name);
%>
</body>
</html>
2. JSP expression tag
Expression tag is mainly used to print the values of variable or method.
We do not need to write out.print() to write data.
The expression element can have any valid Java expression without a semicolon at the end.
Syntax of JSP expression tag
<%= statement %>
Example1:
<html>
<body>
<%= "Hello" %>
</body>
</html>
Example2:
<html>
<body>
<form action="hello.jsp">
<input type="text" name="uname"><br/>
<input type="submit" value=“click">
</form>
</body>
</html>
hello.jsp
<html>
<body>
<%= "hello "+request.getParameter("uname") %>
</body>
</html>
3. JSP Declaration Tag
The JSP declaration tag is used to declare fields and methods.
A declaration declares one or more variables or methods that you can use in Java code later in the JSP file.
Syntax of JSP declaration tag
<%! field or method declaration %>
Example1: Example2:
<html> <html>
<body> <body>
<%! <%!
int age=25; int add(int a, int b){
%> return a+b
<%= "Age: " +age %> }
</body> %>
</html> <%= "Addition is:"+add(2,4) %>
</body>
</html>
JSP Implicit Objects
These objects are created by JSP container while translating the JSP page to Servlet. These objects are present inside service
methods so we can directly use them without declaration.
1) JSP out implicit object
For writing any data to the buffer, JSP provides an implicit object named out. It is the object of JspWriter.
Eg: <html>
<body>
<% out.print("Hello”); %>
</body>
</html>
welcome.jsp
<html>
<html>
<body>
<body>
<form action="welcome.jsp">
<%
<input type="text" name="uname">
String name=request.getParameter("uname");
<input type="submit" value="go"><br/>
out.print("Welcome "+name);
</form>
session.setAttribute("user",name);
</body>
<a href="second.jsp">second jsp page</a>
</html> %>
</body>
</html>
second.jsp
<html>
<body>
<%
String name=(String)session.getAttribute("user");
out.print("Hello "+name);
%>
</body>
</html>
Assignment
Directives are basically used to configure the code that is generated by container in a servlet.
As a part of JSP we have three types of directives; they are page directives, include directives and taglib directives.
The page directive defines attributes that apply to an entire JSP page.
Whenever we use page directive as a part of JSP program that statement must be the first statement.
Import
The import attribute is used to import class, interface or all the members of a package.
When we write 'n' number of JSP pages, there is a possibility of occurring exceptions in each and every JSP page. It is not
recommended for the JSP programmer to write try and catch blocks in each and every JSP page. It is always recommended
to handle all the exceptions in a single JSP page.
Example:
isErrorPage
The isErrorPage attribute is used to declare that the current page is the error page.
isErrorPage is an attribute whose default value is true which indicates exceptions to be processed in the same JSP page.
If isErrorPage is false then exceptions are not processed as a part of current JSP page and the exceptions are processed in
some other JSP page which will be specified through an attribute called errorPage.
Example:
<%@ page isErrorPage=“false” errorPage="myerrorpage.jsp" %>
or
<%@ page isErrorPage=“true” %>
2. Jsp Include Directive
Include is the directive used to include the resource. The resource can be either an JSP or a servlet. If we include html file,
it will be executed by browser when the response is rendering to the client. When we include a JSP or a servlet, it will be
executed by container.
Example:
<body>
<%@ include file=“menu.html" %>
</body>
3. Jsp TaglibDirective
The JSP taglib directive is used to define a tag library that defines many tags.
We use the TLD (Tag Library Descriptor) file to define the tags.
Example: <%@ taglib uri=“WEB-INF/mytags.tld" prefix=“a" %>
Here you have to create three files. First one is servlet(TagHandler class) which will extend the TagSupport class and override
its method dostartTag(). After that create TLD file that contains the information of tag and the TagHandler class. It must be
contained inside the WEB-INF directory. Atlast use the tag in our jsp file. Here, we are specifying the path of tld file directly.
What is MVC?
MVC is an architecture that separates business logic, presentation and data. In MVC,
Model represents the state of the application i.e. data. It can also have
business logic.
request.setAttribute(“mybean",obj);
RequestDispatcher rd=request.getRequestDispatcher("login_success.jsp");
rd.forward(request, response); }
Login Bean
Login_success.jsp
public class LoginBean {
<body>
private String email, password;
<%
public String getEmail() {
TestBean mca=(TestBean)request.getAttribute("gurubean");
return email;
}
out.print("Welcome, "+ mca.getEmail());
public void setEmail(String email) {
%>
this.email = email;
</body>
}
}
Cookies
A cookie is a small piece of information that is persisted between the multiple client requests.
In cookies technique, we add cookie with response from the servlet. So cookie is stored in the cache of the browser.
After that if request is sent by the user, cookie is added with request by default. Thus, we recognize the user as the old
user.
Cookies are maintained at client side. It will not work if cookie is disabled from the browser. Only textual information
can be set in Cookie object.
Types of Cookie
Non-persistent cookie
It is valid for single session only. It is removed each time when user closes the browser.
Persistent cookie
It is valid for multiple session. It is not removed each time when user closes the browser. It is removed only if user logout or
signout.