Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Validate

 
Trupti Mehta
Ranch Hand
Posts: 79
Android Java Java ME
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

In my JSP's, I first want to make surethat all inputs are entered by user before sending it to the Servlet. I tried using the code :



I tried putting the script after <html> or before <html>, but I get the same error - Object doesn't support this property at line referring DOCtype & other at <option> Dermatologist</option> line. If I don't add any script & do all validations checking in the servlet only, thne its working fine. But I don't want to show error on other page & then redirect here. I want to solve any input error here only & then call the servlet with pure data.

I used a differnt method to do all validation checking in other jsp by setting action="other.jsp". At presnet that jsp is only :


I had used <script>alert("Enter ID"); </script> instead of out.println

Here also I get :

that refers to the &lt;% tag after &lt;/h1&gt;


Can anyone help me out, where am I going wrong. I would prefer chcking for input in the same file only. Please help me out with the above problems.

Thanks
 
Vikas Kapoor
Ranch Hand
Posts: 1374
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello Trupti,

Can you edit your post and disable the HTML? You can see that how hard your code is to debug. eg. < is converted to &lt;
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 65115
89
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please take the time to choose the correct forum for your posts. This forum is for questions on JSP.

For more information, please read this.

This post has been moved to a more appropriate forum.

 
Trupti Mehta
Ranch Hand
Posts: 79
Android Java Java ME
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

In my JSP's, I first want to make surethat all inputs are entered by user before sending it to the Servlet. I tried using the code :



I tried putting the script after html and before html, but I get the same error - Object doesn't support this property at line referring DOCtype and other at option - Dermatologist line. If I don't add any script and do all validations checking in the servlet only making the button as submit type, then its working fine. But I don't want to show error on other page and then redirect here. I want to solve any input error here only and then call the servlet with pure data.

I used a differnt method to do all validation checking in other jsp by setting action as loginProceed.jsp. At present that jsp is only :


I had used instead of out.println

Here also I get :

that refers to the scriptlet tag after h1 tag


Can anyone help me out, where am I going wrong. I would prefer chcking for input in the same file only. Please help me out with the above problems.

Thanks
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 65115
89
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can't check the value in the JSP. JSP executes on the server in order to format the HTML to send to the browser. So how can you check the values that the user entered before the page even gets sent to the browser?

Please read this article to understand how JSP works.
 
Eric Pascarello
author
Rancher
Posts: 15385
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You need to do some debugging



Eric
 
Trupti Mehta
Ranch Hand
Posts: 79
Android Java Java ME
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Thanks Bear for the info.

Then what and how should be done for validating with only using JSP/Servlets. This tells me that the checking & validations should be done only in Servlet or basically other JSP. Please Correct me if I am wrong.
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 65115
89
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Validation should be done in the servlet to which you are submitting the form to.

Your JSPs should never do any validation or other data processing,. That's the job of a servlet. Your JSPs, in fact, should never contain any Java code at all.
 
Bauke Scholtz
Ranch Hand
Posts: 2458
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I would just do validation at the both sides.
At the client side for the user experience.
At the server side for the robustness.
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 65115
89
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Bauke Scholtz wrote:I would just do validation at the both sides.
At the client side for the user experience.
At the server side for the robustness.

Exactly. I don't even consider client-side validation to be real validation -- I consider it part of the context-sensitive help!
 
Trupti Mehta
Ranch Hand
Posts: 79
Android Java Java ME
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Hello Bauke Scholtz & Bear Bibeault,

This is what I am also looking for. I don't intend to do validations like user name exists or not, etc in the JSP. That all is and strictly done in servlets only. I intend to do clinet side validations only - like in a form some inputs are not done then alert the user to enter it. I am looking out for such validations which I have also pointed from start. Like in a login form lets say, user has not entered user id or password & clicked login button. Then the user should be asked to enter those details and then only login button should go to the servlet. How do I achive this client side validation is my main point. As you both agree that client side validations should be and can be done on client side i.e. JSP page then how do I do the same.

I hope now my question is very clear and will be able to help me accordingly.

Thanks
 
supriya riya
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Trupti i am providing you a validation code,which i have done myself its basic validation which any form needs,u can use for different elements.I have done for few like select etc.Phone numbers etc in my form.Just try to implement it.It is working code.
 
supriya riya
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you want to set focus after alert on specified field just add this after alert.Depends on ahat you want.Like Phone number wrong entered will have to be cleared then set focus.Else just set focus.


Hope this helps u.

 
Trupti Mehta
Ranch Hand
Posts: 79
Android Java Java ME
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Hello,

Thanks for the code, Supriya. I added chk() & implemented my fields. Initially it worked well, but after I added few more fileds and changed the method name from chk() to checkEmpty() & tried, it started going directly to the servlet. I removed all added fields, renamed the method name & made everything as it was working, but yet now it goes to the servlet only. Restarted the IDE, Clean and Build, but no differnce. Here is the code :



Is it that sometimes it will work and sometimes not. The servlet just displays "Into Servlet" at present. I had also added a function "validateAll" and called chk from it. Removed that also -I just wanted to cal la single function and do all activities; go out whenever false. Can you tell what should be the reason behind just type of reaction. I use Sun Application Server 9.1 with NetBeans.

Isn't this kind of programming liable ???

Thanks
 
Cristian Senchiu
Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Trupti,

if the code you run is exactly what you wrote here, then I spotted a problem at line 39: two "" immediately after alert(:

Generally, unrelated to your problem here: not a bad idea is to use try-catch(-finally) around the js code in the method and proper treat/log/alert any caught exception, as it gives the code robustness, saves time at debug, ...
 
supriya riya
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Trupti as Cristian said remove extra
"

in
alert(""Enter a 8 character long NRIC number.");


i removed it and your code works fine.
When using javascript in netbeans,u cannot find errors easily ,you have to be careful look out for errors.
 
supriya riya
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Changing method name does not change its functionality.You need to be careful how you call.And rest coding and minute details.
Is it that sometimes it will work and sometimes not

No it is not.
 
Trupti Mehta
Ranch Hand
Posts: 79
Android Java Java ME
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Thank you Cristain and Supriya.
Cristain I was wondering in <script ...> how can we add scriplets for try/catch/finally. I belive in robustness programming and in my normal programming I use it a lot. But don't how about this case, kind of newbie....


I also have to look out for logging implementation, to trap error messages and information.

Thanks

 
Cristian Senchiu
Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Trupti,

I mean in javascript, like this:

In exception handling, besides other actions, I do usually also a log on the server, with stacktrace and everything, as in java.
Like this I can look into the log file on client's server and see what happend, where, why ...
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic