• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

URGENT: Alerts and JSP

 
Mrigasira Srinivas
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 13058
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 3252
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 98
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
try this
try{
// jsp code
}catch Exception e
{
out.println("<script>alert(\"error" + e.printStackTrace()
+ "\); </script>");
}
-manav
 
Manoj Karnavat
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic