aspose file tools*
The moose likes Servlets and the fly likes URGENT: Alerts and JSP Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Servlets
Bookmark "URGENT: Alerts and JSP" Watch "URGENT: Alerts and JSP" New topic
Author

URGENT: Alerts and JSP

Mrigasira Srinivas
Greenhorn

Joined: Feb 28, 2001
Posts: 17
Hi All
I am using EJB to connect to SAP through an intermediate interface of Java classes. All the exceptions raised at the various stages, I am having them thrown back to the JSP. I need to catch the exceptions in my JSP and pop it up as an alert. How do I do it?
Thanks in advance
Yamini
William Brogden
Author and all-around good cowpoke
Rancher

Joined: Mar 22, 2000
Posts: 12769
    
    5
Seems to me you have two choices, neither of which will be able to pop up an alert on the user's browser:
1. Use explicit try/catch structures in your JSP
This has the advantage that you can create an error message using the current value of variables.
2. Designate an errorPage for your JSP and handle the exception there.
Both of these are going to write a normal HTML page to the browser.
Bill

------------------
author of:
Peter den Haan
author
Ranch Hand

Joined: Apr 20, 2000
Posts: 3252
This is easiest if you use an MVC (Model 2) architecture. The exceptions are then likely to be thrown in the controller servlet (or command objects executed by the controller servlet). Since you are not generating the response there (yet), you can still forward the request anywhere at this stage.
Catch the exceptions in your controller. Use them to populate some place where you store errors, let's say you have a dedicated request- or session-scoped error JavaBean. If any exceptions have been thrown, forward the request back to the JSP the user came from.
At the start of this JSP, you have a few lines of code checking if there are any messages in the error bean. If there are, you generate a bit of JavaScript in the body onLoad() which causes an alert to pop up. You could use tag libraries, if you wanted, to sugar-coat all of this.
To the user, the effect is that of an error message popping up on the screen (s)he tried to submit.
- Peter

[This message has been edited by Peter den Haan (edited May 07, 2001).]
manav kher
Ranch Hand

Joined: Nov 21, 2000
Posts: 98
try this
try{
// jsp code
}catch Exception e
{
out.println("<script>alert(\"error" + e.printStackTrace()
+ "\); </script>");
}
-manav
Manoj Karnavat
Greenhorn

Joined: May 08, 2001
Posts: 3
Originally posted by Yamini Krishnamurthi:
Hi All
I am using EJB to connect to SAP through an intermediate interface of Java classes. All the exceptions raised at the various stages, I am having them thrown back to the JSP. I need to catch the exceptions in my JSP and pop it up as an alert. How do I do it?
Thanks in advance
Yamini


hi yamini,
i have a code fragment which i hope will satisfy your requirements....

<%@ page language="java" import="java.sql.*" %>
<html>
<body>
<%
try
{
throw new Exception();
}
catch(Exception e)
{
%>
<script lanugage=javascript>
alert("Exception");
</script>
<%
}
%>
</body>
</html>

this is a jsp code which has a try-catch block. the try block throws an exception which is caught and a alert box is displayed. execute this code and check if you get what u want...
bye
Mnk
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: URGENT: Alerts and JSP