0% found this document useful (0 votes)
2 views

web b-2 ch-3

Uploaded by

ag298744
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 views

web b-2 ch-3

Uploaded by

ag298744
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/ 8

Sumaya academy

G61 near walia nursing home opposite metro pillar 40


Laxminagar delhi-92
8826767298,8826764330

exception implicit object


In JSP, exception is an implicit object of type java.lang.Throwable class. This object can be used to print the
error pages.It is better to learn it after page directive. Let's see a simple example:

Example of exception implicit object:


error.jsp
1. <%@ page isErrorPage="true" %>
2. <html>
3. <body>
4.
5. Sorry following exception occured:<%= exception %>
6.
7. </body>
8. </html>

Exception Handling in JSP


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:

1. By errorPage and isErrorPage attributes of page directive


2. By <error-page> element in web.xml file

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.

There are 3 files:

o index.jsp for input values


o process.jsp for dividing the two numbers and displaying the result
Sumaya academy
G61 near walia nursing home opposite metro pillar 40
Laxminagar delhi-92
8826767298,8826764330

o error.jsp for handling the exception

index.jsp
1. <form action="process.jsp">
2. No1:<input type="text" name="n1" /><br/><br/>
3. No1:<input type="text" name="n2" /><br/><br/>
4. <input type="submit" value="divide"/>
5. </form>
process.jsp
1. <%@ page errorPage="error.jsp" %>
2. <%
3.
4. String num1=request.getParameter("n1");
5. String num2=request.getParameter("n2");
6.
7. int a=Integer.parseInt(num1);
8. int b=Integer.parseInt(num2);
9. int c=a/b;
10. out.print("division of numbers is: "+c);
11.
12. %>
error.jsp
1. <%@ page isErrorPage="true" %>
2.
3. <h3>Sorry an exception occured!</h3>
4.
5. Exception is: <%= exception %>
Example of exception handling in jsp by specifying the
error-page element in web.xml file
This approach is better because you don't need to specify the errorPage attribute in
each jsp page. Specifying the single entry in the web.xml file will handle the exception.
In this case, either specify exception-type or error-code with the location element. If
you want to handle all the exception, you will have to specify the java.lang.Exception
in the exception-type element. Let's see the simple example:

There are 4 files:


Sumaya academy
G61 near walia nursing home opposite metro pillar 40
Laxminagar delhi-92
8826767298,8826764330

o web.xml file for specifying the error-page element


o index.jsp for input values
o process.jsp for dividing the two numbers and displaying the result
o error.jsp for displaying the exception

1) web.xml file if you want to handle any exception


1. <web-app>
2.
3. <error-page>
4. <exception-type>java.lang.Exception</exception-type>
5. <location>/error.jsp</location>
6. </error-page>
7.
8. </web-app>

This approach is better if you want to handle any exception. If you know any specific
error code and you want to handle that exception, specify the error-code element
instead of exception-type as given below:

1) web.xml file if you want to handle the exception for a specific error code
1. <web-app>
2.
3. <error-page>
4. <error-code>500</error-code>
5. <location>/error.jsp</location>
6. </error-page>
7.
8. </web-app>
2) index.jsp file is same as in the above example
3) process.jsp
Now, you don't need to specify the errorPage attribute of page directive in the jsp page.

1. <%@ page errorPage="error.jsp" %>


2. <%
3.
4. String num1=request.getParameter("n1");
5. String num2=request.getParameter("n2");
Sumaya academy
G61 near walia nursing home opposite metro pillar 40
Laxminagar delhi-92
8826767298,8826764330

6.
7. int a=Integer.parseInt(num1);
8. int b=Integer.parseInt(num2);
9. int c=a/b;
10. out.print("division of numbers is: "+c);
11.
12. %>
4) error.jsp file is same as in the above example
Sumaya academy
G61 near walia nursing home opposite metro pillar 40
Laxminagar delhi-92
8826767298,8826764330
Sumaya academy
G61 near walia nursing home opposite metro pillar 40
Laxminagar delhi-92
8826767298,8826764330

JSP - Session Tracking


HTTP is a "stateless" protocol which means each time a client retrieves a Webpage,
the client opens a separate connection to the Web server and the server
automatically does not keep any record of previous client request.

Maintaining Session Between Web Client And Server


Let us now discuss a few options to maintain the session between the Web Client
and the Web Server −

Cookies
A webserver can assign a unique session ID as a cookie to each web client and for
subsequent requests from the client they can be recognized using the received
cookie.
This may not be an effective way as the browser at times does not support a cookie.
It is not recommended to use this procedure to maintain the sessions.
Sumaya academy
G61 near walia nursing home opposite metro pillar 40
Laxminagar delhi-92
8826767298,8826764330

Hidden Form Fields


A web server can send a hidden HTML form field along with a unique session ID as
follows −
<input type = "hidden" name = "sessionid" value = "12345">
This entry means that, when the form is submitted, the specified name and value are
automatically included in the GET or the POST data. Each time the web browser
sends the request back, the session_id value can be used to keep the track of
different web browsers.
This can be an effective way of keeping track of the session but clicking on a regular
(<A HREF...>) hypertext link does not result in a form submission, so hidden form
fields also cannot support general session tracking.

URL Rewriting
You can append some extra data at the end of each URL. This data identifies the
session; the server can associate that session identifier with the data it has stored
about that session.
For example, with http://tutorialspoint.com/file.htm;sessionid=12345, the session
identifier is attached as sessionid = 12345 which can be accessed at the web server
to identify the client.
URL rewriting is a better way to maintain sessions and works for the browsers when
they don't support cookies. The drawback here is that you will have to generate every
URL dynamically to assign a session ID though page is a simple static HTML page.

The session Object


Apart from the above mentioned options, JSP makes use of the servlet provided
HttpSession Interface. This interface provides a way to identify a user across.

• a one page request or


• visit to a website or
• store information about that user
By default, JSPs have session tracking enabled and a new HttpSession object is
instantiated for each new client automatically. Disabling session tracking requires
explicitly turning it off by setting the page directive session attribute to false as
follows −
<%@ page session = "false" %>
Sumaya academy
G61 near walia nursing home opposite metro pillar 40
Laxminagar delhi-92
8826767298,8826764330

The JSP engine exposes the HttpSession object to the JSP author through the
implicit session object. Since session object is already provided to the JSP
programmer, the programmer can immediately begin storing and retrieving data from
the object without any initialization or getSession().

You might also like