Results 1 to 12 of 12
Like Tree1Likes
  • 1 Post By masijade

Thread: Code that works in Java does not work in jsp

  1. #1
    plantrob is offline Member
    Join Date
    Oct 2013
    Posts
    4
    Rep Power
    0

    Default Code that works in Java does not work in jsp

    I've been beating my head against a wall for a while on this; probably missing something obvious.

    I have a .jsp page (called from a java servlet) that includes the following lines:

    <% HashMap<String,String> md=UIUtility.parseMetaData(metaData);%>
    <% String field, value, newValue;
    ...
    if (compare && !md.isEmpty()) {
    for (String key:md.keySet()) {
    value=md.get(key);

    I get "An exception occurred processing JSP page" pointing to the "for (String key:md.keySet()) {" line. When I copy the same code into my servlet (just before calling the jsp handler), it works just fine. Are there differences between jsp and Java that would affect how the code above is processed?
    thanks for your help!
    Rob

  2. #2
    masijade is offline Senior Member
    Join Date
    Jun 2008
    Posts
    2,571
    Rep Power
    9

    Default Re: Code that works in Java does not work in jsp

    Well, for one, don't use scriptlets in a JSP. It leads to unmaintainable, unscalable code, that, truthfully, makes the eyes bleed to read. Use beans, servlets, etc.
    gimbal2 likes this.

  3. #3
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    3,876
    Rep Power
    5

    Default Re: Code that works in Java does not work in jsp

    mandatory link for these kind of threads:

    Free Online Version of Core Servlets and JavaServer Pages (Second Edition) in PDF

    Are there differences between jsp and Java that would affect how the code above is processed?
    No, because a JSP is translated into a servlet when you request it the first time. You can find the generated source file somewhere in your server's directory - if it is Tomcat or JBoss look for a "work" subdirectory. Any exception you get will point to a line in the generated Java source file (and not the JSP, you are probably looking at the wrong line), so it pays to look for it and see what might be the problem.
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  4. #4
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,949
    Rep Power
    19

    Default Re: Code that works in Java does not work in jsp

    Post the full exception and stack trace here.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  5. #5
    plantrob is offline Member
    Join Date
    Oct 2013
    Posts
    4
    Rep Power
    0

    Default Re: Code that works in Java does not work in jsp

    My situation is a bit unusual in that I don't have access to the server on which the code runs. Makes it harder to get at the bottom of some bugs, for sure. As I mentioned in my original post, the jsp file is processed through a call from a servlet - so I don't understand how "use servlets" can help solve my issue. It appears that this way of using .jsp's does not result in precompilation as suggested above - the error I noted is quite clear about the line on which it occurs (and from dealing with earlier, easier-to-pinpoint bugs I know that the line number given does correspond to the line in the jsp file). Unfortunately, the nature of the exception is not given - just that an exception occurred. printStackTrace would go to stderr, which I do not have access to.
    I haven't tried exception handling, since my limited reading on exception handling with .jsp suggests that it doesn't use the same try-catch construct as regular Java.
    Still stumped.

  6. #6
    masijade is offline Senior Member
    Join Date
    Jun 2008
    Posts
    2,571
    Rep Power
    9

    Default Re: Code that works in Java does not work in jsp

    Quote Originally Posted by plantrob View Post
    My situation is a bit unusual in that I don't have access to the server on which the code runs. Makes it harder to get at the bottom of some bugs, for sure. As I mentioned in my original post, the jsp file is processed through a call from a servlet - so I don't understand how "use servlets" can help solve my issue.
    I don't see how it doesn't, write a servlet not a jsp, or look at the part of the statement that you seem to have missed (i.e. "beans").
    It appears that this way of using .jsp's does not result in precompilation as suggested above - the error I noted is quite clear about the line on which it occurs (and from dealing with earlier, easier-to-pinpoint bugs I know that the line number given does correspond to the line in the jsp file).
    It does though, it just may be that JEE has gotten better at pointing out the right line.
    Unfortunately, the nature of the exception is not given - just that an exception occurred. printStackTrace would go to stderr, which I do not have access to.
    I haven't tried exception handling, since my limited reading on exception handling with .jsp suggests that it doesn't use the same try-catch construct as regular Java.
    Still stumped.
    Then you need to GET access to the logs, or, at the least, setup a local server to test on BEFORE deploying to your "real" server. You CANNOT debug/fix a problem when you do not KNOW what the problem is.

  7. #7
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,949
    Rep Power
    19

    Default Re: Code that works in Java does not work in jsp

    Just a thought:
    Java Code:
    for (String key:md.keySet())
    If it is that line...what version of Java is being used on the server?

    But other than that, I'll echo masijade. You cannot be expected to solve a problem on the server without access to the server logs.

    Besides that, don't you have a local test instance? If so then can't you run it up on there and attempt to recreate the problem?
    Seems a bit daft to dump this stuff on to a server for testing...especially one you have no log acces to.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  8. #8
    jim829 is online now Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,508
    Rep Power
    5

    Default Re: Code that works in Java does not work in jsp

    About a year or so ago, I downloaded this and went thru every exercise. A very useful resource. I need to go thru it several more times though.

    Regards,
    Jim
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

  9. #9
    plantrob is offline Member
    Join Date
    Oct 2013
    Posts
    4
    Rep Power
    0

    Default Re: Code that works in Java does not work in jsp

    Quote Originally Posted by Tolls View Post
    Just a thought:
    If it is that line...what version of Java is being used on the server?
    I'll try to find out. Is it possible for the engine that processes jsp code to be different from the one that processes the Java servlets? I wouldn't think so, but you never know...

    Besides that, don't you have a local test instance? If so then can't you run it up on there and attempt to recreate the problem?
    Seems a bit daft to dump this stuff on to a server for testing...especially one you have no log acces to.
    I know. The situation is unusual - the application is a large one, running on multiple servers, and compiling using a few proprietary libraries to which I don't have access. So much as I'd like to, I can't create a local test instance. I've previously requested access to the error logs, but haven't gotten very far. I'll try again.

  10. #10
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,949
    Rep Power
    19

    Default Re: Code that works in Java does not work in jsp

    Blimey.
    I'd really not like to have to work in tha environment.
    Sounds like a recipe for disaster.

    As for the Java version, your servlets would fail to execute if they were compiled on a post for-each loop version and run on a server with a pre-fro-each loop version. It was the only thing on that line I could spot, offhand.

    As for the "don't do this in JSPs", you really should only have tags in there.
    Your servlet should be passing the data the JSP needs to display, and not expect the JSP to interpret it.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  11. #11
    plantrob is offline Member
    Join Date
    Oct 2013
    Posts
    4
    Rep Power
    0

    Default Re: Code that works in Java does not work in jsp

    Quote Originally Posted by Tolls View Post
    As for the Java version, your servlets would fail to execute if they were compiled on a post for-each loop version and run on a server with a pre-fro-each loop version.
    Yeah, that's what prompted my original question - I was wondering whether enhanced for was available in jsp. As far as I know, all of the Java environment I'm working with is the same. But I'll ask around to see if there's something I'm missing

    As for the "don't do this in JSPs", you really should only have tags in there.
    Your servlet should be passing the data the JSP needs to display, and not expect the JSP to interpret it.
    Well, some processing would be required, right? For example, the code I'm referencing needs to build a table. It does so by extracting data from a bean, applying some logic, and producing the table. The jsp actually runs fine if the

    if (compare && !md.isEmpty()) {

    evaluates to false, so it's definitely a run-time thing. I agree that I could do more of the logic and processing up front in the servlet, and produce e.g. a two-dimensional array for the jsp to render as a table. But even in that case I'd still be using for loops, not just tags, right?
    To complicate things, the response is being internationalized, through resourcebundles that are specified in the jsp headers - so I can't just build a string array in the servlet and output it literally.

  12. #12
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,949
    Rep Power
    19

    Default Re: Code that works in Java does not work in jsp

    Quote Originally Posted by plantrob View Post
    Yeah, that's what prompted my original question - I was wondering whether enhanced for was available in jsp. As far as I know, all of the Java environment I'm working with is the same. But I'll ask around to see if there's something I'm missing
    Not the problem, base on what you say next:

    Quote Originally Posted by plantrob View Post
    Well, some processing would be required, right? For example, the code I'm referencing needs to build a table. It does so by extracting data from a bean, applying some logic, and producing the table. The jsp actually runs fine if the

    if (compare && !md.isEmpty()) {

    evaluates to false, so it's definitely a run-time thing.
    Which means that the JSP compiles, so nothing to do with Java versions. You need the stack trace.

    Quote Originally Posted by plantrob View Post
    I agree that I could do more of the logic and processing up front in the servlet, and produce e.g. a two-dimensional array for the jsp to render as a table. But even in that case I'd still be using for loops, not just tags, right?
    It is possible to produce tables with nothing but tags. That's what the various tag libs exist for.
    Entire web apps exist out there that do just that.

    Quote Originally Posted by plantrob View Post
    To complicate things, the response is being internationalized, through resourcebundles that are specified in the jsp headers - so I can't just build a string array in the servlet and output it literally.
    But you can reference the relevant resource via tags.
    Indeed there's an entire tag library devoted to it.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

Similar Threads

  1. Code works but nothing shows
    By Hagelslag in forum New To Java
    Replies: 2
    Last Post: 01-21-2013, 06:03 PM
  2. Replies: 13
    Last Post: 10-04-2012, 07:45 PM
  3. Replies: 5
    Last Post: 06-02-2011, 06:59 AM
  4. Timer java code does not work
    By mariapatrawala in forum New To Java
    Replies: 10
    Last Post: 12-12-2010, 10:34 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •