web b-2 ch-3
web b-2 ch-3
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:
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.
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
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
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 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().