Implicit Objects in JSP
II. Implicit objects in JSP
There are 9 implicit (built-in) Objects available in JSP. All are used to perform different type of tasks in a web application. Such as receiving request from client, sending response to client. Printing messages on pages, maintaining sessions, setting different types of parameters at page and application level, handling exceptions and so on.
The below given tables shows a list of Implicit Objects, their class type and usage.
JSP Home
*** implicit object should not be used inside the declaration
*** implicit object should be used in scriptlet and expression
reuestdemo.jsp
<!DOCTYPE html>
<html>
<head>
<title>Codingzon JSP Demos</title>
</head>
<body>
<%! String str = null; %>
<%
str = request.getParameter("username");
out.println("<br>Hello "+ str); %>
</body>
</html>
URL:
Use the following URL to open in Browser, and check the output.
http://localhost:8080/reuestdemo.jsp?username=abc
request.getParameter("param Name") → reads the given parameter name, that is passed through URL or from HTML page, and returns its value.
In the above program using request object reading the username parameters and storing its value in a string variable called str.
In the next line str is printed with some greeting message.
The given image shows the output.
form.html
<!DOCTYPE html>
<html>
<head>
<title>Codingzon JSP Demos</title>
</head>
<body>
<form action="responsedemo.jsp">
<input type="text" name="uname">
<input type="submit" value="go"><br/>
</form>
</body>
</html>
responsedemo.jsp
<!DOCTYPE html>
<html>
<head>
<title>Codingzon JSP Demos</title>
</head>
<body>
<%
response.sendRedirect("http://www.google.com");
%>
example2:
Reading form data in JSP and printing result.
add.jsp
<html>
<body>
<form method="get" action="add.jsp">
<input type=text name="t1">
<input type=text name="t2">
<input type=submit name="sub">
</form>
</body>
</html>
<% int a=Integer.parseInt(request.getParameter("t1"));
int b=Integer.parseInt(request.getParameter("t2"));
int c=0;
c=a+b;
out.println("c="+c);
%>
</body>
</html>
out Object
It is the PrintWriter object and used to send output to the client.
session Object
It is the HttpSession object and associated with the request.
application
This is the ServletContext object and holds the information about the application context.
config
This is the ServletConfig object associated with the page object.
page
This is simply a synonym for this, and is used to call the methods defined by the translated servlet class.
PageContext:
It is an instance of a javax.servlet.jsp.PageContext class.
The pageContext object represent the entire JSP page.
with this object you can read the following details
1.Read Request Parameter
2.Read Config Parameter Servlet content
ex: config.getServletName(); prints the servletname.
3.Read Context Parameter ServletConfig
4.write to response HttpResponse
pageContext example
it is ServletContext object
setting and reading page level varibales.
pageContext.setAttribute("name", "abc"); out.println(pageContext.getAttribute("name")); <html> <body bgcolor="yellow"> <% String s = request.getParameter("text1"); out.println("we welcome"+<br>); out.println(s); %> </body> </html> in web.xml ----------
...
<context_param>
<pname>init</pname>
<pvalue>xxx</pvalue>
</context_param>
...
exception Object:
This Object holds the exception thrown from the previous page. It is generally used to produce an appropriate response to the error condition. We will learn in details later in this tutorial.
LifeCycle Methods of JSP
There are 3 methods available .
- jsp_Init(): At the time of JSP initialization, this method get called.
- Jsp_Service() : this method represents the body of a JSP page. It is an inbuilt method.
- JspDestroy(): this method get called only once by JSP container.
Scopes in Jsp
- Page
- Request
- Session
- Application
Page Scope:
Data that is declared in page, available only within the page.
Request Scope:
The availability of data in this is during one request and response side data will not be available in next request, i.e available only in one cycle.
Session scope:
Session data is available for multiple requests until session expires. Session is only for one user or user has only one session and this session can have the number of attributes. ex: sessionid, username etc.
application scope:
variables accessible with the application context
Comments
Post a Comment