0% found this document useful (0 votes)
14 views25 pages

JWT Unit 5

Download as pdf or txt
Download as pdf or txt
Download as pdf or txt
You are on page 1/ 25

Unit V

Model-View-Controller (MVC), JSP 2.0 Expression and


Accessing Database with JDBC
MVC Architecture for Web Applications
MVC architecture has become an extremely common and favoured design pattern for creating web applications. Let us explore more
and learn why is it so famous and preferred in the world of web design.

MVC stands for:-

✓ Model(M) : Handles the data(database).

✓ View(V) : Handles what the user sees(html, css, js).

✓ Controller(C) : Acts as a mediator between view and model.

Most modern web frameworks in JS follow the MVC design pattern encouraging developers to write clean, structured code. It allows
the developers to benefit all the benefits of modularity along with integrating a structure that accommodates multiple developers
working on the same project.
Model

● The Model component deals with the data of the application.


● This component is responsible for managing the data, logic, and rules of the application.

View

● Handles the User Interface(UI). This component typically consists of the html, css, and static js files.
● The view component is used to attach and trigger user events but the event handling comes under the domain of the controller .

Controller

● The primary responsibility of a controller is to handle the events triggered by user input and also acts as a mediator between the view
and model.
● In other words, the controller contains client-side specific logic.
● The controller provides various functions based on events that can be triggered and then contacts the model for reading/updating
data and presents the new information to view component to be shown to the user.
Note: A router handles the HTTP requests or events towards the function in the controller that handles
them. Now let’s understand their working properly.

▹ The user interacts with the browser. [ Triggers event ]

▹The browser then sends a HTTP request to the web-server.

▹The router gets a HTTP request from browser and directs it towards the controller .

▹The controller than interacts with model to modify/read data from the database.

▹The new information is then passed onto the view which changes its state accordingly and sends it back to the
browser for the user to see.
Directory Structure
A common way to implement this architecture is to have the files for respective components in different folders. A
very basic directory structure for a project created as per MVC architecture might look something like this →

project-folder

├───controller
│ controller.js

├───model
│ db.js

└───view
│ home.html

└───index.js(entry-point to project)

Note: Obviously the file-names could be anything and the folder structure might differ a little too. In most cases,
there are more folders too containing static files, resource files, cache, routing, etc.
Conclusion
The MVC(Model-View-Controller) is a three-component architectural design each having its own role.

✦ Model handles data

✦ View handles User Interface(UI)

✦ Controller mediates between model and view.

MVC has following benefits for the user →

● Modularizes the application


● Scalable coding design
● Separates business logic and view logic
● Allows simultaneous work between developers who are responsible for different components (such as UI layer and core
logic).
RequestDispatcher in Servlet
The RequestDispatcher interface provides the facility of dispatching the request to another resource it may be html,
servlet or jsp. This interface can also be used to include the content of another resource also. It is one of the way of
servlet collaboration.

There are two methods defined in the RequestDispatcher interface.

Methods of RequestDispatcher interface

The RequestDispatcher interface provides two methods. They are:

1. public void forward(ServletRequest request,ServletResponse response)throws


ServletException,java.io.IOException:Forwards a request from a servlet to another resource (servlet, JSP file, or
HTML file) on the server.
2. public void include(ServletRequest request,ServletResponse response)throws
ServletException,java.io.IOException:Includes the content of a resource (servlet, JSP page, or HTML file) in the
response.
How to get the object of RequestDispatcher
The getRequestDispatcher() method of ServletRequest interface returns the object of
RequestDispatcher. Syntax:

Syntax of getRequestDispatcher method


public RequestDispatcher getRequestDispatcher(String resource);

Example of using getRequestDispatcher method


RequestDispatcher rd=request.getRequestDispatcher("servlet2");

//servlet2 is the url-pattern of the second servlet

rd.forward(request, response);//method may be include or forward


Expression Language (EL) in JSP

The Expression Language (EL) simplifies the accessibility of data stored in the Java Bean
component, and other objects like request, session, application etc.

There are many implicit objects, operators and reserve words in EL.

It is the newly added feature in JSP technology version 2.0.

Syntax for Expression Language (EL)

${ expression }
EL param example
In this example, we have created two files index.jsp and process.jsp. The index.jsp file
gets input from the user and sends the request to the process.jsp which in turn prints the
name of the user using EL.

index.jsp
<form action="process.jsp">

Enter Name:<input type="text" name="name" /><br/><br/>

<input type="submit" value="go"/>

</form>

process.jsp
Welcome, ${ param.name }
EL sessionScope example
In this example, we printing the data stored in the session scope using EL. For this purpose, we have used
sessionScope object.

index.jsp
<h3>welcome to index page</h3>

<%

session.setAttribute("user","sonoo");

%>

<a href="process.jsp">visit</a>

process.jsp
Value is ${ sessionScope.user }
Using EL Operators and Accessing Database with JDBC
<%@ page import = "java.io.*,java.util.*,java.sql.*" %>
<%@ page import = "javax.servlet.http.*,javax.servlet.*" %>
<%@ taglib uri ="http://java.sun.com/jsp/jstl/core" prefix = "c"%>
<%@ taglib uri ="http://java.sun.com/jsp/jstl/sql" prefix = "sql"%>

<html>
<head>
<title>SELECT Operation </title>
</head>

<body>
<sql:setDataSource var = "snapshot" driver = "com.mysql.jdbc.Driver"
url = "jdbc:mysql://localhost/TEST"
user = "root" password = "pass123" />

<sql:query dataSource = "${snapshot}" var = "result" >


SELECT * from Employees;
</sql:query>

<table border = "1" width = "100%">


<tr>
<th>Emp ID</th>
<th>First Name </th>
<th>Last Name </th>
<th>Age</th>
</tr>
<c:forEach var = "row" items = "${result.rows}">
<tr>
<td><c:out value = "${row.id}"/></td>
<td><c:out value = "${row.first}"/></td>
<td><c:out value = "${row.last}"/></td>
<td><c:out value = "${row.age}"/></td>
</tr>
</c:forEach>
</table>

</body>
</html>

You might also like