Unit-6-JSP 241015 231449
Unit-6-JSP 241015 231449
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
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.
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
<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>