JSP
JSP Implicit Objects
JSP Implicit Objects
There are 9 jsp implicit objects.
These objects are created by the web
container that are available to all the
jsp pages.
A list of the 9 implicit objects is
Object Type
out JspWriter
request HttpServletRequest
response HttpServletResponse
config ServletConfig
application ServletContext
session HttpSession
pageContext PageContext
page Object
exception Throwable
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.
In case of servlet you need to write:
PrintWriter out=response.getWriter();
But in JSP, you don't need to write this code.
Example of out implicit object
Index.jsp
<html>
<body>
<% out.print("Today is:"+java.util.Calendar.getInstance().getTime()); %>
</body>
</html>
JSP request implicit object
The JSP request is an implicit object of type HttpServletRequest i.e. created for each jsp request by
the web container. It can be used to get request information such as parameter, header
information, remote address, server name, server port, content type, character encoding etc.
It can also be used to set, get and remove attributes from the jsp request scope.
index.html
<form action="welcome.jsp">
Example of
JSP request <input type="text" name="uname">
implicit object
<input type="submit" value="go"><br/>
</form>
<%
welcome.jsp String name=request.getParameter("uname");
out.print("welcome "+name);
%>
3) JSP response implicit object
In JSP, response is an implicit
object of type
It can be used to add or
HttpServletResponse. The
manipulate response such as
instance of
redirect response to another
HttpServletResponse is created
resource, send error etc
by the web container for each
jsp request.
Example of response implicit object
index.html
<form action="welcome.jsp">
<input type="text" name="uname">
<input type="submit" value="go"><br/>
</form>
welcome.jsp
• <%
• response.sendRedirect("http://www.google.com");
• %>
4) JSP config implicit object
In JSP, config is an implicit object of
type ServletConfig. 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
6) session implicit object
In JSP, session is an implicit object of type
HttpSession
.The Java developer can use this object to set,get
or remove attribute or to get session information.
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" %>
JSP page directive
The page directive defines attributes that apply to an
entire JSP page.
Syntax of JSP page directive
<%@ page attribute="value" %>
import
contentType
extends
info
Attributes buffer
language
of JSP page isELIgnored
isThreadSafe
directive autoFlush
session
pageEncoding
errorPage
isErrorPage
The import attribute is used
to import class,interface or all
1)import the members of a package.
It is similar to import keyword
in java class or interface.
Example of import attribute
• <html>
• <body>
• <%@ page import="java.util.Date" %>
• Today is: <%= new Date() %>
•
• </body>
• </html>
2)contentType
The contentType attribute
defines the .The default value is
MIME(Multipurpose "text/html;charset=ISO-
Internet Mail Extension) 8859-1".
type of the HTTP response
<html>
<body>
<%@ page contentType=application/msword %>
Example
of contentType attribute Today is: <%= new java.util.Date() %>
</body>
</html>
3)extends
The extends attribute
defines the parent
class that will be It is rarely used.
inherited by the
generated servlet.
4)info
• This attribute simply sets the information of the JSP page which is retrieved
later by using getServletInfo() method of Servlet interface.
• <html>
• <body>
Example of •
• <%@ page info="composed by Sonoo Jaiswal" %>
info attribute • Today is: <%= new java.util.Date() %>
•
• </body>
• </html>
5)buffer
The buffer attribute sets the buffer size in
kilobytes to handle output generated by the
JSP page.
The default size of the buffer is 8Kb.
Example of buffer attribute
<html>
<body>
<%@ page buffer="16kb" %>
Today is: <%= new java.util.Date() %>
</body>
</html>
6)language
The language
attribute specifies the The default value is
scripting language "java".
used in the JSP page.
The errorPage attribute is
used to define the error
page,
errorPage
if exception occurs in the
current page, it will be
redirected to the error page.
<html>
<body>
Example of <%@ page errorPage="myerrorpage.jsp" %>
errorPage <%= 100/0 %>
attribute
</body>
</html>
isErrorPage
The isErrorPage
attribute is used to Note: The exception
declare that the object can only be
current page is the used in the error page.
error page.
• Example of isErrorPage attribute
<html>
<body>
<%@ page isErrorPage="true" %>
Sorry an exception occured!<br/>
The exception is: <%= exception %>
</body>
</html>
Jsp Include Directive
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)
The include directive is used to include the contents of any resource it may
be jsp file, html file or text file.
Advantage Code Reusability
of Include Syntax of include directive
directive <%@ include file="resourceName" %>
• To run this example you must create an header.html file.
• <html>.
• <body>.
•
• <%@ include file="header.html" %>
•
• Today is: <%= java.util.Calendar.getInstance().getTime() %>
Example of •
• </body>
include • </html>
directive • Note: The include directive includes the original content, so
the actual page size grows at runtime.
JSP taglib
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. In the custom
tag section we will use this tag so it will be better to learn it in custom tag.
Syntax JSP Taglib directive
<%@ taglib uri="uriofthetaglibrary" prefix="prefixoftaglibrary" %>
Example of JSP Taglib directive
To use this tag we must specify the taglib directive so the container may get information about the tag.
<html>
<body>
<%@ taglib uri="https://www.google.com//tags" prefix="mytag" %>
<mytag:currentDate/>
</body>
</html>
Exception Handling
The exception is normally an object that is thrown at runtime. Exception Handling is the
process to handle the runtime errors. There may occur exception any time in your web
application. So handling exceptions is a safer side for the web developer.
In JSP, there are two ways to perform exception handling:
By errorPage and isErrorPage attributes of page directive
By <error-page> element in web.xml file
index.jsp
• <form action="process.jsp">
• No1:<input type="text" name="n1" /><br/><br/>
• No1:<input type="text" name="n2" /><br/><br/>
• <input type="submit" value="divide"/>
• </form>
PROCESS.JSP
• <%@ page errorPage="error.jsp" %>
• <%
•
• String num1=request.getParameter("n1");
• String num2=request.getParameter("n2");
•
• int a=Integer.parseInt(num1);
• int b=Integer.parseInt(num2);
• int c=a/b;
• out.print("division of numbers is: "+c);
•
• %>
error.jsp
<%@ page isErrorPage="true" %>
<h3>Sorry an exception occured!</h3>
Exception is: <%= exception %>
Database Connection
• There are the following steps to connect a python application to our
database.
• Import mysql.connector module
• Create the connection object.
• Create the cursor object
• Execute the query
Create database
• Create database sam;
• Use sam;
• CREATE TABLE users
• (
• id int NOT NULL AUTO_INCREMENT,
• first_name varchar(50),
• last_name varchar(50),
• city_name varchar(50),
• email varchar(50),
• PRIMARY KEY (id)
• );
Index.html
• <html>
• <body>
• <form method="post" action="process.jsp">
• First name:<br>
• <input type="text" name="first_name"> <br>
• Last name:<br>
• <input type="text" name="last_name"> <br>
• City name:<br>
• <input type="text" name="city_name"> <br>
• Email Id:<br>
• <input type="email" name="email"> <br><br>
• <input type="submit" value="submit">
• </form>
• </body>
• </html>
Process.jsp
• <%@ page language="java" contentType="text/html; charset=ISO-8859-1"
• pageEncoding="ISO-8859-1"%>
• <%@page import="java.sql.*,java.util.*"%>
• <%
• String first_name=request.getParameter("first_name");
• String last_name=request.getParameter("last_name");
• String city_name=request.getParameter("city_name");
• String email=request.getParameter("email");
• try
• {
• Class.forName("com.mysql.jdbc.Driver");
• Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/test", "root", "");
• Statement st=conn.createStatement();
• int i=st.executeUpdate("insert into users(first_name,last_name,city_name,email)values('"+first_name+"','"+last_name+"','"+city_name+"','"+email+"')");
• out.println("Data is successfully inserted!");
• }
• catch(Exception e)
• {
• System.out.print(e);
• e.printStackTrace();
• }
• %>