Lecture 3.4
Lecture 3.4
DEPARTMENT : CSE
Bachelor of Engineering (Computer Science
& Engineering)
PROJECT BASED LEARNING IN JAVA WITH LAB
(22CSH-359/22ITH-359)
TOPIC OF PRESENTATION:
JSP declaration, JSP directives, JSP
Scriptlets (CO 5)
DISCOVER . LEARN .
EMPOWER
Lecture Objectives
2
What is JSP?
JSP lies in the presentation tier on the web server, with the main
responsibility of generating HTML content that needs to be
served to the browser. It also has the additional responsibility
of pass on the requests to the backend through the JavaBeans,
as and when required.
Why JSP?
1. High performance
2. Convenient to code and maintain as against servlets
3. Separates presentation from content
4. Powered by Java
– Has access to all Java APIs
– Has access to all J2EE APIsa
– Inherent Platform independence
Creating JSP in Eclipse IDE with Tomcat server
Syntax is as follows:
<% java source code %>
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 %>
There are 9 jsp implicit objects. These objects are created by the web
container that are available to all the jsp pages.
The available implicit objects are out, request, config, session, application
etc.
A list of the 9 implicit objects is given below:
Object Type
out JspWriter
request HttpServletRequest
response HttpServletResponse
config ServletConfig
application ServletContext
session HttpSession
pageContext PageContext
page Object
exception Throwable
JSP out implicit object
For writing any data to the buffer, JSP provides an implicit object named out. It is the
object of JspWriter.
This object can be used to get initialization parameter for a particular JSP page. The
config object is created by the web container for each jsp page.
The instance of ServletContext is created only once by the web container when
application or project is deployed on the server.
This object can be used to get initialization parameter from configuaration file
(web.xml). It can also be used to get, set or remove attribute from the application
scope.
Index.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/>
</form> out.print("Welcome "+name);
</body> session.setAttribute("user",name);
</html> <a href="second.jsp">second jsp page</
a> %>
</body>
</html>
second.jsp
<html>
<body>
<%
String name=(String)session.getAttribute("user");
out.print("Hello "+name);
%>
</body>
</html>
exception implicit object
In JSP, exception is an implicit object of type java.lang. Throwable class. This object can be used
to print the exception. But it can only be used in error pages.
It is better to learn it after page directive. Let's see a simple example:
error.jsp
<%@ page isErrorPage="true" %>
<html>
<body>
</body>
</html>
Example of exception handling in jsp by the elements of page directive
In this case, you must define and create a page to handle the exceptions, as in
the error.jsp page.
The pages where may occur exception, define the errorPage attribute of page
directive, as in the process.jsp page.
The jsp directives are messages that tells the web container how to translate a
JSP page into the corresponding servlet.
The include directive is used to include the contents of any resource it may be
jsp file, html file or text file.
The include directive includes the original content of the included resource at
page translation time (the jsp page is translated only once so it will be better
to include static resource).
Example of Targlib:
• <%@ taglib uri="http://www.sample.com/mycustomlib" prefix="demotag"
%> <html> <body> <demotag:welcome/> </body> </html>As you can see
that uri is having the location of custom tag library and prefix is identifying
the prefix of custom tag.
Note: In above example – <demotag: welcome> has a prefix demotag.
Summary:
Video Lectures :
https://www.youtube.com/watch?v=NQ7xKNULkTk
Reference Links:
https://beginnersbook.com/2013/05/jsp-tutorial-directives/
https://docs.oracle.com/javaee/5/tutorial/doc/bnaou.html
https://beginnersbook.com/2013/05/jsp-tutorial-scriptlets/
https://www.javatpoint.com/jsp-scriptlet-tag
https://beginnersbook.com/2013/05/jsp-tutorial-directives/
https://beginnersbook.com/2013/11/jsp-declaration-tag/