Session - 8 - App Dev
Session - 8 - App Dev
Session 8
Academic year 2023-2024
Application Application
Program Program
Propietary Network or
dial up phone lines Local Area Network Internet
(a) Mainframe Era (b) Personal Computer Era (c) Web era
• Web browsers avoid the need for downloading/installing specialized code, while
providing a good graphical user interface
HTML example
<html>
<body>
<table border>
<tr> <th>ID</th> <th>Name</th> <th>Department</th> </tr>
<tr> <td>00128</td> <td>Zhang</td> <td>Comp. Sci.</td> </tr> ID Name Department
….
00128 Zhang Comp. Sci.
</table> 12345 Shankar Comp. Sci.
<form action="PersonQuery" method=get> 19991 Brandt History
Search for:
<select name="persontype">
<option value="student" selected>Student </option>
<option value="instructor"> Instructor </option> Search for: Student
</select> <br>
Name: <input type=text size=20 name="name"> Name:
<input type=submit value="submit"> submit
</form>
</body> </html>
Three-Layer Web Architecture
HTTP and sessions
• HTTP is connectionless
• The server closes the connection with the client once the server replies to a
request (releases all the resources reserved)
• Goal: reduce load on server
• But user authentication should be done only once per session
• Solution: use a cookie
• A cookie is a small piece of text containing identifying information
• Sent by server to the browser on the first interaction, to identify the session
• Sent by browser to the server that created the cookie on further interactions
• Part of the HTTP protocol
• Cookies can be stored permanently or for a limited time
Implementation of the client-server
• Usually on the client side: Javascript
• Javascript functions can
• Check input for validity
• Modify the displayed Web page, by altering the underlying Document Object
Model (DOM) tree representation of the displayed HTML text
• Communicate with a Web server to fetch data and modify the current page
using fetched data, without needing to reload/refresh the page
• On the server side greater variety: Java servlet, PHP, Python, RoR, ASP.NET…
• Server-side scripting simplifies the task of connecting a database to the Web
• Define an HTML document with embedded executable code/SQL queries.
• Input values from HTML forms can be used directly in the embedded code/SQL
queries.
• When the document is requested, the Web server executes the embedded
code/SQL queries to generate the actual HTML document
HTTP and sessions
• Servlet API supports handling of sessions by managing cookies
• To check if session is already active:
• if (request.getSession(false) == true) Session exists
HttpSession = request.getSession(false);
String userid = (String) session.getAttribute(“userid”)
• else .. redirect to authentication page
• check login/password
• Create new session
• HttpSession session = request.getSession(true)
• session.setAttribute(“userid”, userid)
• Servlets run inside application servers such as Apache Tomcat, IBM WebSphere and
Oracle Application Servers
Example of Javascript
Javascript used to validate form input
<html> <head>
<script type="text/javascript">
function validate() {
var credits=document.getElementById("credits").value;
if (isNaN(credits)|| credits<=0 || credits>=16) {
alert("Credits must be a number greater than 0 and less than 16");
return false
}
}
</script>
</head> <body>
<form action="createCourse" onsubmit="return validate()">
Title: <input type="text" id="title" size="20"><br />
Credits: <input type="text" id="credits" size="2"><br />
<Input type="submit" value="Submit">
</form>
</body> </html>
Example of servlet code
import java.io.*; String persontype = request.getParameter("persontype");
String number = request.getParameter("name");
import javax.servlet.*;
if(persontype.equals("student")) {
import javax.servlet.http.*;
... code to find students with the specified name ...
public class PersonQueryServlet extends HttpServlet { ... using JDBC to communicate with the database ..
public void doGet (HttpServletRequest request, HttpServletResponse out.println("<table BORDER COLS=3>");
response)
out.println(" <tr> <td>ID</td> <td>Name: </td>" + " <td>Department</td> </tr>");
throws ServletException, IOException for(... each result ...){
{ ... retrieve ID, name and dept name
response.setContentType("text/html"); ... into variables ID, name and deptname
PrintWriter out = response.getWriter(); out.println("<tr> <td>" + ID + "</td>" + "<td>" + name + "</td>" + "<td>" + deptname
+ "</td></tr>");
out.println("<HEAD><TITLE> Query Result</TITLE></HEAD>");
};
out.println("<BODY>");
out.println("</table>");
….. BODY OF SERVLET (on the right) … }
out.println("</BODY>"); else {
out.close(); ... as above, but for instructors ...
} }
}
Application architecture
Application architecture
• Model-View-Controller (MVC) architecture 1
Internet Controller
• Model: business logic 8
6
5
2
7
• View: presentation of data on display device Web browser
View Model
• Controller: receives events, executes actions, and 3
returns a view to the user Data Access
Layer
• Business-logic layer 4