Dynamic content: programming the web servers

29
Dynamic content: programming the web servers Introduzione alla programmazione web – Marco Ronchetti 2020 – Università di Trento 1

Transcript of Dynamic content: programming the web servers

Page 1: Dynamic content: programming the web servers

Dynamic content:programming the

web servers

Introduzione alla programmazione web – Marco Ronchetti 2020 – Università di Trento1

Page 2: Dynamic content: programming the web servers

Step 2:let’s use our web server

to generate dynamic pages

Introduzione alla programmazione web – Marco Ronchetti 2020 – Università di Trento2

Page 3: Dynamic content: programming the web servers

QHow can we obtain dynamic responses from a Web server?

Introduzione alla programmazione web – Marco Ronchetti 2020 – Università di Trento3

Page 4: Dynamic content: programming the web servers

Dynamic pages: the basic idea:1) We add to the URL the parameters which

express our query 2) We write an executable, which somehow

accesses the (GET/POST) parameters and generates an HTML page

3) We associate the URL with an executable

e.g.: CGI, what’s the time?

Introduzione alla programmazione web – Marco Ronchetti 2020 – Università di Trento4

Page 5: Dynamic content: programming the web servers

Creating a dynamic page

Introduzione alla programmazione web – Marco Ronchetti 2020 – Università di Trento5

MR-MBP-14955:local ronchet$ cd /Applications/XAMPP/xamppfiles/cgi-binMR-MBP-14955:htdocs ronchet$ vi getTime.shMR-MBP-14955:htdocs ronchet$ cat getTime.sh#!/bin/shecho "Content-type: text/plain; charset=iso-8859-1"echoecho "<HTML><BODY><H1>"echo `date`echo "</H1></BODY></HTML>"

Edit fileShow content

embed HTML into the code

Page 6: Dynamic content: programming the web servers

http

d

The original web architecture: dynamic pages

Inter

net

HTTP Get + params

Cgi-bin

proc

ess

Client

Browser Server

File System

Evolution 1: dynamically create (interlinked) documents

Page 7: Dynamic content: programming the web servers

Another solution: a simpler ideaInstead of embedding HTML into the code, we could embed the code into HTML…

Introduzione alla programmazione web – Marco Ronchetti 2020 – Università di Trento7

Template page

<HTML>SOME HTML STUFF%SOME CODE%MORE HTML STUFF</HTML>

<HTML>SOME HTML STUFFCODE OUTPUTMORE HTML STUFF</HTML>

Page 8: Dynamic content: programming the web servers

A simpler ideaAugment the web server with an engine capable of parsing a web page, and executing code in it

Introduzione alla programmazione web – Marco Ronchetti 2020 – Università di Trento8

Retrieve template pagefrom file system

<HTML>SOME HTML STUFF%SOME CODE%MORE HTML STUFF</HTML>

<HTML>SOME HTML STUFFCODE OUTPUTMORE HTML STUFF</HTML>

Request URL

Response

Evaluatecode

Page 9: Dynamic content: programming the web servers

example: PHP

whatsTheTime.php

Introduzione alla programmazione web – Marco Ronchetti 2020 – Università di Trento9

Page 10: Dynamic content: programming the web servers

http

d

The original web architecture: dynamic pages

Inter

net

HTTP Get + params

PHP

Inte

rpre

ter

Client

Browser Server

File System

Evolution 1: dynamically create (interlinked) documents

Page 11: Dynamic content: programming the web servers

A third, more efficient solutionInstead of spawning a process, extent the Web Server to allow for threads, and create a callback-based framework

Give developers API to implement the callbacks

Introduzione alla programmazione web – Marco Ronchetti 2020 – Università di Trento11

Page 12: Dynamic content: programming the web servers

http

d

The original web architecture: dynamic pages

Inter

net

HTTP Get + params

Threads

Client

Browser Server

File System

Evolution 1: dynamically create (interlinked) documents

JVM

Page 13: Dynamic content: programming the web servers

QHow can we write a Servlet ?

Introduzione alla programmazione web – Marco Ronchetti 2020 – Università di Trento13

Page 14: Dynamic content: programming the web servers

Servlets1. Create an HttpServlet subclass2. Associate an URL to your class3. implement doGet (or doPost, doHead…) 4. Use the HttpServletRequest parameter to read the

HTTP request and extract info5. Use the HttpServletResponse parameter to write the

HTTP response6. Deploy the Servlet in a servlet container (a suitable

web server)

Introduzione alla programmazione web – Marco Ronchetti 2020 – Università di Trento14

Page 15: Dynamic content: programming the web servers

Example:let’s write a Servlet that does not use

parameters

Introduzione alla programmazione web – Marco Ronchetti 2020 – Università di Trento15

Page 16: Dynamic content: programming the web servers

package it.unitn.disi.ronchet.myservlets;

import java.io.IOException;

import java.io.PrintWriter;

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(name = "ReadPost", urlPatterns = {"/ReadPost"})

public class ReadPost extends HttpServlet {

@Override

protected void doGet(HttpServletRequest request,

HttpServletResponse response)

throws ServletException, IOException {

processRequest(request, response);

}

@Overrideprotected void doPost(HttpServletRequest request,

HttpServletResponse response)

throws ServletException, IOException {

processRequest(request, response);

}

Introduzione alla programmazione web – Marco Ronchetti 2020 – Università di Trento16

Page 17: Dynamic content: programming the web servers

/**

* Processes requests for both HTTP <code>GET</code> and <code>POST</code>

* methods.

*

* @param request servlet request

* @param response servlet response

* @throws ServletException if a servlet-specific error occurs

* @throws IOException if an I/O error occurs

*/

protected void processRequest(HttpServletRequest request,

HttpServletResponse response)

throws ServletException, IOException {

response.setContentType("text/html;charset=UTF-8");

try (PrintWriter out = response.getWriter()) {

/* TODO output your page here. You may use following sample code. */

out.println("<!DOCTYPE html>");

out.println("<html><head>");

out.println("<title>Servlet ReadPost</title>"); out.println("</head><body>");

out.println("<h1>Servlet ReadPost at "+request.getContextPath()+"</h1>");

out.println("</body>");

out.println("</html>");

}

}} Introduzione alla programmazione web – Marco Ronchetti 2020 – Università di Trento17

Page 18: Dynamic content: programming the web servers

What can usehttpServletResponse for?

Introduzione alla programmazione web – Marco Ronchetti 2020 – Università di Trento18

httpServletResponse

servletResponse

See https://docs.oracle.com/javaee/7/api/javax/servlet/ServletRequest.html

• getWriter• setCharacterEncoding• setContentLength• setContentType• setLocale

• addHeader• sendError• sendRedirect• setStatus• Session• addCookie

Page 19: Dynamic content: programming the web servers

Output

Introduzione alla programmazione web – Marco Ronchetti 2020 – Università di Trento19

Page 20: Dynamic content: programming the web servers

Next step:let’s get the parameters contained in the request

Introduzione alla programmazione web – Marco Ronchetti 2020 – Università di Trento20

Page 21: Dynamic content: programming the web servers

Parameters passing

Introduzione alla programmazione web – Marco Ronchetti 2020 – Università di Trento21

<html><body><form action="http://localhost:8084/WebApplication01/ReadPost" method="post">

<label for="fname">First name:</label><input type="text" name="fname"><br><br><input type="submit" value="Submit"><input type="reset" value="Reset">

</form></body></html>

Page 22: Dynamic content: programming the web servers

protected void processRequest(HttpServletRequest request,

HttpServletResponse response)

throws ServletException, IOException {

String name = request.getParameter("fname");

response.setContentType("text/html;charset=UTF-8");

try (PrintWriter out = response.getWriter()) {

/* TODO output your page here. You may use following sample code. */

out.println("<!DOCTYPE html>");

out.println("<html><head>");

out.println("<title>Servlet ReadPost</title>");

out.println("</head><body>");

out.println("<h1> fname="+name+"</h1>");

out.println("</body>");

out.println("</html>");

}

}}

Introduzione alla programmazione web – Marco Ronchetti 2020 – Università di Trento22

Page 23: Dynamic content: programming the web servers

Introduzione alla programmazione web – Marco Ronchetti 2020 – Università di Trento23

Post

Page 24: Dynamic content: programming the web servers

Introduzione alla programmazione web – Marco Ronchetti 2020 – Università di Trento24

Post

Get

Page 25: Dynamic content: programming the web servers

Getting parameters from httpServletRequest:

Introduzione alla programmazione web – Marco Ronchetti 2020 – Università di Trento25

httpServletRequest

servletRequest

See https://docs.oracle.com/javaee/7/api/javax/servlet/ServletRequest.html

Page 26: Dynamic content: programming the web servers

What else can I obtain fromhttpServletRequest?

Introduzione alla programmazione web – Marco Ronchetti 2020 – Università di Trento26

httpServletRequest

servletRequest

See https://docs.oracle.com/javaee/7/api/javax/servlet/ServletRequest.html

• Character encoding• Content length• Content Type• Locale• Protocol• Local/remote name, address, port• Server name• Is Secure ?

• Headers• Method• Path Info• Query String• Session• Cookie

Page 27: Dynamic content: programming the web servers

QHow can we deploy a Servlet ?

Introduzione alla programmazione web – Marco Ronchetti 2020 – Università di Trento27

Page 28: Dynamic content: programming the web servers

Apache Tomcat

Introduzione alla programmazione web – Marco Ronchetti 2020 – Università di Trento28

https://tomcat.apache.org/download-80.cgi

Page 29: Dynamic content: programming the web servers

Servlet Deployment§ By default, a servlet application is located at the path <Tomcat-installationdirectory>/webapps/ROOT and the class file would reside in <Tomcat-installationdirectory>/webapps/ROOT/WEB-INF/classes.

Introduzione alla programmazione web – Marco Ronchetti 2020 – Università di Trento29

If you have a fully qualified class name of com.myorg.MyServlet, then this servlet class must be located in WEB-INF/classes/com/myorg/MyServlet.class.