GeeCON Prague 2014*
The moose likes Servlets and the fly likes if condition in servlet Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Java » Servlets
Bookmark "if condition in servlet" Watch "if condition in servlet" New topic
Author

if condition in servlet

Ritesh raushan
Ranch Hand

Joined: Aug 29, 2012
Posts: 100
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

Joined: Jul 09, 2011
Posts: 272

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.


Ashwin Sridhar
SCJP | SCWCD | OCA
Matthew Brown
Bartender

Joined: Apr 06, 2010
Posts: 4397
    
    8

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

Joined: Sep 21, 2006
Posts: 152

Matthew Brown wrote:Client-side validation can never be relied upon.


Why is that?
Matthew Brown
Bartender

Joined: Apr 06, 2010
Posts: 4397
    
    8

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

Joined: Feb 26, 2001
Posts: 4474
    
    6

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.


Junilu - [How to Ask Questions] [How to Answer Questions]
Bill Clar
Ranch Hand

Joined: Sep 21, 2006
Posts: 152

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

Joined: Oct 31, 2011
Posts: 498

Thanks for your helpful suggestion guys. I am now taking care of server side validation as well.


PM Me If You Want to get Urgent Help on Java Programming
Ritesh raushan
Ranch Hand

Joined: Aug 29, 2012
Posts: 100


can anyone tell about what did i write in if condition.
Junilu Lacar
Bartender

Joined: Feb 26, 2001
Posts: 4474
    
    6

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?
 
GeeCON Prague 2014
 
subject: if condition in servlet