MVC architecture with servlets and jsp

The following two tabs change content below.
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.

Latest posts by Prasad Kharkar (see all)

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.
  • Controller is the component which is responsible for communication between model and view.

A user always sees the view and communicates with the controller. We will understand this using a sample login application 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 business logic for authentication
  • User.java :- Stores username and password for the user.

Requirements:

  • Eclipse IDE
  • Apache tomcat server
  • JSTL 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

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.

 

File Structure

File Structure

Package Structure

Package Structure

 

 

 

Now that we have file structure, put this code in corresponding files.

 

Authenticator.java

User.java

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.

loginForm

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

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

MVCFlow

  1. First, user visits login.jsp page and fills out data and submits form.
  2. This causesΒ  LoginControllerΒ  to be invoked because of form action="LoginController" .
  3. In LoginController , following code causes the model to be invoked and set the User properties.

    and User user = new User(username, password); .
  4. rd.forward(request, response); causes the servlet to forward to jsp page.

success.jsp page displays the username with expression language Welcome ${requestScope['user'].username}.

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.

Share Button

180 comments for “MVC architecture with servlets and jsp

  1. Sudhir Kumar
    November 21, 2013 at 3:16 am

    superb..

    • Prasad Kharkar
      November 21, 2013 at 10:34 am

      Thank you Sudhir.

      • sruthi
        May 14, 2015 at 10:14 pm

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

        • May 15, 2015 at 9:59 am

          At beginner 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

      • bhavana chowdary
        February 17, 2017 at 11:57 am

        Hi Prasad

        thank you for your tutorial. But, can you help me with an example of JSP and Database connectivity.

      • ranjith
        August 28, 2017 at 12:08 pm

        very useful and systematic presentation for beginners and pros…..
        you are a good presenter
        thank you very much

    • kirankumar
      February 23, 2016 at 11:18 am

      very nice Explanation Prasad

    • May 26, 2016 at 4:23 pm

      very good

    • Sudhir Kumar
      July 5, 2016 at 10:24 pm

      Thanks

    • P.venkata rami reddy
      September 14, 2016 at 7:40 pm

      I understand easily
      Thank you sir

  2. Sudhir Kumar
    November 21, 2013 at 3:18 am

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

    • Prasad Kharkar
      November 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.

      • May 26, 2016 at 4:27 pm

        sir i was make project online testing system on MVC but Occurred one error.
        all function well in my project but next question not going sir i am suffering it.

        • May 26, 2016 at 5:40 pm

          Please check your exception logs, you’ll see something, you can post it here. I will try to help.

  3. Sudhir Kumar
    November 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,struts,hibernate,spring,DB (some part of XML & WebService also) as Home work. so can you give any idea or, can you give Question for developing small application which gives real world meaning to become exp.

  4. Sudhir Kumar
    November 22, 2013 at 11:59 pm

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

    • Prasad Kharkar
      November 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 MVC architecture in it.

  5. suhail
    March 24, 2014 at 1:52 pm

    good one… thank u so much….

  6. sundyp
    April 23, 2014 at 12:30 pm

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

    • Prasad Kharkar
      April 23, 2014 at 1:11 pm

      Thank you Sandyp for good feedback πŸ™‚ I will fulfil your expectations πŸ™‚

  7. MD NISHAD HUSSAIN
    June 5, 2014 at 5:59 pm

    hello sir,
    i have started learning jsp and servlet…and 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..

    • June 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.

      • MD NISHAD HUSSAIN
        June 7, 2014 at 11:58 am

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

        • June 7, 2014 at 9:41 pm

          Right now if you don’t have idea about jsf, its good to start with servlets and JSP itself. JSF part can be done later once you’ve understood servlets and jsp πŸ™‚

          • MD NISHAD HUSSAIN
            June 10, 2014 at 9:22 am

            ok…thanks sir….

          • June 10, 2014 at 2:46 pm

            Happy learning πŸ™‚

          • Nishad
            June 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 HUSSAIN
            June 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………..????

  8. sravani
    June 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

    • June 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.

  9. Kishan
    June 6, 2014 at 1:45 am

    ThanQ sir…really itz useful to me as am doing a project now in my training period…am searching these kind of neat explaination thanq once again…and i want you to give more tutorials on how to connect db for update profile and i have 2 questions 1) am using NetBeans IDE…so 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…

    • June 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 need to create a dynamic web project in netbeans and follow the process given in this tutorial
      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.

  10. MD NISHAD HUSSAIN
    June 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 SYstem”…its a live project…..want to use jsp and servlet….plz kindly guide me step by step…..

    thanks and regards,
    Nishad

    • June 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.

      • MD NISHAD HUSSAIN
        June 12, 2014 at 12:32 am

        ok thanks sir….

      • Syed
        September 21, 2016 at 3:03 pm

        Hii Prasad am I trying to intergrate jsp form and servlets via webservices for passing values from Jsp to servlets
        Suggest me some with example

  11. MD NISHAD HUSSAIN
    June 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….?

  12. randy
    July 24, 2014 at 3:35 pm

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

    • July 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.

  13. July 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

  14. MADHU
    July 31, 2014 at 11:32 pm

    Prasad bro…awesome 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….

    • August 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

      • MADHU
        August 7, 2014 at 11:36 pm

        Thank you bro…and 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 ?

        • August 8, 2014 at 7:45 am

          Knowledge is never enough πŸ™‚ but my articles will definitely be helpful.

  15. vamsi
    August 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.

  16. Debaprio Banik
    August 22, 2014 at 5:34 pm

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

    • August 22, 2014 at 7:18 pm

      I have directly visited the url for login.jsp

  17. manisha agarwal
    September 6, 2014 at 2:40 pm

    please explain the use of jstl.jar

  18. Snigdhodeb Mitra
    September 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

    • September 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.

  19. krishna
    October 16, 2014 at 8:55 pm

    Nice work prasad,Simpe but perfect

    • October 17, 2014 at 11:42 am

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

  20. Flora
    October 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

    • October 28, 2014 at 8:57 am

      Hi Flora, I am glad this was useful for you. Happy learning πŸ™‚

  21. Akila
    October 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…………..

  22. Sandeep
    November 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.

    • November 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?

  23. Santosh
    November 10, 2014 at 11:56 am

    great article sir thank you so much

  24. Nageswar
    November 28, 2014 at 12:22 pm

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

    • November 29, 2014 at 11:47 pm

      I am glad it was useful for you. I will surely keep writing πŸ™‚ Happy learning

  25. abhinay jain
    November 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

    • November 29, 2014 at 11:46 pm

      I am glad it was useful for you πŸ™‚ What is your query?

  26. Ruthvik
    January 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?

    • January 9, 2015 at 5:13 pm

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

  27. anusha
    January 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 authenticator = new Authenticator();
    String result = authenticator.authenticate(username,password);

    please check and give me the reply asap.

    My IDE:Eclipse luna
    JDK7
    Tomcat 8

    • January 10, 2015 at 5:12 pm

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

    • akash
      June 2, 2015 at 10:18 pm

      Hi,

      I think you have imported wrong class file.
      Please import mvcdemo.model.Authenticator;

  28. Priya
    January 18, 2015 at 7:13 pm

    Superb …

  29. RAVI
    January 22, 2015 at 9:18 pm

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

  30. James
    February 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.

    • February 9, 2015 at 9:40 pm

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

  31. jugal
    February 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

    thank you

  32. kavitha.N
    February 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

    • February 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.

  33. samin
    March 3, 2015 at 12:10 am

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

  34. Chris
    March 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!

    • March 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? I’ll try to resolve them

  35. azzmi
    March 14, 2015 at 3:10 pm

    very good article..regards

  36. Shakour Ghafarzoy
    March 17, 2015 at 4:51 pm

    ….terrific MVC example thank u Sir πŸ™‚

  37. sachin
    March 20, 2015 at 9:48 pm

    owsm……………………….

  38. Ashish Mahadik
    March 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?

    • March 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 from database.

      • Ashish Mahadik
        March 25, 2015 at 2:49 pm

        Thanks Prasad ,
        it can clear all my concept about mvc pattern

        • March 26, 2015 at 10:56 am

          I’m glad you found it useful. Happy learning Ashish πŸ™‚

  39. kapil
    March 28, 2015 at 11:46 am

    sir, i created that architectur but how to run it..
    plz specify here….

    • March 28, 2015 at 8:44 pm

      Hi Kapil, I’ve already explained how to run it. What is the problem you are facing? Please let me know.

  40. ravi
    April 1, 2015 at 1:35 pm

    superb tutorial…. show me to database authentication of servlet mvc

  41. aishwarya
    April 4, 2015 at 12:07 pm

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

    • April 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

  42. veena
    April 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.

    • April 9, 2015 at 9:42 am

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

  43. Mourad
    April 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 can we respect MVC architecture ? I mean in that case we will have two beans, two classes for DAO access ? thanks for helping

  44. Ashish
    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.

  45. Juan
    April 25, 2015 at 10:29 pm

    Would it be possible to download the project ? It would help πŸ™‚

  46. Nahar
    May 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…

  47. Muthu
    May 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..

    • May 28, 2015 at 9:23 am

      The Form you are talking about is available in struts.

  48. Utsav
    June 9, 2015 at 12:51 pm

    Simply awesome example. Hats off to you Prasad ji… πŸ™‚

  49. Hakeem
    June 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.

    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 advice would be greatly appreciated. Thank you much in advance.

    Kind Regards,
    Hakeem

    • June 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.

      • Hakeem
        June 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 ?

  50. prakhar
    June 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.

    • June 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 πŸ™‚

  51. Saha
    July 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 also, I got severely confused becoz, they taught MVC, DAO without explaining well.
    Tell me about that also.

  52. July 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 link…like 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

  53. July 13, 2015 at 1:07 pm

    Hi Prasad,
    This was very helpful. Thanks a bunch!

  54. Kannan
    July 13, 2015 at 3:52 pm

    Hello Prasad,

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

    • July 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 the model layer as business layer here.

      • logeshkumar
        October 28, 2015 at 2:45 pm

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

        • October 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

  55. srikanth addani
    July 15, 2015 at 4:31 pm

    very good architecture..

  56. Suhas
    August 6, 2015 at 12:23 pm

    Thanks for good explanation

  57. Ajit
    August 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

  58. subodh
    August 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

  59. yanyan
    August 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.

  60. October 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!

  61. anupam pal
    November 2, 2015 at 1:54 pm

    Simply out and awesomely explained πŸ™‚

  62. Muzamil Marwat
    November 14, 2015 at 4:19 am

    Hi Prasad,
    i have more interest in programming ..
    i am a student of BSCS 1st semester reg from university of sargodha pakistan….
    can you help me…………..!

    • November 15, 2015 at 1:51 pm

      Yes Muzamil, please let me know what problem you are facing.

  63. Asif Ali Zardari
    December 2, 2015 at 5:49 pm

    isn’t mvc rule been broken here as you are using business logic (user authentication part) inside the controller.

    • December 4, 2015 at 10:06 am

      Hi Asif, thank you for your comment. If you watch closely, there is a separate class called Authenticator which does the job of authentication. It is simply being called from the controller i.e. Servlet.

  64. EduardoC
    December 12, 2015 at 2:19 am

    Excelent example!!!!

  65. harshali
    December 14, 2015 at 12:26 pm

    Description Resource Path Location Type
    Access restriction: The type ICUBinary.Authenticate is not accessible due to restriction on required library C:\Program Files (x86)\Java\jre6\lib\rt.jar Logincontroller.java /MVCDemo/src/mvcdemo/controllers line 14 Java Problem
    HttpServlet cannot be resolved to a type Logincontroller.java /MVCDemo/src/mvcdemo/controllers line 16 Java Problem
    HttpServletRequest cannot be resolved to a type Logincontroller.java /MVCDemo/src/mvcdemo/controllers line 23 Java Problem
    HttpServletResponse cannot be resolved to a type Logincontroller.java /MVCDemo/src/mvcdemo/controllers line 24 Java Problem
    RequestDispatcher cannot be resolved to a type Logincontroller.java /MVCDemo/src/mvcdemo/controllers line 28 Java Problem
    ServletException cannot be resolved to a type Logincontroller.java /MVCDemo/src/mvcdemo/controllers line 24 Java Problem
    The import javax.servlet.http cannot be resolved Logincontroller.java /MVCDemo/src/mvcdemo/controllers line 7 Java Problem
    The import javax.servlet.http cannot be resolved Logincontroller.java /MVCDemo/src/mvcdemo/controllers line 8 Java Problem
    The import javax.servlet.http cannot be resolved Logincontroller.java /MVCDemo/src/mvcdemo/controllers line 9 Java Problem
    The import javax.servlet.RequestDispatcher cannot be resolved Logincontroller.java /MVCDemo/src/mvcdemo/controllers line 5 Java Problem
    The import javax.servlet.ServletException cannot be resolved Logincontroller.java /MVCDemo/src/mvcdemo/controllers line 6 Java Problem
    The public type LoginController must be defined in its own file Logincontroller.java /MVCDemo/src/mvcdemo/controllers line 16 Java Problem

    i have found this error in logincontroller plzz tell me how to solve this problem

  66. Raj gopal
    January 19, 2016 at 10:47 pm

    Good example but
    JSTL jar is not required for this example

  67. sachin
    February 15, 2016 at 6:14 pm

    Nice example
    give me some more example
    with hibernet nn struts

  68. sachin
    February 15, 2016 at 6:16 pm

    i have some problem in eclipse
    can you help me
    Several ports (2024, 4040, 8007) required by Tomcat v7.0 Server at localhost are already in use. The server may already be running in another process, or a system process may be using the port. To start this server you will need to stop the other process.
    how to solve this problem

    • February 16, 2016 at 12:14 pm

      Hi Sachin, have you tried killing all the processes which are using these ports? Your tomcat is already running and you are again trying to start it that is why problem occurs.

  69. mahima
    March 12, 2016 at 10:01 pm

    Thnku sir for this exp
    Give me exp of struts nd jsf

  70. Liam O Gorman
    March 22, 2016 at 5:32 pm

    Thank you Prasad

  71. Liam O Gorman
    March 22, 2016 at 5:34 pm

    Very good explanation sir

  72. March 22, 2016 at 6:09 pm

    Hello Prasad,

    Wowe..

    Much Explanation.

    Such information good.

    Tnx

  73. Usha
    April 2, 2016 at 12:53 pm

    Hi Prasad,

    I have just started learning MVC Architecture. You explanation was the simplest yet most beneficial to me. I did not understand half the things from my lectures and was getting so frustrated. I went thru so many sites on MVC your explanation was by far the best.

    However when i try to run login.jsp and enter the details I get the following error.
    type Status report

    message /MVCDemo/LoginController

    description The requested resource is not available.

    Thank you and bless you
    Usha

    • April 2, 2016 at 3:34 pm

      Thank you Usha for kind words. Could you please let me know whether you are getting any exception in logs?

      • usha
        April 5, 2016 at 12:07 pm

        Hi Prasad, it works now thanks for your help

  74. project
    April 3, 2016 at 1:02 pm

    hi prasad, i like ur tutorial. i want ur help on struts framework with database (to manage emusic library.)

    • April 3, 2016 at 10:19 pm

      Thank you for your words. Please let me know any problem you face. I will surely try to help.

      • project
        April 11, 2016 at 1:02 pm

        i want to know how can i manage my emusic library using struts framework.

  75. Deepak
    April 10, 2016 at 9:35 pm

    Hi Sir,
    I am getting the below error whenever i try to run the JSP page and enter the details. Can you please let me know what could be the root cause for this?


    HTTP Status 404 – /MVCDemo/LoginController

    ——————————————————————————–

    type Status report

    message /MVCDemo/LoginController

    description The requested resource is not available.

    ——————————————————————————–

    Apache Tomcat/7.0.56

  76. umesh
    April 16, 2016 at 4:34 pm

    thanks for providing example sir

  77. Dan
    April 23, 2016 at 1:58 am

    Very instructive and informative Tutorial. Another instructive example would be to show MVC pattern with regards to a NEW user registration.

    The reason this example would be beneficial is because it gives an opportunity to demonstrate the post/redirect/get pattern.

  78. Faizan
    July 26, 2016 at 10:59 am

    Please share the code of INSERT, UPDATE , and DELETE record as well, Thanks.

  79. Sadbhavana bhardwaj
    July 26, 2016 at 6:00 pm

    Thnkew sir…bt can u plz tell me k user.java kb call hua???

    • Sadbhavana bhardwaj
      July 26, 2016 at 6:51 pm

      Thnkew…..sir…pta chl gya….

  80. Mansi Verma
    August 1, 2016 at 8:15 pm

    Thank you Mr. Prasad for this helpful tutorial. I m a beginner in java and your explanation helped me a lot. Now things are quite clear than before. Please make some more tutorials on other j2EE/struts/hibernate topics.Thank you.

    • October 5, 2016 at 5:56 pm

      hello mansi struts is out of market so don’t learn struts it is time waiste if you learn then go for spring framework is the best framework at that time,, in spring more jobs……….
      ye alag bat hai mujhe job nahi mili………………

  81. August 5, 2016 at 8:52 pm

    Programming is combination of intelligent and creative work. Programmers can do anything with code. The entire Programming tutorials that you mention here on this blog are awesome. also provides latest tutorials of Programming from beginning to advance level.
    Be with us to learn programming in new and creative way.

  82. pavan
    August 29, 2016 at 11:48 am

    how to create a jar file in lib??

  83. snehal
    October 5, 2016 at 9:04 am

    Can anybody will help me with library management system for book issuing function using MVC programming in JAVA and its database connectivity?

    • October 5, 2016 at 9:14 am

      hi Snehal, Please show me what you have tried. I will be happy to help.

  84. October 5, 2016 at 5:49 pm

    Hello Sir
    My name is shilendra Tiwari i learnt servlet, jsp, jdbc, Spring, Hibernate, and also make 6 projects on servlet jsp but sir i have no job that time becoz sir at that time no vaicency came, Sir my english very veak,sir i want to know which place is more jobs in Spring Hibernate but english is not more mindatory i am very confused…………..

  85. sathishkumar
    January 18, 2017 at 7:14 pm

    nice explanation….now my thoughts so clear

  86. Nikhil Padwalkar
    April 1, 2017 at 11:20 am

    Nice description of servlet and jsp with mvc

  87. Anusha
    April 3, 2017 at 7:25 pm

    Thank you Mr. Prasad Kharkar. Your post has helped me in understanding MVC better! Keep posting.

  88. Mahantesh S B
    April 12, 2017 at 7:34 pm

    sir i am getting 500 and 404 error to moving for next page

  89. Bhuvana
    April 14, 2017 at 10:35 pm

    I created a registration page where i need to insert data to database using servlets and in my registration page i have country and state where i need to fetch this data from database when ever i select a particular country only particular country states should display ..how do i pass to my jsp page using servlets

  90. Sunil
    July 15, 2017 at 12:56 pm

    Hi Sir,

    I’m getting an error in LoginController.java at “protected void (HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException{” as below

    Syntax error on token “void”, Identifier expected after this token

  91. Arun Singh
    August 11, 2017 at 7:25 pm

    where is the Model dude?

    • August 14, 2017 at 8:52 am

      User class is the model. Its there in tutorial πŸ™‚

  92. Steve
    September 8, 2017 at 7:08 pm

    Thanks for sharing Prasad!

  93. Hamid
    November 27, 2017 at 2:00 pm

    Hi Prasad give me code for jsp & Servlet mvc insert, update,delete operations with database.

    Fastly reply me. Thanks….

  94. Hamid
    November 30, 2017 at 2:20 pm

    Please give me code jsp & servlet mvc insert,update & delete code.
    Please fast reply me because my project regarding. Thanks….

    • December 19, 2017 at 9:49 am

      Hi Hamid, are you facing certain problems in your project? I will be happy to solve any of your problems if you post here.

  95. Hamid
    December 2, 2017 at 9:56 am

    Please give me code for JSP & SERVLET MVC insert, update, & delete code
    One help me please

    • December 19, 2017 at 9:45 am

      hi, could you please post the problem you are facing? Are you facing some error in your application ?

  96. December 18, 2017 at 9:02 am

    Osmmm

  97. Vikas
    January 25, 2018 at 5:29 pm

    It is very helpful for understanding MVC architecture with Jsp and Servlet.

  98. Hamid
    February 12, 2018 at 8:41 pm

    Hey Prasad what the purpose use of
    User user=new User() class.
    Why is need.

  99. Hamid
    February 12, 2018 at 8:44 pm

    Why use
    User user = new User(username, password);
    What is role this above code.

  100. prashant
    March 14, 2018 at 11:59 am

    hi prasad,
    does this code work with tomcat 8???

  101. vyga
    March 23, 2018 at 4:43 pm

    Hi ,
    can you please tell me what is model here in this example. i understood controller and view.
    Thanks

  102. bipin jaiswal
    April 19, 2018 at 5:29 pm

    Hi,
    it was great example but can you please explain why we use here POJO class or can you explain why we use pojo class in mvc based web application because,here I am not able to see any usage of it.

    • April 21, 2018 at 10:14 am

      Hi, the POJO is being used a model in mvc architecture

Leave a Reply

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