aspose file tools*
The moose likes JSP and the fly likes Trying to use a PatternSyntaxException message for a pop-up in a JSP page Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » JSP
Bookmark "Trying to use a PatternSyntaxException message for a pop-up in a JSP page" Watch "Trying to use a PatternSyntaxException message for a pop-up in a JSP page" New topic
Author

Trying to use a PatternSyntaxException message for a pop-up in a JSP page

Don Smith
Greenhorn

Joined: Mar 05, 2009
Posts: 6
Hello. Here's the code I am basically trying to run in a JSP page:



The above code gets an "Unterminated string constant" error from Javascript, which I believe is because of the inclusion of the pattern in the message. This makes me believe there isn't any standard way to "cleanse" the exception message since it stems from a pattern problem that could include just about ANY bogus regular expression. If someone has insight here though I'd be glad to listen.

I can put the exception directly to the page (see the commented line), and I was also able to create an error page. While those "worked", both are extremely ugly / user unfriendly.

Can anyone give me some way I could create a nice pop-up here which can give the user some insight into why the pattern they entered was bad?

Thanks!
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18541
    
    8

You want some Java code which escapes a string into something which can be used as a Javascript string literal. (That of course isn't anything to do with JSP, but then using Java like that in a JSP is bad practice these days.) You should be able to google up something like that, I think.
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61010
    
  65

Two very important things:

  • Firstly, what you are trying to do is simply impossible. Read this JspFaq entry to find out why.


  • Secondly, using scriptlets in a JSP has been obsolete and discredited for over 10 years now. Read this JspFaq entry.


  • [Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
    Don Smith
    Greenhorn

    Joined: Mar 05, 2009
    Posts: 6
    Bear Bibeault wrote:Two very important things:

  • Firstly, what you are trying to do is simply impossible. Read this JspFaq entry to find out why.


  • Secondly, using scriptlets in a JSP has been obsolete and discredited for over 10 years now. Read this JspFaq entry.


  • Even if mixing the JSP and Javascript *was* possible, I know that cleaning this string is not as simple as it sounds.

    Couldn't agree with you more here. *sigh* This is what happens when a grizzled, back-end Java developer (previously C++, more recently even C#) programmer is "assigned" to do a GUI because he's a "senior" guy. Well, I've told them I am not fluent at all in modern web pages, but of course they think it "should be easy" and ask me "then what *IS* your skillset?" Ugh. So I am trapped trying to find solutions and management doesn't feel the need to give me the time to determine the right way to do things. I know... beating a dead horse.

    Of course even if I use a bean (the code snippet I showed was actually abstracted out of a bean call so I wouldn't have more code than necessary), at the end of the day I will have to provide the user with a message... and I'm not sure of the best way even if the bean does ALL the processing.

    Thank you for the links... I will see what I can do here.
    Bear Bibeault
    Author and ninkuma
    Marshal

    Joined: Jan 10, 2002
    Posts: 61010
        
      65

    Sorry to hear of your predicament. But we're here to help.

    What is the end goal? It's really not a good idea to show exceptions to end users. They have no idea what they are looking at.

    You'd be better of dealing with the exception in code, logging it so that developers can see it and determine what went wrong, and then showing a generic "Oops, the server went belly up" message to the end user.

    And of course, none of this should be done in a JSP. Are you writing new code, or saddled with badly written legacy pages?

    Don Smith
    Greenhorn

    Joined: Mar 05, 2009
    Posts: 6
    Fortunately it is new code. If they'd gotten a web developer (my company has MANY of them), I'm sure this would have been a breeze.

    You are exactly right: logging the real problems and then simply responding to the user with "something went wrong... please try again" is the best bet. I wasn't trying to give them a stack trace (that would have been super easy but also NOT DESIRABLE on so many levels. I keep forgetting (since my typical work doesn't involve end users, usually other systems) that my user community isn't programmers by trade; I simply saw what would have lead me DIRECTLY to the problem and thought I should send it along. I think I can make a decent message on the error page if I simply don't try to send the included exception message.

    Thanks so much for your time!
     
    wood burning stoves
     
    subject: Trying to use a PatternSyntaxException message for a pop-up in a JSP page