This week's book giveaway is in the Security forum.
We're giving away four copies of Securing DevOps and have Julien Vehent on-line!
See this thread for details.
Win a copy of Securing DevOps this week in the Security forum!

Keri Mathis

+ Follow
since Jul 23, 2009
Cows and Likes
Total received
In last 30 days
Total given
Total received
Received in last 30 days
Total given
Given in last 30 days
Forums and Threads
Scavenger Hunt
expand Ranch Hand Scavenger Hunt
expand Greenhorn Scavenger Hunt

Recent posts by Keri Mathis

Ok, I'll peruse the FAQ in more detail. This existing application is using OC4J, with which I'm not as familiar as I am Tomcat. Thanks for the replies and information.
8 years ago
Yes, you are correct (as you knew). View source shows the ${} notation on that variable line. I understand this is not a JavaScript issue, as it is getting called correctly. I'm specifically trying to get the right data to pass from a bean that is sometimes there, sometimes not. If that notation is correctly resolved everywhere else, albeit within JSTL tags, what is there to try differently in this case?
8 years ago
Pretty sure everything is set up correctly. The following line is populating correctly, and only displays when !(empty user).

As an alternative, thinking there was an issue specifically with the bean 'user,' I put the following code at the top of my page
and changed the script lines to

But what gets passed is still '${testvar}' as a literal string. I'm using beans and other variables successfully in other parts of my page, but I just can't seem to generate a value to pass to JavaScript. If there is instead a way to call the JavaScript, and pull a data value from that field instead, maybe that's what I should try (a hidden input field or something). But it seems like it should work passing the data.
8 years ago
In my struts application, within my JSP, I need to look up some information if my session user bean is not null. Here is what I have:

My trouble is getting the 'username' value to pass into the javascript function. I have tried the following syntax:
var name = '${user.username}'; > PASSED ${user.username} AS STRING
var name = "${user.username}"; > PASSED ${user.username} AS STRING
var name = ${user.username}; > ERROR IN FIREBUG "missing ; before statement"
var name = ${'user.username'}; > ERROR IN FIREBUG "missing ; before statement"

What's the key to make this work?
8 years ago
Resolved. The fix, in case it's ever useful to someone else:

JSP1 collects the data, and passes it to JSP2.
JSP2 stores the values as hidden variables, to allow the user to preview the form, then submits from there.
If the user entered a name like Larry “the Cucumber” Smith on JSP1, JSP2 does this: Which results in this on the page, and really results in just “Larry ” being the true value.

The new code adds this JSTL function using page variables:
Someone suggested using org.apache.commons.lang.StringEscapeUtils. I didn't explore this because it came after the fact, but I could not pass ${param.CurrentName} into any JavaScript function or variable, because it resolved the string prior to passing and then was an invalidly-formatted parameter to pass because of the extra quotes.
8 years ago
JSP1 does go through quite a process to collect data dynamically, based on user input along the way, along with some validation.
JSP2 just takes all the data passed from JSP1, and formats it into a predefined document format using tables. That way the user can view the information prior to submitting to the database.

While it may not be ideal, this is a complicated form that I inherited, and has gone through quite a bit of testing. There's a lot of data fields, and the JSP2 format had to pass through our legal dept. So I'm not real free to completely redo it.

However, JSP2 really isn't doing a while lot besides (1) storing all variables in hidden fields, and (2) displaying the values on the screen. Then the form gets submitted as shown. If the quotes are in the text on the preview page, the hidden variables should function just like filling out a form and should be a straight-forward submit.

If this is supposed to work, I will create a sample HTTP form, and submit it to a sample servlet, and see if values with double-quotes make it through. My thinking was there was some basic processing that needed to be added to handle this scenario that I was missing.
8 years ago
Ok, more info. JSP1 collects data via form - sample in first post. Submits to JSP2 which presents all the data prior to sending to Servlet. Here's some code from JSP2
In the JSP2 page, the data displays correctly as: John "Bear Wannabe" Johnson


When I break after this last line and examine the data in 'params' the entry for CurrentName only contains the String "John" (no quotes). Is encoding supposed to happen automatically, such that the Map should contain CurrentName=John \"Bear Wannabe\" Johnson?
8 years ago
Problem: User fills out a form in my JSP page, uses double-quotes in the value, and once the servlet gets the data, it cuts it off at the double-quote.

JSP form field: If the user enters: John "Bear Wannabe" Johnson

The first thing my servlet does is call request.getParameterMap() (on HttpServletRequest). If I examine the map and find CurrentName field, the only value is: John. The rest gets cut off after the first double-quote.

Is this a common problem? If so, I could not find a common solution. Do I need to add a filter before the servlet does something? Does the JSP need to encoding all fields prior to submitting? Do I need a JavaScript routine? I've got about 100 fields, so I'm hoping for a possible solution where I don't need to validate each field individually by name.

8 years ago
I can use a properties bundle. But that raises other questions for me.

1) I still need somewhere to store all the keys that will be reused. Static strings in another class?

2) Accessing the strings from multiple classes - what is the best way to re-use or access a ResourceBundle? Is this a shared object? Does each class that uses the strings create it's own ResourceBundle?
8 years ago
What is the best way to use labels and error messages? I definitely don't want to assign them inline (new Label("field1") but I've thought of the following choices:

1) private static final String class variables
2) public static final String variables in an Interface, then implement the interface where needed
3) Access via ResourceBundle.

OPT 1 - does not allow me to use in other classes, like JUnits
OPT 2 - seems to work best, but is it best-practices?
OPT 3 - These strings will not be translated, so is ResourceBundle overkill? I'd also rather not create a .properties file. Is OK to use ResourceBundle with a Java file of strings?

8 years ago
Agreed it would be messy to use a ghost JSP page, but it would beat 100 servlet request.getParameter() calls. I'm surprised there is not a given, standard way to do this. How many html forms are out there submitting to Java servlets, and there's no standard answer to this problem?

I saw you referenced using a Map wrapper, but was not clear how that worked either. But the BeanUtils.setProperty() may be just the thing if I can pass a new bean and the request.getParameterMap(). Thank you for the feedback and the tip.
8 years ago
Thanks, I'll take a look at the configuration.

Yes, my plan is to do JSP to servlet. I'm back to the question I've seen posted a few times where folks are trying to get the html form to auto-populate the bean, and send that to the servlet. I've got the JSP-servlet communication down fine. But I'm really not understanding how/when the following line gets executed, and if it doesn't really load up a bean with matching param values, what does it do?

<jsp:setProperty name="workorder" property="*" />

So I thought I could easily evaluate the bean using a 2nd JSP page. But that's proving difficult as well. Longer-term, I thought if I need to use the setProperty in the 2nd jsp page, I could auto-populate it then forward it to the servlet. But I'm not sure if that will work.
8 years ago
JSP to JSP using a bean, and the EL code displays as plain text, rather than the evaluated value. If it evaluated to blank or null I could see it, but am not sure why it spits it out as generic text. Yes, I am new to using EL, so please let me know if I'm missing something obvious.

This stems from trying to use a bean to pass data, so really I just need to verify exactly what setProperty does, and if it does something in test1.jsp or test2.jsp.



Screen result:
idNumber: ${workorder.idNumber}
username: ${workorder.username}

I tried bean scope = request, session, application. I tried sticking the EL inside a <c: out> tag. I tried moving the jsp:setProperty up and down. Nothing worked.
Testing with Java6 in Eclipse 3.5 running Tomcat 6 with Firefox 3.5.
8 years ago
OK, thanks. I can set up a servlet/dao layer base on previous Java applications I've worked with, just thought there may be some better way with the JSP pages I was unaware of. Thanks for the info and the article link.
8 years ago
Bear with me - I last worked with servlets/jsps in 2003. I recently inherited an application that uses all JSPs with JSTL, no servlet. All SQL is done with the JSTL tags, and we are seeing connection/deadlock errors. After trying to troubleshoot the issue, all signs point to "rewrite the database stuff using beans - don't use JSTL sql tags in production."

My problem is I can't find out how to exactly do that. I know how to use JavaBeans with JSP pages, but I don't know how to use them for database interaction. Based on this website, I ordered the highly recommended book "Head First Servlets & JSP 2nd Edition," and I have to say I'm highly disappointed and frustrated that I spent 40 bucks and waited a week for shipping to find out that it doesn't address the database at all. It even admits most JSP/Servlet applications work with a database, but it does not address how to do so. In fact, it barely mentions using beans at all. (Maybe it's my own fault; I was leery that the title geared it to the exam, but everyone said it was a great resourse book as well. But how an 800 page book on Servlets/JSP does not even cover one simple web app end-to-end is beyond me. Thank you for letting me vent, I'm better now.)

So I've been searching forums and Java sites, but cannot find anything that explains in any amount of detail the best way to update my database with JSP form data. If I had two weeks, I'd convert the application to Grails. But for now, I just need to solve the database errors we are getting in the quickest way possible, while adhering to good Java practices.

The official J2EE tutorial uses Java Persistence API, which looks similar to Hibernate. I have not used either outside of a small Grails app, and we have no long-term plans to create more JSP applications and there are time constraints. So I'll learn it if it's the best way, but want to be sure I'm spending my time wisely.

If anyone can point me to some good resources on using JSP with a database (MySql in this case), I'd be very grateful.
8 years ago