Exception Handling using JSTL tag….

Here in this post we will learn how to handle Exception in jsp page using JSTL tags

Here is the sample code for this app…

index.jsp

<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<html>
<body>
<strong>Exception Handling Example</strong><br>
<a href="<c:url value='Check.jsp' />" >Go to Exception Page</a><br>
<br>
<%-- The above line is similar to response.encodeURL("url") method of servlet --%>
</body>
</html>

when we click on Go to Exception Page it will send u too Check.jsp page contain the following code:
check.jsp

<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>

<html>
<body>
<strong>Exception Handling Example</strong>
<br>
<c:catch var=”myexception” ><%—This is similar to try { }catch{} of normal exception handling –-%>
<strong>Inside the Catch Block : >></strong>
<br>
<br>
<% int x = 10/0; %>
</c:catch>
<%– the <c:chosoe> , c:when and c:otherwise is equivalant to if {}else {} statement !! thanx –%>

<c:choose>
<c:when test=”${myexception != null}” >
There was an Exception : ${myexception.message}<br><br>
</c:when>
<c:otherwise >
No Problem thanks!!
</c:otherwise >
</c:choose>
</body>
</html>

——————————————————–
at int x=10/0; there is devide by zero exception which is catch by the above given block

the running example can be found here

Handling 404 with exception handling in JSP

Here we are going to discuss simple way to handling 404 page Not found error and exception handling in jsp :

here is the directory structure of this simple app :

1

just download this app from here and call some page like xyz.jsp the it will show u the output like this :

Untitled

what happening inside the app is all about this code :

web.xml

<web-app>

<error-page>
<error-code>404</error-code>
<location>/notFound.jsp</location>
</error-page>

</web-app>

when we request for some page which is not inside the web app then the container automatically detects that by reading the above web.xml code and redirect request to the given page which is notFound.jsp as given above. so u can create your own custom not found page and use it in your web applicatin!!.

the second part of this app is to handle some exception in jsp. here in the above given dierctory struc. error.jsp page has some exception :

error.jsp

<%@ page errorPage=”errorhandler.jsp” %><%– errorhandler.jsp  page will Handle the Error found in this page –%>
<html>
<body>
<strong>Welcome to web App Test</strong>
<% int x = 10/0; %><%– here is the exp. –%>
</body>
</html>

 

this could also done by configuring in web.xml for example if a page has java.lang.ArithmeticException then it could be handled by adding this code in web.xml:

<error-page>
<exception-type>java.lang.ArithmeticException</exception-type>
<location>/notFound1.jsp</location><!– this page will be handle the the exception –>
</error-page>

 

use it in your application and enjoy @@. feel free to ask if you get any problem….

J2EE Application Into The Cloud . . .

Hi Friends,

Here we quickly find out how to deploy our j2ee application into the cloud by just following some steps:

Step 1: First You will need to select your cloud service Provider and create an account.

There are several Cloud Providers who provide a limited capable account for free

and those are :
Open Shift (from Red Hat Inc.)

CloudFoundry (from Vmware Inc. )

Cloudifysources

etc

Step 2. Create your web app using the technologies provide/supported by the cloud provider.

Here we are using OpenShift for J2EE apps. When using eclipse IDE  then cont.

 

Sorry @@ this topic will be continue later due to some other project work i am busy there @@