[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 540
  • Last Modified:

Exception Handling (error page)

Hello -

If the URL ID of the jsp page is incorrect, for example - http://10.21.163.179:8080/iPadMyNewsShareClip/index.jsp?id=26864........
Is there JSP code that can catch that exception or error and automatically redirects the webpage to errorPage.jsp?

What I have in the main.jsp header is <%@ page errorPage="errorPage.jsp" %>

What I have in the errorPage.jsp header is:

<%@ page isErrorPage="true" %>
<%@ page import = "java.io.*" %>
<%@ page import = "java.util.*" %>

and in the body tags -

<%@ page isErrorPage="true" %>
  <% out.print("Error!!!");  
out.print(exception.getMessage()); %>

<%= exception.toString() %>



Whats the correct approach in catching this type of exception or error?
0
RobWellsMc
Asked:
RobWellsMc
  • 15
  • 14
  • 2
4 Solutions
 
for_yanCommented:

And where do you have this inorrect jsp URL - where does it come from?
If the url to your jsp is incorrect, then no matter whwt you write there
it will not be useful, as this page itself will not be reached.
0
 
for_yanCommented:
If you have it somewhere in your code you can catch
URL MalformedException and take some action, but
if it is the address of your initial page  then they would not even reach this page.
On the other hand in your code - where is the URL coming from?
0
 
RobWellsMcAuthor Commented:
I'm not 100% of the correct address or string - the videos are coming from this site -  
(external link)
https://mynews.xfinitysearch.com:8443/mynews/admin/
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
for_yanCommented:


This page shows how to redirect to another JSP in general:
http://stackoverflow.com/questions/2361559/handling-response-redirect-errors-in-jsp-page


one.jsp  
---------  
<%@ page errorPage="exceptionHandler.jsp" %>  
<%


String webBrowserURL="http://URL"  

response.sendRedirect(webBrowserURL);  

%>  


exceptionHandler.jsp  
------------------------  

<%@ page isErrorPage="true" %>  
<% String root = request.getContextPath(); %>  

                <%  
                         //get the correct url form the database and send it to the page  
                %>  


<jsp:forward page="one.jsp" />  

Open in new window


However your main problem as I understan is to
discover if the site is up and working
and if it is not then redirect
0
 
for_yanCommented:


look at this page:
http://www.coderanch.com/t/438599/HTML-JavaScript/Check-dead-link-Javascript

They give sound advice here that you check if this site is up using the URLconnection
even before you serve your JSP which should include this link
and if it is not up don't inlcude this link on your JSP

Seems reasonable approach to me
0
 
RobWellsMcAuthor Commented:
yan,

This doesn't quite solve the URL issue....

This code below automatically redirects the main page to the errorPage.jsp page:

<%
int i = 10;

// This line will create an error so error page will be called
i = i / 0;
%>


Is there similar JSP code that tests to see if the URL returns error code and automatically *creates an error* so error page will be called ??
0
 
for_yanCommented:


It is not a prtoblem to check URL string formally - just from the point of view of URL format -
you just try to create the new URL(urlString) and catch the URLMalformedException
but thsi is probably not what you want.
You want to know if that page really responds or not, even though it may have valid form
but the site may not be running?

So the suggestion in this situation (see my previous post) is to tcheck
if the link is working   even before you send the JSP page
and include the link on your JSP page - so
you make URLConnection and try to get the response.
If it does not respond generrate JSP page
without this bad link, or say with another link which works.
If it responsa OK, then put the link on your JSP page.

Thsi strategy makes  sense to me
0
 
for_yanCommented:
When you have a link on your JSP page, if that link has a valid format
but is not accessible, there cannot be any error until
user clicks that link.
So you cannot do it like with i/0
That's why I suggest to check it upfront if the link is avaialble
and then generate on the fly or maybe point to appropriate variant of
  pages pre-created for different situations, depending on the outcomw of this
preliminary connection check.
0
 
RobWellsMcAuthor Commented:
I don't quite get 100% of what you mean, what you said above....

Lets see, for example, one.jsp (example above),
for example, should it be modified to have the URL from the video streaming site:
 
<%


String webBrowserURL="https://mynews.xfinitysearch.com:8443/mynews/admin/"  

response.sendRedirect(webBrowserURL);  

%>  


What's the right way to do this correctly?
0
 
for_yanCommented:
No you see that example was showing how to redirect, but it was not
showing how to determine when you want to redirect - and I guess, thuis is your main issue.

Why would you have this link there on your page if you do't want user to go there.
Still, please, read about suf=ggestion to check connection before and explain me if that for some reason
is not waht you want.

0
 
RobWellsMcAuthor Commented:
Yan,

Okay, thanks, I'll research this and keep you posted....
0
 
for_yanCommented:
Look also at discusion at that link at coderanch
http://www.coderanch.com/t/438599/HTML-JavaScript/Check-dead-link-Javascript

they had some other ideas athere - about Ajax, etc, but this one  - to test it prior
to puting it on your page seems the most straightforward and reasonable to me
0
 
RobWellsMcAuthor Commented:
Yan -

By the way, if you can provide me an example of what you might do - that would be great.

Otherwise, no worries and have a great weekend.  I'll catch up with you on Monday with what I have come up with......
0
 
for_yanCommented:
You mean how to check if the site is up and working?
0
 
objectsCommented:
How do you know if the id is invalid?
0
 
RobWellsMcAuthor Commented:
I manually change id info based on what video streams are currently available from the MSNBC library.  

IDs for video streams expire - so when an ID expires, the page is supposed to return the errorPage.jsp content.
0
 
RobWellsMcAuthor Commented:
For example, on my tomcat server, if I have the URL with full id (268673), then it works.

http://10.21.163.179:8080/iPadMyNewsShareClip/index.jsp?id=268673

But if I trunciate the id from 268673 to 2686  then I get this below (instead of to errorPage.jsp):



type Exception report

message

description The server encountered an internal error () that prevented it from fulfilling this request.

exception

org.apache.jasper.JasperException: Unable to compile class for JSP:

An error occurred at line: 77 in the jsp file: /jsp/main.jsp
exception cannot be resolved
74:          
75: </head>
76: <body>
77: <%= exception.toString() %>
78: <div id="layout_xfinityLogo" style="display:inline-block; height:100%; width:960px; margin:0px auto;">       
79:  
80:   <img src="images/layout_masthead.png"  style="height:40px; width:960px;" >


Stacktrace:
      org.apache.jasper.compiler.DefaultErrorHandler.javacError(DefaultErrorHandler.java:95)
      org.apache.jasper.compiler.ErrorDispatcher.javacError(ErrorDispatcher.java:330)
      org.apache.jasper.compiler.JDTCompiler.generateClass(JDTCompiler.java:457)
      org.apache.jasper.compiler.Compiler.compile(Compiler.java:374)
      org.apache.jasper.compiler.Compiler.compile(Compiler.java:352)
      org.apache.jasper.compiler.Compiler.compile(Compiler.java:339)
      org.apache.jasper.JspCompilationContext.compile(JspCompilationContext.java:594)
      org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:344)
      org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:391)
      org.apache.jasper.servlet.JspServlet.service(JspServlet.java:334)
      javax.servlet.http.HttpServlet.service(HttpServlet.java:722)
      com.streamsage.mynews.services.MainServlet.doGet(MainServlet.java:83)
      javax.servlet.http.HttpServlet.service(HttpServlet.java:621)
      javax.servlet.http.HttpServlet.service(HttpServlet.java:722)

note The full stack trace of the root cause is available in the Apache Tomcat/7.0.11 logs.
0
 
for_yanCommented:

The eaxmple you gave - that you modify the valuie of id -
it cannot diagnose that its is a wrong URL and it is not malformed URL,
so you cannot check it on this formal level.
In order to diagnose this you need to attempt to connect to URL (using URLConnection class, etc,
e.g. http://download.oracle.com/javase/tutorial/networking/urls/connecting.html)
and if you fail to receieve the response then you know that you have either not valid
address of the site  or maybe the site is down.
You probably want to do in the servlet  it before you load your JSP and if the link
didn't work in your test don't refer to it on your JSP at all.
0
 
RobWellsMcAuthor Commented:
Can you add jQuery / DOM within the URL("")  ??  This way the code can grab what is in the URL....?

URL yahoo = new URL("http://www.yahoo.com/");
0
 
for_yanCommented:
Well there are many ways to get the contents f te URL and then parse it from Java code.
You can use, say,  HttpUnit or for more simple case clases form standard Java library.
And you first need to clean up the html say with Tidy and then you can use DOM or SAX
too parse it and dpeneding on your method and contents of the page you can
extract different sorts of information.

I thought, though, that  yoru question was about diagnosing if the site is good and responding - for that
as a rule you don't need to parse the output - yoou'll receive the error before that.
The you can put it on your JSP page.
0
 
RobWellsMcAuthor Commented:
Let me know if this code looks Ok (in between the body tags):

<html>
<body

<%
try {
    URL urlString = new URL(
                "$(document).ready(function() {
        var pathname = window.location.pathname;});"
);
    URLConnection urlStringConnection = urlString.openConnection();
    urlStringConnection.connect();

} catch (MalformedURLException e) {     // new URL() failed
    . . .
} catch (IOException e) {               // openConnection() failed
    . . .
}
%>

</body?
</html>
0
 
RobWellsMcAuthor Commented:
Ok you got it, I'll clean up the JSP code with HTML tidy, etc.  Thanks!
0
 
for_yanCommented:

I think something like that.
I'm more into servlets than into JSP - I would have done it in a special
servlet than in the JSP page, so that say your previous action could
point to servlet, which would do it in pure java - try to connect and if it does connect
then it redirects to JSP wihich contains this link, if it does not then
to JSP without tis link or with another one which went throigh succesful
test.
Then you would not need to mix java with JavaScript.
Well, perhpas those are my personal preferences I feel more at home with servlets.

But in general I think your code
looks good and should do the check you want.

And yes, parsing is another long story - starting with HTMl Tidy and then
say parsing as HTML, etc.
That of course can all be rather cumbersomebased on the complexity of
the site and on the spcificity of what you need to extract.

 
0
 
RobWellsMcAuthor Commented:
Is it possible to add the code *below* within the catch (IOException e)?

int i = 10;
// This line will create an error so error page will be called
i = i / 0;
0
 
for_yanCommented:
Maybe better to refer to say hidden field in you document and toggle its value
from your catch loop and then check it when you get to your page code out of this
java piece. I'm sure there should be a better way to do it in jsp then
to add this zero division
 
0
 
RobWellsMcAuthor Commented:
okay, can you provide a rough example - it doesnt have to be perfect.... just to give me the idea what to work on next....  ?
0
 
for_yanCommented:

I think this shows how to handle exceptions in JSP page:
http://www.roseindia.net/jsp/simple-jsp-example/exception-handling-in-jsp.shtml
After all your connection exceptionis also exception - so you can deal
with it iin the same way as they show here

I think this is the  directive which should be used in this case (the same one as you use):

<%@ page errorPage="exceptionHandler.jsp" %>

As I sse here when you execute  in JSP, you probably don't catch execptions
it will do it for you and redirect to uyour error page, the same what it would do
if you do i/0




0
 
RobWellsMcAuthor Commented:
Great, thanks, let me test that one and get back to you.
0
 
for_yanCommented:
Sure, wish you good luck
0
 
objectsCommented:
from what you've said you don't need an error page at all.
Can you show me the code that looks up the url based on the id
0
 
RobWellsMcAuthor Commented:
Yah,

Thanks for the answers, they were very helpful and helped me figure things out and eventually I found the exact solutions to my JSP / Java issues.

Thanks again!!

Rob
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 15
  • 14
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now