Dynamic Web Site
Junghoo Cho
Dynamic User Interaction
- Many sites generate contents dynamically based on “user input”
- e.g., search by keywords, facebook status update, …
- Q: How can a server obtain user input?
HTML Form
HTML Form Interaction
HTML Form: <form>
<form action="url" method="GET">
HTML Form: <input>
<input type="type" name="name">
Input Encoding: GET
vs POST
- User-provided input is sent as query “
name=value
” pairs of request
- GET: query is added to request path
- POST: query is added to request body
Content-Type: application/x-www-form-urlencoded
- Example
Input Types
- Common input types
hidden
type
- HTML5 adds many more input types
date
, time
, email
, color
, number
, …
Uploading a File: file
Type
MIME: multipart/form-data
- Way to include multiple “objects” in a single message
boundary
attribute of Content-Type
header
- See HTTP request
Coding Dynamic Web Server
- Simple example: Hello, John!
- Q: How does it work? What happens at the server?
- Q: How should we write code to generate dynamic content at the server?
- Two approaches
- Programmatic vs Template
- “Write a program!” vs “Write a Web page!”
Programmatic Approach
- Write a program that prints out the HTML page!
- Example: Java Servlet for “Hello, John!”
protected void doGet(HttpServletRequest request,
HttpServletResponse response)
throws ServletException, IOException
{
PrintWriter out = response.getWriter();
out.println("<html>");
out.println("<head><title>Hello</title></head>");
out.println("<body>Hello, " + request.getParameter("fname") + "!");
out.println("</body>");
out.println("</html>");
out.close();
}
Template Approach
Separating Code from Page
- Even with the template approach, the page gets messy quickly once complex application logic is added
- Q: Can we separate “code” from “page”?
- Code “ownership”
- Often, page design is done by designers, while app coding is done by developers.
- Who “owns” the above pages?
- When multiple people “own” the same page, “conflicts” arise
Model-View-Controller (MVC) Pattern
- Most programs (including web sites) have to deal with
- data
- application logic, and
- final result presentation
MVC Pattern: Data
- Data may be stored
- in a file or database engine, and
- locally or remotely
- Where and how data is stored and managed change over time
- Let us create an abstract “data layer” and make it separate from others
- Detailed storage mechanism is hidden from other layers
- Changes in data layer do not affect other layers
MVC Pattern: Presentation
- The same “data” may be presented in many different ways
- Depending on user, device, location, …
- Let us make “presentation layer” separate from others
- Presentation changes do not affect other layers
MVC Pattern
- Split the code into three modular components!
- Model: deals with data storage and access
- View: deals with result presentation
- Controller: deals with “application logic”
- Each component may be “owned” by different people
- e.g., model: DB engineer, controller: app developer, view: UI designer
MVC Example: Controller
protected void doGet(HttpServletRequest request,
HttpServletResponse response)
throws ServletException, IOException
{
user = getUser('id_34');
request.setAttribute("user_name", user.name);
request.getRequestDispatcher("/index.jsp").forward(request, response);
}
MVC Example: Model
User getUser(String userid)
{
}
MVC Example: View
/* VIEW */
<html>
<head><title>Demo</title></head>
<body>Hello, <%= request.getAttribute("user_name") %></body>
</html>
Special Tag Libraries
Four Layers of Web site
- Encryption layer: encrypt transport
- HTTP layer: interpret request and serve response
- Application layer: generate dynamic content
- Storage/Data layer: store and retrieve data
What We Learned
- HTML
form
and input
elements
- Dynamic site: programmatic vs template approach
- MVC (Model-View-Controller) pattern
- Web server architecture