aspose file tools*
The moose likes Servlets and the fly likes when to create class in java? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "when to create class in java?" Watch "when to create class in java?" New topic
Author

when to create class in java?

Punit Jain
Ranch Hand

Joined: Aug 20, 2011
Posts: 1000
    
    2
i have a question, i have created a program for employee records i used a simple employee class for that, which have setters and getters, and one java class is for database code, and one is the main class, now my assignment is to create a registration form using servlets, and i want to write a good code, but i m confused do i create simple class file also for this??
simple class i mean with all the form fields with their getters and setters???
Anayonkar Shivalkar
Bartender

Joined: Dec 08, 2010
Posts: 1509
    
    5

Hi,

First things first : you'll get much more help if you put this query in servlet forum.

Apart from that, if you really want to write a good code, I would not recommend writing a servlet which looks like a bean (i.e. all private fields, getters and setters et-cetera).

Much better would be to use MVC pattern. It looks like you've got your code ready for desktop environment (employee and registration class etc.)

All you'll have to do is:
1) Create a 'view' (which can be as simple as HTML page, or a complex JSP or anything from which you can call servlet)
2) Create a 'controller' (here, it would be servlet - which will accept the requests from view; not necessarily that you'll have only one controller throughout your code)
3) Create a 'model' - which is your existing code. Controller, depending on the nature of requests, will invoke respective methods on corresponding objects. Of course, you can write all your business logic in servlet, but then, it would not be good code

If you don't know anything about servlet, MVC etc. then I'll suggest to go through some basic book like head first servlet & jsp.

I hope this helps.


Regards,
Anayonkar Shivalkar (SCJP, SCWCD, OCMJD, OCEEJBD)
Punit Jain
Ranch Hand

Joined: Aug 20, 2011
Posts: 1000
    
    2

First things first : you'll get much more help if you put this query in servlet forum


i dont know ho do i move my post fro one forum to another.
and yes i got it what you want to say, you mean for registration i should use an mvc architecture, but what if i need to do this using 3tier architecture.??
Anayonkar Shivalkar
Bartender

Joined: Dec 08, 2010
Posts: 1509
    
    5

Punit Jain wrote:i dont know ho do i move my post fro one forum to another.

I'm not sure about this. I guess only admins/moderators (Roberto Perillo, Rob Spoor, Stephan van Hulst) can do this.
Punit Jain wrote:and yes i got it what you want to say, you mean for registration i should use an mvc architecture, but what if i need to do this using 3tier architecture.??

I'm not a JEE guru, but I guess MVC itself is 3 tier architecture. Anyways, if you wan to further extend this, you can have one more layer - database. All you'll have to do is make a call to model, and your model (business logic) would do the rest - i.e. modifying database etc.

I hope this helps.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39415
    
  28
No, I think MVC and three-tier architecture are different things.
Wendy Gibbons
Bartender

Joined: Oct 21, 2008
Posts: 1107

To answer your question, yes you will need a servlet (control) for the login screen.
Is this logging into the app?
as you will then need to call out to another database class (model), which will test if the credentials match a user on the database. returning to your servlet, which will then control the next page (view) the user goes to.

I like a servlet for each page or form.
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61433
    
  67

You may benefit from reading this article.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Punit Jain
Ranch Hand

Joined: Aug 20, 2011
Posts: 1000
    
    2
Is this logging into the app?


ya definitely i will nedd a mvc pattern for login screen, but don't know why mvc..
but i have only registration page, and that i want to code but with a good code/pattern.



No, I think MVC and three-tier architecture are different things.

yes i think so that both are different...
Punit Jain
Ranch Hand

Joined: Aug 20, 2011
Posts: 1000
    
    2

You may benefit from reading this article


i read this article, really nice, i never thought that this can also be happen when i make such type of applications, but i have a question,
suppose i have a page in which i have code to delete as well as to display, as in your example in the article, but in the code i am checking weather the request is for delete or display, so how this problem comes, although i m checking for post??



one more thing, i want to know how can i check for "Double Submit problem" i mean can i test this, weather my data is double submitted or not, i m still confused...

and as your article says, for my registration page i should use mvc/model2??
Victor M. Pereira
Ranch Hand

Joined: Mar 02, 2012
Posts: 50
Well in any case it's just a normal class extending HttpServlet.

If you want to do the extra mile.

1) An HTML page with the login form.
2) You can have a servlet (Controller) that process the data and calls your database class to persist.
3) A JSP Page that shows all the rows or just shows confirmation of the persistance.

I'll also recomend you the head first servlet and jsp book.

P.D. If you want good code don't write scriptlets (i.e. the <% %> tag) in your jsp


regards,
Victor M. Pereira
Punit Jain
Ranch Hand

Joined: Aug 20, 2011
Posts: 1000
    
    2
thank you..
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: when to create class in java?