• JSP technologyis used to create web application just
like Servlet technology. It can be thought of as an
extension to Servlet because it provides more
functionality than servlet such as expression
language, JSTL, etc.
• 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.
3.
Advantages of JSPover Servlet
• There are many advantages of JSP over the Servlet. They are as follows:
• 1) 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.
• 2) 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.
• 3) 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.
• 4) 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.
4.
The Lifecycle ofa JSP Page
The JSP pages follow these phases:
• Translation of JSP Page
• Compilation of JSP Page
• Classloading (the classloader loads class file)
• Instantiation (Object of the Generated Servlet is created).
• Initialization ( the container invokes jspInit() method).
• Request processing ( the container invokes _jspService()
method).
• Destroy ( the container invokes jspDestroy() method).
5.
• As depictedin the above diagram, 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. Moreover, all the
processes that happen in Servlet are performed on JSP later like initialization, committing response to the
browser and destroy.
6.
Creating a simpleJSP 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.
• <html>
• <body>
• <% out.print(2*5); %>
• </body>
• </html>
7.
How to runa 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
8.
• The Directorystructure 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.
9.
• JSP Scriptlettag (Scripting elements)
• In JSP, java code can be written inside the jsp page using the
scriptlet tag. Let's see what are the scripting elements first.
• JSP Scripting elements
• The scripting elements provides the ability to insert java
code inside the jsp. There are three types of scripting
elements:
• scriptlet tag
• expression tag
• declaration tag
10.
• JSP scriptlettag
• A scriptlet tag is used to execute java source code in JSP. Syntax is
as follows:
• <% java source code %>
• Example of JSP scriptlet tag
• In this example, we are displaying a welcome message.
• <html>
• <body>
• <% out.print("welcome to jsp"); %>
• </body>
• </html>
11.
Example of JSPscriptlet tag that prints the user name
In this example, we have created two files index.html and welcome.jsp. The index.html file gets the
username from the user and the welcome.jsp file prints the username with the welcome message.
File: index.html
<html>
<body>
<form action="welcome.jsp">
<input type="text" name="uname">
<input type="submit" value="go"><br/>
</form>
</body>
</html>
File: welcome.jsp
<html>
<body>
<%
String name=request.getParameter("uname");
out.print("welcome "+name);
%>
</form>
</body>
</html>
12.
• JSP expressiontag
• The code placed within JSP expression tag is
written to the output stream of the response.
So you need not write out.print() to write
data. It is mainly used to print the values of
variable or method.
• Syntax of JSP expression tag
• <%= statement %>
13.
• Example ofJSP expression tag
• In this example of jsp expression tag, we are
simply displaying a welcome message.
• <html>
• <body>
• <%= "welcome to jsp" %>
• </body>
• </html>
14.
• Example ofJSP expression tag that prints current time
• To display the current time, we have used the getTime() method
of Calendar class. The getTime() is an instance method of
Calendar class, so we have called it after getting the instance of
Calendar class by the getInstance() method.
• index.jsp
• <html>
• <body>
• Current Time: <%= java.util.Calendar.getInstance().getTime() %>
• </body>
• </html>
15.
• Example ofJSP expression tag that prints the user name
• In this example, we are printing the username using the expression tag. The index.html file gets the
username and sends the request to the welcome.jsp file, which displays the username.
• File: index.jsp
• <html>
• <body>
• <form action="welcome.jsp">
• <input type="text" name="uname"><br/>
• <input type="submit" value="go">
• </form>
• </body>
• </html>
• File: welcome.jsp
• <html>
• <body>
• <%= "Welcome "+request.getParameter("uname") %>
• </body>
• </html>
16.
JSP Declaration Tag
•The JSP declaration tag is used to declare fields and
methods.
• The code written inside the jsp declaration tag is placed
outside the service() method of auto generated servlet.
• So it doesn't get memory at each request.
• Syntax of JSP declaration tag
• The syntax of the declaration tag is as follows:
• <%! field or method declaration %>
17.
• Example ofJSP declaration tag that declares field
• In this example of JSP declaration tag, we are declaring the field
and printing the value of the declared field using the jsp
expression tag.
• index.jsp
• <html>
• <body>
• <%! int data=50; %>
• <%= "Value of the variable is:"+data %>
• </body>
• </html>
18.
• Example ofJSP declaration tag that declares method
• In this example of JSP declaration tag, we are defining the method which returns the
cube of given number and calling this method from the jsp expression tag. But we can
also use jsp scriptlet tag to call the declared method.
• index.jsp
• <html>
• <body>
• <%!
• int cube(int n){
• return n*n*n*;
• }
• %>
• <%= "Cube of 3 is:"+cube(3) %>
• </body>
• </html>
19.
• JSP directives
•The jsp directives are messages that tells the web
container how to translate a JSP page into the
corresponding servlet.
• There are three types of directives:
– page directive
– include directive
– taglib directive
Syntax of JSP Directive
<%@ directive attribute="value" %>
20.
• SP pagedirective
• The page directive defines attributes that
apply to an entire JSP page.
• Syntax of JSP page directive
• <%@ page attribute="value" %>