1

I'm new to Java Web Development and I have a lot of confusion in my head. I'm trying to develop a small project in Java, but i don't know how the components of the MVC pattern should be connected.

I have a DAO that talk with the DB and is able to make normal CRUD operations pretty well. The DAO works with the package Model, that reflects my tables inside the DB. O the other side I have my JSP pages, inside WEB-INF folder.

I read this info tag about the servlets https://stackoverflow.com/tags/servlets/info and it doesn't works for me. As explained here: How to develop JSP/Servlets Web App using MVC pattern?

The MVC pattern is composed with

  1. Model (java classes / beans): that reflect the DB data (with properties, getter/setter methods).
  2. View (JSP pages) here, my HTML, the presentation level.
  3. Controller (The servlets) that map urls to JSP pages, right?

So the model works, the view work, the controller what should do? actually i get this error:

HTTP Status 404 - /crm/hello.jsp type. Status report message. /crm/hello.jsp description. The requested resource is not available.

But the resource inside WEB-INF exists, the file hello.jsp, is there. Here the snippet of code:

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@WebServlet("/hello")
public class HelloServlet extends HttpServlet {

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) 
            throws ServletException, IOException {
        // Preprocess request: we actually don't need to do any business stuff, so just display JSP.
        request.getRequestDispatcher("hello.jsp").forward(request, response);
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) 
            throws ServletException, IOException {
        // Postprocess request: gather and validate submitted data and display result in same JSP.
        request.getRequestDispatcher("hello.jsp").forward(request, response);
    }
}

I'm using the @WebServlet notation, that should replace web.xml, right? I have also read the answer: Design Patterns web based applications and as explained there:

Request (action) based MVC: this is the simplest to implement. The (Business) Model works directly with HttpServletRequest and HttpServletResponse objects. You have to gather, convert and validate the request parameters (mostly) yourself. The View can be represented by plain vanilla HTML/CSS/JS and it does not maintain state across requests. This is how among others Spring MVC, Struts and Stripes works.

I have to map each url to a JSP page and Servlet with a Request/Response inside or there's should be one Servlet that handle all request/response? Where should i trace the session? Where should i set the cookies? Where should i do the business flow? For example insert the data from a web form to a db, or display the DB data to a JSP page?

I'm trying to connecting the dots, but someone missing. The problem is how the Model and View should talks with the controller? May you provide some example?

realnot
  • 721
  • 1
  • 11
  • 31
  • 1
    Forward to `/WEB-INF/hello.jsp` instead. – Kayaman Mar 06 '15 at 11:20
  • Thanks for the answer, i tried to put the page in /WEB-INF/ and it works. Before was setted in the same way, but the page was under Web Pages in NetBeans and not in /WEB-INF/. By the way, now it works, but all other questions are still unsolved. – realnot Mar 06 '15 at 12:18
  • Your other questions are quite broad. You should search for existing answers. – Kayaman Mar 06 '15 at 12:24
  • i'll try to search again and again, thanks for your time. – realnot Mar 06 '15 at 12:26
  • 1
    Controller works as a communicator between model and view. It updates the model based on view and it also updates the view based on controller. – Brijesh Bhatt Mar 06 '15 at 18:11

0 Answers0