Frage

I am messing around in Java2EE Servlets and JSP and I am still confused when the generated HTML code is sent to the web browser. I implemented the FrontController pattern, so the user requests are intercepted by Controller servlet (FC), which based on the command packed in request finds out, what jsp page is being requested and forwards request and response there using request.getRequestDispatcher(path).forward(request, response); Then I guess that the jsp page with few lines of dynamically set data retreived from request passed by forward method is generated and sent back to browser?

I guess that this is not the correct way how it works, it is just how I see it, so could somebody correct me possible write the whole procedure and explain the exact purpose of getRequestDispatcher(path).forward(request, response);?

Thanks in advance!

War es hilfreich?

Lösung

Forwarding consists in saying: I've done my part of the job, let the component living at path do the rest.

Servlets (controllers) are good at reading and validating parameters, invoking business logic and creating Java Bean instances (the model). They suck at generating HTML code.

JSPs (views) are great to dynamically generate HTML code using data stored in Java beans, but they're not good at executing business logic.

So the request is first handled by a servlet, which reads and validates the parameters, invokes business logic, gets data and stores it in Java Beans stored as request attributes. Then the servlet forwards the request and response to a JSP, which generates the HTML and writes it to the response writer. And what is written to the response writer goes to the browser.

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top