mvc architecture with servlets and jsp - thejavageek

Upload: rohan-bose

Post on 21-Feb-2018

229 views

Category:

Documents


0 download

TRANSCRIPT

  • 7/24/2019 MVC Architecture With Servlets and Jsp - TheJavaGeek

    1/21

    Core JavaServlets & JspJPA 2.0MongoDBOO & DAbout MeContact Me

    Bio Latest Posts

    JAVA EE, SERVLETS AND JSP

    MVC architecture with servlets and jspby Prasad Kharkar August 11, 2013 111 Comments

    Prasad harkar

    Prasad Kharkar is a java enthusiast and always keen to explore and learn java technologies.

    He is SCJP,OCPWCD, OCEJPAD and aspires to be java architect.

    In this tutorial we are going to learn how to create a simple MVC application using servlets and jsp.

    MVC i.e. Model-View-Controller is a pattern helpful separation of concerns.

    Model represents a POJO object that carries data.

    View is the layer in which the data is presented in visual format.

    Controlleris the component which is responsible for communication between model and view.

    A user always se es t he view an d co mmunicates with th e con troller. We will understan d t his usin g a sample loginapplication which will display a welcome username message and if the login fails, it will redirect to an error page.

    Here is what we are going to create.

    login.jsp :- this will input username and password

    success.jsp :- If login is successful, then this page is displayed

    error.jsp :- If login is not successful then this page is displayed.

    LoginController.java :- This is controller part of the application which communicates with model

    Authenticator .java :- Has bu siness lo gic fo r a uthentication

    User.java :- Stores username and password for the user.

    Requirements:

    Eclipse IDE

    Apache tomcat serverJSTL jar

    Create a new Dynamic web project in eclipse by clicking File -> New -> Dynamic Web Project. Fill

    the details i.e. project name, the server. Enter your project name as MVCDemo. You will get the

    following directory structure for the project.

    Initial Project Structure

    Create success.jsp, error.jsp and login.jsp and LoginController servlet, Authenticator class, User

    class in the packages as shown in the images. Put the jstl.jar in WEB-INF/lib folder.

    http://www.thejavageek.com/wp-content/uploads/2013/08/ProjectStructure.jpghttp://www.thejavageek.com/wp-content/uploads/2013/08/ProjectStructure.jpghttp://www.thejavageek.com/object-oriented-analysis-and-design/http://www.thejavageek.com/core-java/http://thejavageek.com/http://www.thejavageek.com/wp-content/uploads/2013/08/ProjectStructure.jpghttp://www.thejavageek.com/http://-/?-http://www.thejavageek.com/author/pdkharkargmail-com/http://www.thejavageek.com/category/java-ee/servlets-and-jsp/http://www.thejavageek.com/category/java-ee/http://-/?-http://www.thejavageek.com/contact/http://www.thejavageek.com/about-me/http://www.thejavageek.com/object-oriented-analysis-and-design/http://www.thejavageek.com/mongodb/http://www.thejavageek.com/jpa-tutorials/http://www.thejavageek.com/java-servlets-jsp-tutorials/http://www.thejavageek.com/core-java/http://thejavageek.com/
  • 7/24/2019 MVC Architecture With Servlets and Jsp - TheJavaGeek

    2/21

    File Structure

    Package Structure

    Now

    that we

    have

    filestructure, put this code in corresponding files.

    Authenticator .java

    User.java

    123456789101112131415161718192021222324252627282930313233

    343536373839404142

    packagemvcdemo.controllers;importjava.io.IOException;importjavax.servlet.RequestDispatcher;importjavax.servlet.ServletException;importjavax.servlet.http.HttpServlet;importjavax.servlet.http.HttpServletRequest;importjavax.servlet.http.HttpServletResponse;importmvcdemo.model.Authenticator;importmvcdemo.model.User;importsun.text.normalizer.ICUBinary.Authenticate;publicclassLoginControllerextendsHttpServlet{ privatestaticfinallongserialVersionUID= 1L;

    publicLoginController(){ super(); }

    protectedvoiddoPost(HttpServletRequest request, HttpServletResponse response)throwsServletException,IOException{

    Stringusername= request.getParameter("username"); Stringpassword= request.getParameter("password"); RequestDispatcher rd= null;

    Authenticator authenticator= newAuthenticator(); Stringresult= authenticator.authenticate(username,password); if(result.equals("success")){ rd= request.getRequestDispatcher("/success.jsp");

    User user= newUser(username,password); request.setAttribute("user",user); }else{ rd= request.getRequestDispatcher("/error.jsp"); } rd.forward(request,response); }}

    1234567

    8910111213

    packagemvcdemo.model;publicclassAuthenticator{

    publicStringauthenticate(Stringusername,Stringpassword){ if(("prasad".equalsIgnoreCase(username)) && ("password".equals(password))){

    return"success"; }else{ return"failure"; } }}

    12345678910

    111213141516171819

    packagemvcdemo.model;publicclassUser{

    privateStringusername; privateStringpassword;

    publicUser(Stringusername,Stringpassword){ this.username= username; this.password= password;

    }

    publicStringgetUsername(){ returnusername; }

    publicvoidsetUsername(Stringusername){ this.username= username; }

    http://www.thejavageek.com/wp-content/uploads/2013/08/PackageStructure.jpghttp://www.thejavageek.com/wp-content/uploads/2013/08/FileStructure.jpg
  • 7/24/2019 MVC Architecture With Servlets and Jsp - TheJavaGeek

    3/21

    error.jsp

    login.jsp

    success.jsp

    and the web.xml

    We are done with the code. Let us try and run it.

    Start your tomcat server and hit url http://localhost:8080/MVCDemo/login.jsp.

    You should be able to view this page.

    Enter username as prasad and password as password. You will see the message Welcome prasad.

    20212223242526272829

    publicStringgetPassword(){

    returnpassword; }

    publicvoidsetPassword(Stringpassword){ this.password= password; }}

    1234567891011121314

  • 7/24/2019 MVC Architecture With Servlets and Jsp - TheJavaGeek

    4/21

    f orm at ti ng dat es us ing Si mpl eDat eFormat Pas s by v alue or pas s by ref erenc e?

    Tags: architecture eclipse javaee mvc request response servlets tomcat

    Let us understand what happens under the hood with the help of diagram.

    1. First, user visits login.jsp page and fills out data and submits form.

    2. This causes to be invoked because of

    3. In , following code causes the model to be invoked and set the properties.

    and .

    4. causes the servlet to forward to jsp page.

    success.jsp page displays the username with expression language

    Notable advantages of mvc pattern are:

    This separate presentation layer from business layer.

    The controller performs action of invoking the model and sending data to view.

    Model is not even aware that it is used by some web application or desktop application. Authenticator

    class can be used by desktop applications also. Thus separation helps in re-usability.

    Hope this helps in understanding how to create MVC application using servlets and jsp.

    Related Posts

    111 comments for MVC architecture with servlets and jsp

    LoginController formaction="LoginController".

    LoginController User

    User user= newUser(username,password);

    rd.forward(request,response);

    Welcome

    ${requestScope['user'].username}.

    Servlet listeners :

    ServletContextList

    ener example.

    Servlet listeners :

    ServletRequestLis

    tener

    Servlet listeners :

    HttpSessionListen

    er example

    RequestDispatch

    er forward method

    RequestDispatch

    er include method

    12

    Authenticator authenticator= newAuthenticator();Stringresult= authenticator.authenticate(username,password);

    Reply

    Sudhir KumarNovember 21, 2013 at 3:16 am

    superb..

    Prasad Kharkar

    http://www.thejavageek.com/2013/08/11/mvc-architecture-with-servlets-and-jsp/?replytocom=9404#respondhttp://www.thejavageek.com/2013/12/11/requestdispatcher-include-method/http://www.thejavageek.com/2013/12/11/requestdispatcher-include-method/http://www.thejavageek.com/2013/12/10/requestdispatcher-forward-method/http://www.thejavageek.com/2013/12/10/requestdispatcher-forward-method/http://www.thejavageek.com/2013/10/27/servlet-listeners-httpsessionlistener-example/http://www.thejavageek.com/2013/10/27/servlet-listeners-httpsessionlistener-example/http://www.thejavageek.com/2013/10/27/servlet-listeners-servletrequestlistener/http://www.thejavageek.com/2013/10/27/servlet-listeners-servletrequestlistener/http://www.thejavageek.com/2013/10/24/servlet-listeners-servletcontextlistener-example/http://www.thejavageek.com/2013/10/24/servlet-listeners-servletcontextlistener-example/http://www.hupso.com/share/http://www.thejavageek.com/wp-content/uploads/2013/08/MVCFlow1.jpghttp://www.thejavageek.com/tag/tomcat/http://www.thejavageek.com/tag/servlets/http://www.thejavageek.com/tag/response/http://www.thejavageek.com/tag/request/http://www.thejavageek.com/tag/mvc/http://www.thejavageek.com/tag/javaee/http://www.thejavageek.com/tag/eclipse/http://www.thejavageek.com/tag/architecture/http://www.thejavageek.com/2013/08/24/pass-by-value-or-pass-by-reference/http://www.thejavageek.com/2013/08/06/formatting-dates-using-simpledateformat/
  • 7/24/2019 MVC Architecture With Servlets and Jsp - TheJavaGeek

    5/21

    Reply

    November 21, 2013 at 10:34 am

    Thank you Sudhir.

    Reply

    sruthiMay 14, 2015 at 10:14 pm

    Hi , Thank you very much for the information.In the above Example in the Authenticatclass we have password and username .How to authenticate if they are stored in database in

    the above example.Thank you very much.

    Reply

    Prasad KharkarMay 15, 2015 at 9:59 am

    At beg inner level, you can either create database connection from web application

    and read username and password from servlet and then check for equality. Or you can

    go for a better way of using Java Authentication and Authorization Service which can be

    used with tomcat, jboss or wildfly. Please follow these links

    jaas with mysql and jboss

    jaas with mysql and tomcat

    Reply

    Sudhir KumarNovember 21, 2013 at 3:18 am

    can you explain more about Struts,Hibernate,Spring with MVC and Front Controler Design

    Pattern .

    Reply

    Prasad KharkarNovember 21, 2013 at 10:50 am

    Yes Sudhir, tutorials about them will definitely come soon. I am working on them. Thank you

    for reading and showing interest.

    Reply

    Sudhir KumarNovember 22, 2013 at 10:25 pm

    can tell about, how to practice best because giving answer in interview like real world is difficult

    and developing any application from scratch is difficult for me at home. actually i want to use all

    java/j2ee,str uts,hiberna te,spring,DB (some part of XML & WebService also) as Hom e wor k. so can you

    give any idea or, can you give Question for developing small application which gives real world

    meaning to become exp.

    Sudhir KumarNovember 22, 2013 at 11:59 pm

    can you tell about, how to practice best because giving answer in interview like real world is

    difficult .

    http://www.thejavageek.com/2013/08/11/mvc-architecture-with-servlets-and-jsp/?replytocom=9804#respondhttp://www.thejavageek.com/2013/08/11/mvc-architecture-with-servlets-and-jsp/?replytocom=9466#respondhttp://www.thejavageek.com/2013/08/11/mvc-architecture-with-servlets-and-jsp/?replytocom=9405#respondhttp://www.thejavageek.com/2013/07/07/configure-jdbcrealm-jaas-for-mysql-and-tomcat-7-with-form-based-authentication/http://www.thejavageek.com/2013/09/18/configure-jaas-jboss-7-1-mysql/http://www.thejavageek.com/http://www.thejavageek.com/2013/08/11/mvc-architecture-with-servlets-and-jsp/?replytocom=41592#respondhttp://www.thejavageek.com/2013/08/11/mvc-architecture-with-servlets-and-jsp/?replytocom=41580#respondhttp://www.thejavageek.com/2013/08/11/mvc-architecture-with-servlets-and-jsp/?replytocom=9455#respond
  • 7/24/2019 MVC Architecture With Servlets and Jsp - TheJavaGeek

    6/21

    Reply

    Reply

    Prasad KharkarNovember 23, 2013 at 1:03 am

    I would recommend you to take up a sample project and implement it using MVC

    architecture so that you will get to know the difficulties faced during development. For example,

    create a simple user registration form in which fields are entered on a JSP, access them in a

    servlet and again display the list of all registered users on a separate JSP page. Use MVCarchitecture in it.

    Reply

    suhailMarch 24, 2014 at 1:52 pm

    good one thank u so much.

    Reply

    sundypApril 23, 2014 at 12:30 pm

    very helpful.. finding for this for a long.. expecting more topics from u..

    Reply

    Prasad KharkarApril 23, 2014 at 1:11 pm

    Thank you Sandyp for good feedback I will fulfil your expectations

    Reply

    MD NISHAD HUSSAINJune 5, 2014 at 5:59 pm

    hello sir,

    i have started learning jsp and servletand at the same time doing project in both the technologies

    So will you kindly tell me what would be the best start for me and what are the main topics in both

    which will help me in making project..

    Reply

    Prasad KharkarJune 5, 2014 at 11:59 pm

    Hi Nishad, now that you have learned MVC architecture well, learn about servlets, listeners

    and other web components. I would recommend you to use JSF if it is possible as it is event

    based framework.

    Reply

    MD NISHAD HUSSAINJune 7, 2014 at 11:58 am

    thanks sir but i have no idea about jsf as of nowso will u tell me how .?

    http://www.thejavageek.com/2013/08/11/mvc-architecture-with-servlets-and-jsp/?replytocom=24118#respondhttp://www.thejavageek.com/http://www.thejavageek.com/2013/08/11/mvc-architecture-with-servlets-and-jsp/?replytocom=24020#respondhttp://www.thejavageek.com/2013/08/11/mvc-architecture-with-servlets-and-jsp/?replytocom=23989#respondhttp://www.thejavageek.com/2013/08/11/mvc-architecture-with-servlets-and-jsp/?replytocom=21149#respondhttp://www.thejavageek.com/2013/08/11/mvc-architecture-with-servlets-and-jsp/?replytocom=21148#respondhttp://www.thejavageek.com/2013/08/11/mvc-architecture-with-servlets-and-jsp/?replytocom=19152#respondhttp://www.thejavageek.com/2013/08/11/mvc-architecture-with-servlets-and-jsp/?replytocom=9841#respondhttp://www.thejavageek.com/2013/08/11/mvc-architecture-with-servlets-and-jsp/?replytocom=9821#respond
  • 7/24/2019 MVC Architecture With Servlets and Jsp - TheJavaGeek

    7/21

    Reply

    Prasad KharkarJune 7, 2014 at 9:41 pm

    Right now if you dont have idea about jsf, its good to start with servlets and JSP

    itself. JSF part can be done later once youve understood servlets and jsp

    MD NISHAD HUSSAINJune 10, 2014 at 9:22 am

    okthanks sir.

    Prasad KharkarJune 10, 2014 at 2:46 pm

    Happy learning

    NishadJune 11, 2014 at 6:24 pm

    Hi,

    sir i am getting a bit confused regarding jsp like it eventually change into servlet

    then what is the need of taking jsp..if i want to make a design form for user entry

    like name,sex address in jsp and more on that using jsp only so can i make it or

    not..

    plzz explain

    MD NISHAD HUSSAINJune 11, 2014 at 6:28 pm

    and if i want to run an applet code in eclipse then is it possible to run or

    not.

    if i make a ui form using applet then it would be a good idea..????

    Reply

    sravaniJune 5, 2014 at 9:41 pm

    Excellent Mr.Prasad, i have looking for this kind of explanation so far. can you please post some

    more examples

    Reply

    Prasad KharkarJune 5, 2014 at 11:54 pm

    Sravani, thank you for positive feedback :). I am constantly adding some nice articles. You

    can like theJavaGeek page on facebook for regular updates. I would also like to receive a

    feedback from you for any improvements and suggestions.

    http://www.thejavageek.com/http://www.thejavageek.com/2013/08/11/mvc-architecture-with-servlets-and-jsp/?replytocom=24018#respondhttp://www.thejavageek.com/2013/08/11/mvc-architecture-with-servlets-and-jsp/?replytocom=24003#respondhttp://www.thejavageek.com/http://www.thejavageek.com/http://www.thejavageek.com/2013/08/11/mvc-architecture-with-servlets-and-jsp/?replytocom=24147#respond
  • 7/24/2019 MVC Architecture With Servlets and Jsp - TheJavaGeek

    8/21

    Reply

    KishanJune 6, 2014 at 1:45 am

    ThanQ sirreally itz useful to me as am doing a project now in my training periodam

    searching these kind of neat explaination thanq once againand i want you to give more tutorials on

    how to connect db for update profile and i have 2 questions 1) am using NetBeans IDEso any

    difference between eclipse am i hv to do any modification from ur tutorial?

    2) how to get photo as a input for update profile page ThanQ in advance

    Reply

    Prasad KharkarJune 6, 2014 at 10:36 am

    1. Though you are using netbeans, it may have a different directory structure, so instead of

    webcontent directory, it can have other one. The rules for a web application do not change. You

    just n eed to create a dynamic web project in ne tbeans and follow t he process given in th is tut orial

    2. You will have to use file upload functionality for uploading photo and while displaying, I

    recommend you simply store the uploaded photo on hard drive and put the location of photo in

    database. While displaying photo for profile, you can pick photo location from database and

    display on web page.

    I will certainly write more tutorials as per your suggestion Thank you for reading a positive

    feedback. Happy learning.

    Reply

    MD NISHAD HUSSAINJune 11, 2014 at 6:35 pm

    sir,it would be a great favour if you help me in my project..if you allow me then i will keep

    posting the queries and you guide me how to do it.my project title is Student Mangement

    SYstemits a live project..want to use jsp and servlet.plz kindly guide me step by step..

    thanks and regards,

    Nishad

    Reply

    Prasad KharkarJune 11, 2014 at 11:23 pm

    Hi Nishad, I am sure this link will tell you why you should JSP. You can always post queries

    about your application and I will try my best to help you out.

    Reply

    MD NISHAD HUSSAINJune 12, 2014 at 12:32 am

    ok thanks sir.

    Reply

    MD NISHAD HUSSAINJune 25, 2014 at 8:45 am

    Comparison of MVC implementation between J2EE and ASP.NET, Who is the best?

    what is the difference between the two.?

    http://www.thejavageek.com/2013/08/11/mvc-architecture-with-servlets-and-jsp/?replytocom=25552#respondhttp://www.thejavageek.com/2013/08/11/mvc-architecture-with-servlets-and-jsp/?replytocom=24417#respondhttp://www.thejavageek.com/2013/12/30/advantages-jsp-servlets/http://www.thejavageek.com/http://www.thejavageek.com/2013/08/11/mvc-architecture-with-servlets-and-jsp/?replytocom=24412#respondhttp://www.thejavageek.com/2013/08/11/mvc-architecture-with-servlets-and-jsp/?replytocom=24398#respondhttp://www.thejavageek.com/http://www.thejavageek.com/2013/08/11/mvc-architecture-with-servlets-and-jsp/?replytocom=24049#respondhttp://www.thejavageek.com/2013/08/11/mvc-architecture-with-servlets-and-jsp/?replytocom=24027#respond
  • 7/24/2019 MVC Architecture With Servlets and Jsp - TheJavaGeek

    9/21

    Reply

    randyJuly 24, 2014 at 3:35 pm

    How can I connect a already completed JSP with a Oracle database into a MVC?

    Reply

    Prasad KharkarJuly 25, 2014 at 9:25 am

    You can create a class which returns the database connection and use that object into

    business logic i.e. model.

    Reply

    zchumagerJuly 26, 2014 at 10:50 pm

    Great tutorial, just one error. On the jsp file the line

    Welcome ${requestScope[user].username}

    should be

    Welcome ${requestScope[user].getUsername()}

    because your attribute is private and you have a getter for it

    Reply

    MADHUJuly 31, 2014 at 11:32 pm

    Prasad broawesome explanatn .Tq so much and can u please update some more example on

    each topic of servlets and jsp so that would be better for beginners.

    Reply

    Prasad KharkarAugust 1, 2014 at 10:35 am

    Hi Madhu, thank you for good feedback. You can find good tutorials for servlets and jsp

    right in the menu. Here

    Reply

    MADHUAugust 7, 2014 at 11:36 pm

    Thank you broand core java concepts what you have given is that enough or we

    need to learn more ? Because i want put one year virtual experience on Java so is the

    concepts you explained core java and j2ee is sufficient ?

    Reply

    Prasad KharkarAugust 8, 2014 at 7:45 am

    Knowledge is never enough but my articles will definitely be helpful.

    http://www.thejavageek.com/http://www.thejavageek.com/2013/08/11/mvc-architecture-with-servlets-and-jsp/?replytocom=28735#respondhttp://www.thejavageek.com/2013/08/11/mvc-architecture-with-servlets-and-jsp/?replytocom=28721#respondhttp://www.thejavageek.com/java-servlets-jsp-tutorials/http://www.thejavageek.com/http://www.thejavageek.com/2013/08/11/mvc-architecture-with-servlets-and-jsp/?replytocom=28242#respondhttp://www.thejavageek.com/2013/08/11/mvc-architecture-with-servlets-and-jsp/?replytocom=28210#respondhttp://geekalt42.net/http://www.thejavageek.com/2013/08/11/mvc-architecture-with-servlets-and-jsp/?replytocom=27868#respondhttp://www.thejavageek.com/http://www.thejavageek.com/2013/08/11/mvc-architecture-with-servlets-and-jsp/?replytocom=27748#respondhttp://www.thejavageek.com/2013/08/11/mvc-architecture-with-servlets-and-jsp/?replytocom=27706#respond
  • 7/24/2019 MVC Architecture With Servlets and Jsp - TheJavaGeek

    10/21

    Reply

    vamsiAugust 14, 2014 at 7:48 pm

    The explanation was excellent. I am now able to understand MVC architecture very well. Could

    you please post some examples on CRUD operations involving MVC architecture. That is one area

    that I want to improve.

    Reply

    Debaprio BanikAugust 22, 2014 at 5:34 pm

    Nicely put. Can you please explain a bit more detail on how the login.jsp gets loaded first?

    Reply

    Prasad KharkarAugust 22, 2014 at 7:18 pm

    I have directly visited the url for login.jsp

    Reply

    manisha agarwalSeptember 6, 2014 at 2:40 pm

    please explain the use of jstl.jar

    Reply

    Snigdhodeb MitraSeptember 24, 2014 at 11:54 pm

    Sir,

    I am a fresher btech IT . I have started learning J2EE for the past few weeks . I am facing some

    problem in structuring a MCV pattern in java . It would be very helpful if you can please guide me in

    this and if can send me a small example of the basic functionalities of INSERT, UPDATE , and DELETE

    features using Mysql in a MVC format . I would me eagerly waiting for you reply and would be very

    thankful if you can make some time form your busy schedule and help me

    Reply

    Prasad KharkarSeptember 25, 2014 at 8:32 am

    Hi Snigdhodeb, Please let me know the problems you are facing while creating application

    using mvc pattern. I will defintiely help you with it.

    Reply

    krishnaOctober 16, 2014 at 8:55 pm

    Nice work prasad,Simpe but perfect

    http://www.thejavageek.com/2013/08/11/mvc-architecture-with-servlets-and-jsp/?replytocom=33385#respondhttp://www.thejavageek.com/http://www.thejavageek.com/2013/08/11/mvc-architecture-with-servlets-and-jsp/?replytocom=31852#respondhttp://www.thejavageek.com/2013/08/11/mvc-architecture-with-servlets-and-jsp/?replytocom=31834#respondhttp://www.thejavageek.com/2013/08/11/mvc-architecture-with-servlets-and-jsp/?replytocom=30739#respondhttp://www.thejavageek.com/http://www.thejavageek.com/2013/08/11/mvc-architecture-with-servlets-and-jsp/?replytocom=29669#respondhttp://www.thejavageek.com/2013/08/11/mvc-architecture-with-servlets-and-jsp/?replytocom=29661#respondhttp://www.thejavageek.com/2013/08/11/mvc-architecture-with-servlets-and-jsp/?replytocom=29185#respond
  • 7/24/2019 MVC Architecture With Servlets and Jsp - TheJavaGeek

    11/21

    Reply

    Prasad KharkarOctober 17, 2014 at 11:42 am

    Hi Krishna, thank you for reading. I am glad it was useful for you.

    Pingback: Coffee Advisor web application | Nigoutsi's Local Site

    Pingback: Coffee Advisor web application | AM0335

    Reply

    FloraOctober 27, 2014 at 9:38 pm

    Thank u.the article is very useful.After reading so many articles on other website I found this

    one neat,clear and to the point.I am from non cs background and now working in IT company and

    undergoing training in java.I think this will help me to clear my doubts and make me love coding

    Reply

    Prasad KharkarOctober 28, 2014 at 8:57 am

    Hi Flora, I am glad this was useful for you. Happy learning

    Reply

    AkilaOctober 28, 2014 at 11:59 am

    Thank You Very much I got a clear idea on MVC model.

    It was very useful and easy to understand.. Keep Going..

    Reply

    SandeepNovember 5, 2014 at 1:30 pm

    Hi Prasad do you have any other example in which a form has many input values and POJO

    model in such case.

    Reply

    Prasad KharkarNovember 5, 2014 at 11:05 pm

    This form already contains two values in it. Could you please tell me what exactly you want

    to ask when you say it has many input values and POJO model in such case?

    Reply

    SantoshNovember 10, 2014 at 11:56 am

    great article sir thank you so much

    http://www.thejavageek.com/2013/08/11/mvc-architecture-with-servlets-and-jsp/?replytocom=34835#respondhttp://www.thejavageek.com/http://www.thejavageek.com/2013/08/11/mvc-architecture-with-servlets-and-jsp/?replytocom=34570#respondhttp://www.thejavageek.com/2013/08/11/mvc-architecture-with-servlets-and-jsp/?replytocom=34558#respondhttp://www.thejavageek.com/2013/08/11/mvc-architecture-with-servlets-and-jsp/?replytocom=34175#respondhttp://www.thejavageek.com/http://www.thejavageek.com/2013/08/11/mvc-architecture-with-servlets-and-jsp/?replytocom=34167#respondhttp://www.thejavageek.com/2013/08/11/mvc-architecture-with-servlets-and-jsp/?replytocom=34133#respondhttp://am0335.wordpress.com/2014/10/26/coffee-advisor-web-application/http://83.212.123.61/coffee-advisor-web-application/http://www.thejavageek.com/http://www.thejavageek.com/2013/08/11/mvc-architecture-with-servlets-and-jsp/?replytocom=33453#respond
  • 7/24/2019 MVC Architecture With Servlets and Jsp - TheJavaGeek

    12/21

    Reply

    NageswarNovember 28, 2014 at 12:22 pm

    Thank u prasad ,i got clear idea on mvc..please post more tutorials

    Reply

    Prasad Kharkar

    November 29, 2014 at 11:47 pm

    I am glad it was useful for you. I will surely keep writing Happy learning

    Reply

    abhinay jainNovember 29, 2014 at 12:01 pm

    very simple and easy to understand..

    Its a good example.Thank you Prasad .

    Prasad i have a quary corresponding to requestDispatcher

    can u explain me in detail

    Reply

    Prasad KharkarNovember 29, 2014 at 11:46 pm

    I am glad it was useful for you What is your query?

    Reply

    RuthvikJanuary 5, 2015 at 8:30 pm

    hi prasad,

    its a good example but if want to connect to a database how should it be done? can u help me with a

    example where the queries should be written?

    Reply

    Prasad KharkarJanuary 9, 2015 at 5:13 pm

    Hi Ruthvik, I didnt exactly get what you wanted to say. Could you please elaborate?

    anushaJanuary 10, 2015 at 11:19 am

    Hi,

    The tutorial is very good. I tried the same but m getting error in my LoginController.java.

    The error is here . It is sayinf Cannot instantiate the type authenticator at the first line and The

    method authenticate(Stirng,String) is undefined for the type Authenticator.

    Authenticator authenticat or = new Authenticator( )

    String result = authenticator.authenticate(username,password)

    please check and give me the reply asap.

    http://www.thejavageek.com/http://www.thejavageek.com/2013/08/11/mvc-architecture-with-servlets-and-jsp/?replytocom=37863#respondhttp://www.thejavageek.com/2013/08/11/mvc-architecture-with-servlets-and-jsp/?replytocom=37741#respondhttp://www.thejavageek.com/http://www.thejavageek.com/2013/08/11/mvc-architecture-with-servlets-and-jsp/?replytocom=35900#respondhttp://www.thejavageek.com/2013/08/11/mvc-architecture-with-servlets-and-jsp/?replytocom=35863#respondhttp://www.thejavageek.com/http://www.thejavageek.com/2013/08/11/mvc-architecture-with-servlets-and-jsp/?replytocom=35901#respondhttp://www.thejavageek.com/2013/08/11/mvc-architecture-with-servlets-and-jsp/?replytocom=35779#respond
  • 7/24/2019 MVC Architecture With Servlets and Jsp - TheJavaGeek

    13/21

    Reply

    My IDE:Eclipse luna

    JDK7

    Tomcat 8

    Reply

    Prasad KharkarJanuary 10, 2015 at 5:12 pm

    It seems your container is not able to recognize Authenticator class. Could you please post

    full stacktrace?

    Reply

    akashJune 2, 2015 at 10:18 pm

    Hi,

    I think you have imported wrong class file.

    Please import mvcdemo.model.Authenticator

    Reply

    PriyaJanuary 18, 2015 at 7:13 pm

    Superb

    Reply

    RAVIJanuary 22, 2015 at 9:18 pm

    very good tutorial..superb prasad keep it up..:

    Reply

    JamesFebruary 9, 2015 at 2:27 am

    Hi Prasad,great document you have written here,keep up the good work.I have tried all the

    mentioned steps,but when i hit the URL http://localhost:8080/MVCDemo/login.jsp i am getting a

    requested resource is not available, page not found error.Can you please help me out here.Many

    thanks.

    Reply

    Prasad KharkarFebruary 9, 2015 at 9:40 pm

    please check whether your server is up by visiting locahost:8080.

    jugalFebruary 11, 2015 at 9:40 am

    Hello sir currently i working on the live project and ur tutorial is very useful to me

    but can u please help me in spring mvc give me some exaples and websites to learn online

    http://www.thejavageek.com/http://www.thejavageek.com/2013/08/11/mvc-architecture-with-servlets-and-jsp/?replytocom=38966#respondhttp://www.thejavageek.com/2013/08/11/mvc-architecture-with-servlets-and-jsp/?replytocom=38941#respondhttp://www.thejavageek.com/2013/08/11/mvc-architecture-with-servlets-and-jsp/?replytocom=38313#respondhttp://www.thejavageek.com/2013/08/11/mvc-architecture-with-servlets-and-jsp/?replytocom=38175#respondhttp://www.thejavageek.com/2013/08/11/mvc-architecture-with-servlets-and-jsp/?replytocom=42167#respondhttp://www.thejavageek.com/http://www.thejavageek.com/2013/08/11/mvc-architecture-with-servlets-and-jsp/?replytocom=37903#respondhttp://www.thejavageek.com/2013/08/11/mvc-architecture-with-servlets-and-jsp/?replytocom=37891#respond
  • 7/24/2019 MVC Architecture With Servlets and Jsp - TheJavaGeek

    14/21

    Reply

    thank you

    Reply

    kavitha.NFebruary 11, 2015 at 10:45 am

    thanks sir for the information ,its very much helpful to me

    i am fresher i dnt have any experience in java, but i want to start up projects frm home using java

    technology, so plz can u suggest me the things wat and all i have to learn to develop project in java

    Reply

    Prasad KharkarFebruary 12, 2015 at 5:32 pm

    Hi Kavitha, I am glad it was useful for you. First decide what kind of project you want to do,

    then will be able to tell.

    Reply

    saminMarch 3, 2015 at 12:10 am

    hello, pls tell what is the advantages to make jsp program by mvc struture rather then simple..

    Reply

    Prasad KharkarMarch 3, 2015 at 11:33 am

    Hi Samin, I think this linkshould answer your question.

    Pingback: MVC architecture with servlets and jsp - theJav...

    Reply

    ChrisMarch 9, 2015 at 4:23 am

    Hello Prasad, thank you for this tutorial! Can you please discuss how packaging works in

    servlets? I always seem to get errors when I create packages for my servlets, and then try to import

    other classes and use their functions.

    Thank you!

    Reply

    Prasad KharkarMarch 9, 2015 at 4:18 pm

    Hi Chris, there is no difference in servlet packages and normal package. Could you please

    post the errors you are getting? Ill try to resolve them

    azzmiMarch 14, 2015 at 3:10 pm

    http://www.thejavageek.com/http://www.thejavageek.com/2013/08/11/mvc-architecture-with-servlets-and-jsp/?replytocom=39644#respondhttp://www.thejavageek.com/2013/08/11/mvc-architecture-with-servlets-and-jsp/?replytocom=39635#respondhttp://www.scoop.it/t/andragogie/p/4038333825/2015/03/02/mvc-architecture-with-servlets-and-jsp-thejavageekhttp://www.thejavageek.com/2013/12/30/advantages-jsp-servlets/http://www.thejavageek.com/http://www.thejavageek.com/2013/08/11/mvc-architecture-with-servlets-and-jsp/?replytocom=39603#respondhttp://www.thejavageek.com/2013/08/11/mvc-architecture-with-servlets-and-jsp/?replytocom=39595#respondhttp://www.thejavageek.com/http://www.thejavageek.com/2013/08/11/mvc-architecture-with-servlets-and-jsp/?replytocom=39053#respondhttp://www.thejavageek.com/2013/08/11/mvc-architecture-with-servlets-and-jsp/?replytocom=39018#respondhttp://www.thejavageek.com/2013/08/11/mvc-architecture-with-servlets-and-jsp/?replytocom=39015#respond
  • 7/24/2019 MVC Architecture With Servlets and Jsp - TheJavaGeek

    15/21

    Reply

    very good article..regards

    Reply

    Shakour GhafarzoyMarch 17, 2015 at 4:51 pm

    .terrific MVC example thank u Sir

    Reply

    sachinMarch 20, 2015 at 9:48 pm

    owsm.

    Reply

    Ashish MahadikMarch 25, 2015 at 10:54 am

    Hi,

    Can you tell me if we want to print table in our jsp page and table value come from database and we

    use mvc architecture then it is correct to get all values in jsp page by directly write query in that jsp

    page or use servlet to get all values?

    Reply

    Prasad KharkarMarch 25, 2015 at 11:51 am

    Hi Ashish, jsps are meant for displaying purpose only. a JSP developer need not have

    knowledge of sql queries. Writing sql or java code in jsp would break the mvc pattern. It is not

    advisable to write logic in jsp. You should use servlet or further layers to call values fromdatabase.

    Reply

    Ashish MahadikMarch 25, 2015 at 2:49 pm

    Thanks Prasad ,

    it can clear all my concept about mvc pattern

    Reply

    Prasad KharkarMarch 26, 2015 at 10:56 am

    Im glad you found it useful. Happy learning Ashish

    Reply

    kapilMarch 28, 2015 at 11:46 am

    sir, i created that architectur but how to run it..

    plz specify here.

    http://www.thejavageek.com/2013/08/11/mvc-architecture-with-servlets-and-jsp/?replytocom=40063#respondhttp://www.thejavageek.com/http://www.thejavageek.com/2013/08/11/mvc-architecture-with-servlets-and-jsp/?replytocom=40007#respondhttp://www.thejavageek.com/2013/08/11/mvc-architecture-with-servlets-and-jsp/?replytocom=39991#respondhttp://www.thejavageek.com/http://www.thejavageek.com/2013/08/11/mvc-architecture-with-servlets-and-jsp/?replytocom=39986#respondhttp://www.thejavageek.com/2013/08/11/mvc-architecture-with-servlets-and-jsp/?replytocom=39984#respondhttp://www.thejavageek.com/2013/08/11/mvc-architecture-with-servlets-and-jsp/?replytocom=39873#respondhttp://www.thejavageek.com/2013/08/11/mvc-architecture-with-servlets-and-jsp/?replytocom=39789#respondhttp://www.thejavageek.com/2013/08/11/mvc-architecture-with-servlets-and-jsp/?replytocom=39734#respond
  • 7/24/2019 MVC Architecture With Servlets and Jsp - TheJavaGeek

    16/21

    Reply

    Prasad KharkarMarch 28, 2015 at 8:44 pm

    Hi Kapil, Ive already explained how to run it. What is the problem you are facing? Please let

    me know.

    Reply

    raviApril 1, 2015 at 1:35 pm

    superb tutorial. show me to database authentication of servlet mvc

    Reply

    aishwaryaApril 4, 2015 at 12:07 pm

    sir can u explain me if i dont use eclipse how can i proceed in notepad

    Reply

    Prasad KharkarApril 4, 2015 at 3:47 pm

    you will have to compile all java files, servlet,jsp separately with respective commands

    (Need some googling for it). After that you need to create war file and then deploy under tomcat

    Reply

    veenaApril 9, 2015 at 1:30 am

    Hi,

    I am trying to run the login Jsp , but I am facing 404 error also I am getting error for taglib in

    success.jsp.

    Please help.

    Reply

    Prasad KharkarApril 9, 2015 at 9:42 am

    please provide the stacktrace. Have you followed all the steps mentioned in the article?

    Reply

    MouradApril 14, 2015 at 4:57 pm

    Thanks for this tutorial. I have a problem, if we have two classes for example, User and Admin ,

    Admin extends User, how ca n we respect MVC arch itecture ? I mean in th at ca se we will have two

    beans, two classes for DAO access ? thanks for helping

    Ashish

    http://www.thejavageek.com/2013/08/11/mvc-architecture-with-servlets-and-jsp/?replytocom=40540#respondhttp://www.thejavageek.com/http://www.thejavageek.com/2013/08/11/mvc-architecture-with-servlets-and-jsp/?replytocom=40417#respondhttp://www.thejavageek.com/2013/08/11/mvc-architecture-with-servlets-and-jsp/?replytocom=40402#respondhttp://www.thejavageek.com/http://www.thejavageek.com/2013/08/11/mvc-architecture-with-servlets-and-jsp/?replytocom=40278#respondhttp://www.thejavageek.com/2013/08/11/mvc-architecture-with-servlets-and-jsp/?replytocom=40274#respondhttp://www.thejavageek.com/2013/08/11/mvc-architecture-with-servlets-and-jsp/?replytocom=40171#respondhttp://www.thejavageek.com/http://www.thejavageek.com/2013/08/11/mvc-architecture-with-servlets-and-jsp/?replytocom=40072#respond
  • 7/24/2019 MVC Architecture With Servlets and Jsp - TheJavaGeek

    17/21

    Reply

    April 18, 2015 at 8:01 pm

    Hello Prasad,

    I am confusing in MVC pattern that

    1. In project or MVC pattern how many servlet we can use in one project is it recommend that only one

    servlet is in all project or multiple we can use?what would be prefer.

    2.what difference between data transfer object class and data access object (DAO) class used in

    application.

    3.how we should hide DAO class from direct access in MVC pattern.

    Provide standard industrial coding procedure to me.

    Reply

    JuanApril 25, 2015 at 10:29 pm

    Would it be possible to download the project ? It would help

    Reply

    NaharMay 23, 2015 at 8:01 am

    Thanks for the tutorial prasad, but i have a problem which is when i compile 3 of that java file, i

    got this error from LoginController.java, package sun.text.normalizer.ICUBinary does not exist I

    dont know much about that package declaration Can u help me

    Reply

    MuthuMay 26, 2015 at 5:37 pm

    thanks for your example it is very nice.. i have one question regarding above

    example whether we can use form in the layer between jsp and servlet controller?

    UserForm userForm

    String username = userForm.getUsername()

    like this one..

    Reply

    Prasad KharkarMay 28, 2015 at 9:23 am

    The Form you are talking about is available in struts.

    Reply

    UtsavJune 9, 2015 at 12:51 pm

    Simply awesome example. Hats off to you Prasad ji

    HakeemJune 9, 2015 at 8:16 pm

    Thank you Mr. Prasad. Your explanation on MVC was simple yet very detailed and helpful.

    I am currently develop an application for the organization I work for , I am using JAVA and I have a

    couple quesitons that I hope you can answer.

    http://www.thejavageek.com/2013/08/11/mvc-architecture-with-servlets-and-jsp/?replytocom=42371#respondhttp://www.thejavageek.com/http://www.thejavageek.com/2013/08/11/mvc-architecture-with-servlets-and-jsp/?replytocom=42025#respondhttp://www.thejavageek.com/2013/08/11/mvc-architecture-with-servlets-and-jsp/?replytocom=41975#respondhttp://www.thejavageek.com/2013/08/11/mvc-architecture-with-servlets-and-jsp/?replytocom=41857#respondhttp://www.thejavageek.com/2013/08/11/mvc-architecture-with-servlets-and-jsp/?replytocom=40872#respondhttp://www.thejavageek.com/2013/08/11/mvc-architecture-with-servlets-and-jsp/?replytocom=40638#respond
  • 7/24/2019 MVC Architecture With Servlets and Jsp - TheJavaGeek

    18/21

    Reply

    Within the aformentioned application I want to implement Server Side validation, where would you put

    the said block of code, in the Model or the Controller class?

    Using your above example, lets assume that you wanted to ensure that the user entered both a

    password and username, would you put the validaiton block in the LoginController class or the

    Authenticator class?

    Secondly, what is the best way to implement User Privilleges and Permissions in JAVA using the MVC

    Design pattern? Do I implement an Intercepting filter and place the logic there or do I distrubute the

    logic between the view and model?

    Any ad vice would be greatly appreciate d. Thank you much in ad vance.

    Kind Regards,

    Hakeem

    Reply

    Prasad KharkarJune 9, 2015 at 8:21 pm

    Hi Hakeem, I am glad you found it useful. To be honest there is no IDEAL way of doing

    things. This tutorial only explains how mvc architecture can be done using servlets and JSP. For

    authentication and authorization, I believe you should use Java Authentication and Authorization

    Service. You can find some tutorials on my blog itself. I am sure they will help.

    Reply

    HakeemJune 10, 2015 at 2:42 am

    Wow, Prasad. Thank you so much for your quick response. I am going to look at your

    tutorials now. Also, do you know of any tutorials site I can read up on Java Authentication and

    Autorizaiton ?

    Reply

    prakharJune 25, 2015 at 4:42 pm

    How we can handle multiple request using single Controller(Servlet) like we do using filter in

    struts ? please explain with an example if possible.

    Reply

    Prasad KharkarJune 26, 2015 at 9:12 am

    There are some shortcomings with a simple MVC architecture in servlets and jsp and that is

    why struts provides that functionality

    Reply

    SahaJuly 6, 2015 at 5:31 pm

    How to start learning JSP, Servlet?I got training in J2EE tech, but it went over my head. Cz, I am a slow learner!

    Now, I am trying to learn it myself. Please suggest me.

    And a lso, I got severely confused becoz, they taught MVC, DAO without explaining well.

    Tell me about that also.

    http://www.thejavageek.com/2013/08/11/mvc-architecture-with-servlets-and-jsp/?replytocom=42931#respondhttp://www.thejavageek.com/http://www.thejavageek.com/2013/08/11/mvc-architecture-with-servlets-and-jsp/?replytocom=42678#respondhttp://www.thejavageek.com/2013/08/11/mvc-architecture-with-servlets-and-jsp/?replytocom=42665#respondhttp://www.thejavageek.com/2013/08/11/mvc-architecture-with-servlets-and-jsp/?replytocom=42389#respondhttp://www.thejavageek.com/http://www.thejavageek.com/2013/08/11/mvc-architecture-with-servlets-and-jsp/?replytocom=42382#respondhttp://www.thejavageek.com/2013/08/11/mvc-architecture-with-servlets-and-jsp/?replytocom=42381#respond
  • 7/24/2019 MVC Architecture With Servlets and Jsp - TheJavaGeek

    19/21

    Reply

    AnishJuly 7, 2015 at 10:53 pm

    Hello! Sir.Myself Anish.I want to know how to retrieve all record from a table on a view page

    such as jsp.After retrieved we make the all the first rows provide a linklike as when we access the

    irctc rail site there we check the train and after checking if valid train all info then it render the all train

    details.when we click on the train no by link then it moved us book page thats so on..please tell me

    how to implement in my project this proceedure..ok

    Reply

    AditiJuly 13, 2015 at 1:07 pm

    Hi Prasad,

    This was very helpful. Thanks a bunch!

    Reply

    KannanJuly 13, 2015 at 3:52 pm

    Hello Prasad,

    Can you tell me which is the business logic in this example?

    Reply

    Prasad KharkarJuly 14, 2015 at 8:48 am

    Hi Kannan, this is just a sample mvc architecture which elaborates different layers.

    Business logic term refers to the code which fulfills functionality. Although you can consider themodel layer as business layer here.

    Reply

    logeshkumarOctober 28, 2015 at 2:45 pm

    sir i need a solution for creating web app in framework-spring,hibernate,jsf

    Reply

    Prasad KharkarOctober 29, 2015 at 10:50 am

    Hi Logesh,

    Please tell me the problem you are facing while building the app, I will try my best to help

    Reply

    srikanth addaniJuly 15, 2015 at 4:31 pm

    very good architecture..

    http://www.thejavageek.com/2013/08/11/mvc-architecture-with-servlets-and-jsp/?replytocom=43233#respondhttp://www.thejavageek.com/http://www.thejavageek.com/2013/08/11/mvc-architecture-with-servlets-and-jsp/?replytocom=47675#respondhttp://www.thejavageek.com/2013/08/11/mvc-architecture-with-servlets-and-jsp/?replytocom=47655#respondhttp://www.thejavageek.com/http://www.thejavageek.com/2013/08/11/mvc-architecture-with-servlets-and-jsp/?replytocom=43203#respondhttp://www.thejavageek.com/2013/08/11/mvc-architecture-with-servlets-and-jsp/?replytocom=43180#respondhttps://www.linkedin.com/in/aditisilawathttp://www.thejavageek.com/2013/08/11/mvc-architecture-with-servlets-and-jsp/?replytocom=43176#respondhttps://www.facebook.com/anish1571994http://www.thejavageek.com/2013/08/11/mvc-architecture-with-servlets-and-jsp/?replytocom=42987#respondhttp://www.thejavageek.com/2013/08/11/mvc-architecture-with-servlets-and-jsp/?replytocom=42931#respond
  • 7/24/2019 MVC Architecture With Servlets and Jsp - TheJavaGeek

    20/21

    Leave a Reply

    Your email address will not be published. Required fields are marked *

    Name *

    Email *

    Website

    Comment

    Post Comment

    Copyright 2015 theJavaGeek. All Rights Reserved.

    Reply

    SuhasAugust 6, 2015 at 12:23 pm

    Thanks for good explanation

    Reply

    AjitAugust 7, 2015 at 11:34 am

    Hello sir ,

    i am fresher , i dont know actual working of mvc

    can u tell me how to implement servlet , jsp and how to get the connection

    Reply

    subodhAugust 10, 2015 at 12:47 pm

    sir..May i write multiple method in one servlet..

    if yes then how can we call from html that which particular one is getting to call

    Reply

    yanyanAugust 11, 2015 at 9:22 am

    would you explain the way that I do not need to use the build path to have servlet.jar for my

    program.

    Reply

    Gao L-JOctober 11, 2015 at 7:19 pm

    Thanks for your sharing! I am learning web service to build a Big data ecosystem between my

    Hadoop system and application. Your MVC JSP tutorial helps me!

    http://glj8989332.blogspot.tw/http://www.thejavageek.com/2013/08/11/mvc-architecture-with-servlets-and-jsp/?replytocom=47163#respondhttp://www.thejavageek.com/2013/08/11/mvc-architecture-with-servlets-and-jsp/?replytocom=44423#respondhttp://www.thejavageek.com/2013/08/11/mvc-architecture-with-servlets-and-jsp/?replytocom=44384#respondhttp://www.thejavageek.com/2013/08/11/mvc-architecture-with-servlets-and-jsp/?replytocom=44220#respondhttp://www.thejavageek.com/2013/08/11/mvc-architecture-with-servlets-and-jsp/?replytocom=44176#respondhttp://www.thejavageek.com/
  • 7/24/2019 MVC Architecture With Servlets and Jsp - TheJavaGeek

    21/21

    Quick Links

    Core JavaServlets & Jsp

    JPA 2.0MongoDBOO & DAbout MeContact Me

    2015 thejavageek.com - All rights reserved.

    Privacy Policy

    heJavaGeek

    898 likesLike Page

    https://www.facebook.com/TheJavaGeek/https://www.facebook.com/TheJavaGeek/https://www.facebook.com/TheJavaGeek/http://www.thejavageek.com/privacy.htmlhttp://www.thejavageek.com/contact/http://www.thejavageek.com/about-me/http://www.thejavageek.com/object-oriented-analysis-and-design/http://www.thejavageek.com/mongodb/http://www.thejavageek.com/jpa-tutorials/http://www.thejavageek.com/java-servlets-jsp-tutorials/http://www.thejavageek.com/core-java/