0% found this document useful (0 votes)
2 views12 pages

Unit-6-JSP 241015 231449

Uploaded by

kalyanipawar863
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views12 pages

Unit-6-JSP 241015 231449

Uploaded by

kalyanipawar863
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 12

JSP technology is 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.
Advantages of JSP over Servlet
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.
Advantages of JSP over Servlet
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.
The Lifecycle of a 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).
JSP API
The JSP API consists of two packages:
1.javax.servlet.jsp
2.javax.servlet.jsp.tagext

javax.servlet.jsp package
The javax.servlet.jsp package has two interfaces and classes.The two interfaces are as follows:
1. JspPage
2. HttpJspPage

The classes are as follows:


1. JspWriter
2. PageContext
3. JspFactory
4. JspEngineInfo
5. JspException
6. JspError
Methods of JspPage interface
1.public void jspInit(): It is invoked only once during the life cycle of the JSP when JSP page is
requested firstly. It is used to perform initialization. It is same as the init() method of Servlet
interface.
2.public void jspDestroy(): It is invoked only once during the life cycle of the JSP before the JSP
page is destroyed. It can be used to perform some clean up operation.

The HttpJspPage interface


The HttpJspPage interface provides the one life cycle method of JSP. It extends the JspPage
interface.
Method of HttpJspPage interface:
public void _jspService(): It is invoked each time when request for the JSP page comes to the
container. It is used to process the request. The underscore _ signifies that you cannot
override this method.
JSP Scriptlet tag (Scripting elements)

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

JSP scriptlet tag


A scriptlet tag is used to execute java source code in JSP.
Syntax :- <% java source code %>
Example
<html>
<body>
<% out.print(“Hello”); %>
</body>
</html>
Example of JSP scriptlet tag to process request parameter

first.html first.jsp
<html>
<html> <body>
<body> <%
<form action=“first.jsp"> int n=Integer.parseInt(request.getParameter(“n
<input type="text" name=“num"> um"));
<input type="submit" value=“Square"> out.print(“Square is "+n*n);
<br/> %>
</form> </form>
</body> </body>
</html> </html>
JSP expression tag
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 %>

Example
<html>
<body>
<%= "welcome to jsp" %>
</body>
</html>
second.html second.jsp

<html> <html>
<body> <body>
<form action=“second.jsp"> <%= “Hi "+request.getParameter("uname") %>
<input type="text" name="uname">
<br/> </body>
<input type="submit" value=“Submit"> </html>

</form>
</body>
</html>
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
<%! field or method declaration %>
Difference between JSP Scriptlet tag and declaration tag

Jsp Scriptlet Tag Jsp Declaration Tag


The jsp scriptlet tag can only declare variables The jsp declaration tag can declare variables
not methods. as well as methods.
The declaration of scriptlet tag is placed inside The declaration of jsp declaration tag is placed
the _jspService() method. outside the _jspService() method.
Example of JSP declaration tag (to Example of JSP declaration tag (to
declare variable) declare method)

<html> <html>
<body> <body>
<%! int x=50; %> <%! int sum(int n, int m)
<%= "Value of the variable x is:"+x %> {
</body> return n+m;
</html> }
%>
<%= “Sum of 5 and 3 is:"+sum(5,3) %>
</body>
</html>

You might also like