2

I have a JSP named process.jsp.Upon clicking on a command in my application a popup will open with some data to select ,after I select the data and click on submit ,procees.jsp will be called where I have to process the data and store to database.In process.jsp,I have try catch block.suppose if any exception is thrown in try block it should be caught in catch block and an alert message should be displayed as "processing failed" and parent window should be refreshed after closing the popup.the code looks like below

<%
try{
int a=1/0;
}catch(Exception ex){
ex.printStackTrace();
%>
<script>alert("process fialed");
window.close();
parent.loaction.href=parent.loaction.href;
</script>

<%
}%>

I tried with above code,but none of the script code lines got executed.Can any one help me out. Thanks in advance

Natan Streppel
  • 5,759
  • 6
  • 35
  • 43
Praveen
  • 201
  • 5
  • 18

2 Answers2

0

the very first thing

parent.loaction.href=parent.loaction.href; //what is loaction ???? its location

and 2nd

what you wanted to do is to call an alert in your catch when an exception occurs what i suggest is to don't use this ! simply because mixing them will not solve your purpose which you actually wanted to do.

what you doing is mixing a Programming language & a scripting language the difference between them is clear scripting languages are run on client side while programming languages are complied and desired results are calculated after the logic is build.

if you wanted to call java methods using js you can use DWR but don't do this

to know more refer

https://softwareengineering.stackexchange.com/questions/46137/what-is-the-main-difference-between-scripting-languages-and-programming-language

Scripting Language vs Programming Language

also to catch an exception and show to user you can directly display in a div element.or show or hide that div when it happens

Community
  • 1
  • 1
Divya
  • 1,469
  • 1
  • 13
  • 25
0

I think you might be misunderstanding the relationship between the Java code in your JSP and the Javascript/HTML of the page. In any case I think this could solve what you're trying to do:

<html>
<body style="margin:0; padding:0">
    <%
    String errorMsg = null;
    try {
        // do something
        throw new Exception("This is an error");
    } catch (Exception e) {
        errorMsg = e.getMessage();
    }
    if (errorMsg != null) {
        %>
        <script>alert('<%=errorMsg%>');</script>
        <%
    }   
    %>
</body>
</html>
grizznant
  • 1
  • 1