Hi. I have a java project, QuickPoll that contains 4 classes, which are
- QuickPollAsk
- QuickPollSetup
- QuickPollVotes
- QuickPollResults
These are made using servlets, and run on a tomcat server. I am asked to reimplement the project in the MVC style, using Java-less JSP for the view.
I know what the Controller, Model and View should contain, but I just don't know how to get started. Once I get started it will be all good I think. I know that the controller can contain many classes, which I think is the purpose here, isn't it? Would you create the controller in a package (Eclipse) on its own, containing several classes?
Thank you in advnace
Servlets using MVC pattern
Page 1 of 17 Replies - 1515 Views - Last Post: 04 September 2011 - 06:33 PM
Replies To: Servlets using MVC pattern
#2
Re: Servlets using MVC pattern
Posted 25 August 2011 - 05:19 PM
I would do this:
Models:
-This are your Java business logics, go add them on a package example: pkg.models
Views:
-This are your JSPs, add it to a JSP folder on your WebContent folder
Controller:
-The servlets which only job is to get data from JSP, pass it to a model, get some pertinent data needed, then pass it along another view. As much as possible, no business logic should be allowed here. Add them to a package example: pkg.servlet
To make a simple example:
- Create an Index.jsp(or home or hq, whatever suits you), then add a form(or forms) that will access your controllers.
Ex: A simple login page with the textbox for Username and Password.
- Create a servlet controller and a model. This one will just take the values from the previous views, pass them to a model and return to evaluate what new view is needed.
Ex: You get the parameters from the previous page and pass them to an instance of a User class, which validates if the user exists. It returns a boolean. If true, forward to homepage view. If not, redirect back to the homepage or an error page if you like.
- Create views that will serve you based on what the controllers decide on sending.
Some reminders:
- The web.xml is key to the navigation and overall structure of the site, be sure to pay attention to it.
- There is a design pattern that lets you remove all the ifs on your controllers, I forgot what but if you find it it would truly help( I seem to remember it was Action something)
- Last of all good luck!
So the structure would be:
Eclipse will be a big help
Models:
-This are your Java business logics, go add them on a package example: pkg.models
Views:
-This are your JSPs, add it to a JSP folder on your WebContent folder
Controller:
-The servlets which only job is to get data from JSP, pass it to a model, get some pertinent data needed, then pass it along another view. As much as possible, no business logic should be allowed here. Add them to a package example: pkg.servlet
To make a simple example:
- Create an Index.jsp(or home or hq, whatever suits you), then add a form(or forms) that will access your controllers.
Ex: A simple login page with the textbox for Username and Password.
- Create a servlet controller and a model. This one will just take the values from the previous views, pass them to a model and return to evaluate what new view is needed.
Ex: You get the parameters from the previous page and pass them to an instance of a User class, which validates if the user exists. It returns a boolean. If true, forward to homepage view. If not, redirect back to the homepage or an error page if you like.
- Create views that will serve you based on what the controllers decide on sending.
Some reminders:
- The web.xml is key to the navigation and overall structure of the site, be sure to pay attention to it.
- There is a design pattern that lets you remove all the ifs on your controllers, I forgot what but if you find it it would truly help( I seem to remember it was Action something)
- Last of all good luck!
So the structure would be:
Java src
pkg.model // business logic
- User.java
pkg.servlet // controllers
- Login.java
Webcontent
JSP //views
-Login.jsp
Eclipse will be a big help
This post has been edited by fromTheSprawl: 25 August 2011 - 05:21 PM
#3
Re: Servlets using MVC pattern
Posted 26 August 2011 - 06:44 AM
Thanks a lot, this is very helpful 
I don't know if you have worked with annotations before, but is there any reason why a descripter (web.xml) is prefered over using annotations?
I don't know if you have worked with annotations before, but is there any reason why a descripter (web.xml) is prefered over using annotations?
#4
Re: Servlets using MVC pattern
Posted 30 August 2011 - 06:26 PM
Well a web.xml is easier to configure plus you have all your information in one file. Annotations get in battle with XML files when it comes to dependency injection(using Spring for example) or for setting security. I'm not sure, but I think web.xml is indispensable because it is used by the web app to deploy your project and check what files you have and how to handle them. I haven't used annotations before(just a little with JPA) so I'm not sure if you can replace the web.xml with it( I don't think so)
#5
Re: Servlets using MVC pattern
Posted 31 August 2011 - 07:03 AM
Why would you use JavaEE. PHP seems much easier for your purpose. But if you do use it, then good luck.
#6
Re: Servlets using MVC pattern
Posted 03 September 2011 - 01:21 AM
fromTheSprawl, on 30 August 2011 - 06:26 PM, said:
Well a web.xml is easier to configure plus you have all your information in one file. Annotations get in battle with XML files when it comes to dependency injection(using Spring for example) or for setting security. I'm not sure, but I think web.xml is indispensable because it is used by the web app to deploy your project and check what files you have and how to handle them. I haven't used annotations before(just a little with JPA) so I'm not sure if you can replace the web.xml with it( I don't think so)
Hey.
You can replace the web.xml with annotations, which is much easier since you cut many line of a web.xml file to just 1 line. That's why I was asking/wondering why som people still made web.xml
darek9576, on 31 August 2011 - 07:03 AM, said:
Why would you use JavaEE. PHP seems much easier for your purpose. But if you do use it, then good luck.
I don't like php.
Besides, using java EE was part of the excercise
#7
Re: Servlets using MVC pattern
Posted 04 September 2011 - 05:09 PM
What? They use annotations for what? Haven't encountered that yet, interesting. Thanks for the information
#8
Re: Servlets using MVC pattern
Posted 04 September 2011 - 06:33 PM
AOM_Set, on 25 August 2011 - 01:30 PM, said:
using Java-less JSP for the view.
What the hell does that mean? JSP is Java... It's also kind of old school, Java EE is still on it's Bean push, I think. Perhaps that's what you mean; keeping the all that icky Java code JSP uses locked up in Beans? Dunno, it's still Java.
In a sense, all web development is MVC. The view is the HTML that's spit out at the end of the process. The controller is that server side code. Your model could be anything, but if it's a database, more better.
If your Model is abstracted enough that your Controller doesn't know how it's stored, it's probably alright. If all your JSP only talks to the controller, then that's probably good too.
Honestly, MVC is a pretty broad concept and it sounds like you have something specific in mind.
Page 1 of 1
|
|

New Topic/Question
Reply



MultiQuote





|