Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

if condition in servlet

 
Ritesh raushan
Ranch Hand
Posts: 101
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i have created a program to take a name and list of books from user and my code calculate the total amount of book whatever he selected and gives all books name selected and total amount of books to output.but
my code works even user doesn't give her/his name so
i want to create condition that when user will not give her/his name then my program will not work and gives proper message to user "please enter your name".

but i doesn't understanding that what did i give condition .i have uncommented a if condition and my else block is at last




 
Ashwin Sridhar
Ranch Hand
Posts: 277
Flex Oracle Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why do you check it in server side. You probably check it in client side and prevent user from submitting the form to the server.

 
Matthew Brown
Bartender
Posts: 4567
8
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ashwin Sridhar wrote:Why do you check it in server side. You probably check it in client side and prevent user from submitting the form to the server.

Client-side validation can never be relied upon. You can do it if you want, but you have to do it server side as well.
 
Bill Clar
Ranch Hand
Posts: 163
Eclipse IDE Java Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Matthew Brown wrote:Client-side validation can never be relied upon.


Why is that?
 
Matthew Brown
Bartender
Posts: 4567
8
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Bill Clar wrote:Why is that?

Because it can always be circumvented. For example, if you're using Javascript validation it can be turned off. Or they can modify the Javascript on the fly. Or they can avoid using a web browser at all and just sent custom HTTP messages straight to your server.

You have no control over the client at all, so you can't make any assumptions about it. Client-side validation can be great for usability, but if it's for security or application integrity you have to do it server side.
 
Junilu Lacar
Bartender
Posts: 7480
50
Android Eclipse IDE IntelliJ IDE Java Linux Mac Scala Spring Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And unless you are writing a very simple web application, you shouldn't do the validation in a servlet either. The servlet should delegate that responsibility to some other class. Your business logic should not rely on having a servlet to validate input data. That will couple your business logic too tightly to the delivery mechanism and that's not a good thing. It also makes your logic more difficult to test.
 
Bill Clar
Ranch Hand
Posts: 163
Eclipse IDE Java Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Matthew Brown wrote:Because it can always be circumvented. For example, if you're using Javascript validation it can be turned off. Or they can modify the Javascript on the fly. Or they can avoid using a web browser at all and just sent custom HTTP messages straight to your server.

You have no control over the client at all, so you can't make any assumptions about it. Client-side validation can be great for usability, but if it's for security or application integrity you have to do it server side.


Never thought of it that way. Thanks for the insight, Matthew!
 
Nam Ha Minh
Ranch Hand
Posts: 502
Eclipse IDE Firefox Browser Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for your helpful suggestion guys. I am now taking care of server side validation as well.
 
Ritesh raushan
Ranch Hand
Posts: 101
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


can anyone tell about what did i write in if condition.
 
Junilu Lacar
Bartender
Posts: 7480
50
Android Eclipse IDE IntelliJ IDE Java Linux Mac Scala Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A couple of things:

1. It would help if you fixed the indentation of the code that you provided. Right now, the indentation seems to be random and it makes your code even more unclear and harder to read. Few people will be inclined to clean up the code indentation for you so please ShowSomeEffort (←click)
2. In your own words, what do you want to check before executing some of this logic?
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic