2

What I want to do:

I have a form with a lot of fields(nick, email, name, surname, etc.) but the user has to fill Nick and Email first in order to be able to fill the other fields(this is because we want to check that the nick and mail aren't in use by another client before he can introduce the rest of his information(name, surname, etc.)). So, the user introduces Nick and Email and then he must press a button named "Validate", if the values are available(successful validation) then the rest of the fields are enabled and the user can continue filling the form, otherwise the fields stay disabled and an error is showed to the user.

The form will be located in a JSP, it will be submitted to a Servlet, once in the servlet I must validate the information that is in the form(i have a .JAR file included in this servlet, the validation consists in calling a function from that library, the function returns a boolean) and then I must return back to the same JSP the boolean that will represent the result of the validate function. Now in the JSP I must enable(or not, depending on the value of the boolean) the rest of the TextFields.

I'm not sure if this is right but i was trying to submit with the button and at the same time run a javascript(onclick) that will use this boolean value that the servlet sends back to the JSP after making the validation. The javascript consists on an IF sentence that evaluates the boolean and if it's true then it enables all the fields on the JSP.

Problems so far:

I was able to send the Nick and Email from the JSP to the Servlet and to make the validation of the values, now i have the boolean but i have no idea on how to send it from the Servlet to the same JSP and use it in the onclick event of the same button I used to submit the info. I don't even know if it's possible to do this...

I'd be grateful if someone could give me a hand with this, i'm newbie in Java programming so i would appreciate simple explanations if possible. Also, if there is a better way of doing what i want please share it, and if there are any doubts ask and i will try to explain it better.

3 Answers 3

2

There is no need for JavaScript at all.

In your servlet you can store the validation result into the request context:

req.setAttribute('checkResult', checkResult);

where req is of type HttpServletRequest and checkResult is a Boolean.

Then you can forward to your JSP:

RequestDispatcher dispatcher = req.getRequestDispatcher("/your.jsp");
dispatcher.forward(req, resp);

In your JSP you can set your form elements as read only depending on the attribute checkResult which you have put into the request context:

<textarea name="text" cols="50" rows="10" 
  <%= request.getAttribute("checkResult") != null && request.getAttribute("checkResult") ? "" : "readonly" %>
>...</textarea>

So if the check is not valid then the <textarea> element will contain the readonly attribute. Otherwise readonly is not present.

Sign up to request clarification or add additional context in comments.

6 Comments

Hello, thanks for the advice, it seems to be a good solution but I'm having problems with this line: <%= request.getAttribute("checkResult") != null && request.getAttribute("checkResult") ? "" : "readonly" %> I don't understand it, what does the "?" and the ":" do? Im getting an error in that line: "the operator && is undefined for the argument type(s) boolean, Object"
Sorry, you are right. getAttribute() returns Object, so you have to cast it to Boolean. The part before the ? have to be: request.getAttribute("checkResult") != null && ((Boolean) request.getAttribute("checkResult")). This is the condition of the ternary operator (see en.wikipedia.org/wiki/%3F:#Java ). It evaluates to true, if the attribute "checkResult" is set in request context and the Boolean value is true. There are two values after the ?. The first one before the colon is the value of the ternary expression if the condition is true.
The ternary expression evaluates to the value after the colon, if the condition is false. So the result of the whole <%= ... %> part is the empty string, if the checkResult attribute is set to true. And the string "readonly" if the checkResult attribute is not present in request context or is set to false.
<%= ... %> is a scriptlet expression. See techfaq360.com/tutorial/jsp/jspscripting.jsp
JSP scriptlets is a very basic JSP technique and allows to write Java code inside a JSP page which is mostly considered as bad practice for larger real world projects. You are encouraged to use tag libraries instead. My personal opinion is that JSP scriptlets are ok if you have only a hand full small pages.
|
1

As Roy mentioned AJAX is best suited for your problem. You can use DWR! , it makes normal java classes available as AJAX services, just call the method on them and get the result. So easy.

Comments

0

I think AJAX is more suitable for your application, which will not require to submit the whole form and you can send back the validation flag as plain responseText or well-formatted responseXML. Also you can use a lot of good javascript library such as jQuery that helps you send an AJAX request quickly and simply.

2 Comments

Thanks for your answer. Anyway, i would prefer not to use AJAX because I don't know anything about it and i have a time limit to develop this application. Therefore I don't know if it would be a good idea to dedicate time to study AJAX from zero.
Ok, then for your case, you may still forward back to this same page (URL) and append the validation flag (Y/N) in the URL, for example: "?validatedOK=Y".

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.