Professional Documents
Culture Documents
Web Technologies
Dynamic content generation
• Motivations
– Interactive applications require pages that contain
data that depends on the user’s need
• Data coming from databases
• Response to specific requests (e.g., queries)
– Client-side scripting does not achieve the required
results in dynamic data gathering
• Solution
– Server-side architectures that generate content
dynamically
HTTP basics
• HTTP is a stateless protocol:
– The client performs the request
– The web server responds and the transaction is done
• Each request is associated with a method, that
specifies the type of action the client wants
performed
• Available methods:
– GET
– POST
– Others: HEAD, PUT, DELETE, TRACE, OPTIONS
GET method
• The GET method is designed for getting
information (e.g., a document, a chart, the
result of a database query)
• The request contains some information that
describes what the user wants (e.g.,
coordinates x,y for a chart)
• This information is collected in the query
string, passed as a sequence of characters
appended to the request URLQuery string
http://my.server/page?par1=val1&par2=val2
POST method
• The POST method is designed for posting
information (a credit card number,
information that has to be stored in a
database)
• A POST request passes all its data, of
unlimited length, as part of its HTTP request
body
• The exchange is invisible to the client
– POST requests cannot be bookmarked or reloaded
POST method: form
Servlet1
request
service()
response
HttpServlet
out.println("<HTML>");
out.println("<HEAD><TITLE>Hello World Servlet
</TITLE></HEAD>");
out.println("<BODY>");
out.println("Hello, World!");
out.println("</BODY>");
out.println("</HTML>");
out.close();
}
}
Hello World! servlet
import java.io.*;
import javax.servlet.*;
Packages HTTP servlet
import javax.servlet.http.*;
interface
out.println("<HTML>");
out.println("<HEAD><TITLE>Hello World
Servlet</TITLE></HEAD>");
out.println("<BODY>");
out.println("Hello, World!");
out.println("</BODY>");
out.println("</HTML>");
out.close();
}
Hello World! servlet
out.println("<HTML>");
out.println("<HEAD><TITLE>Hello World
Servlet</TITLE></HEAD>");
out.println("<BODY>");
out.println("Hello, World!");
out.println("</BODY>");
out.println("</HTML>");
out.close();
}
Hello World! servlet
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
out.println("<HTML>");
out.println("<HEAD><TITLE>Hello World
Servlet</TITLE></HEAD>");
out.println("<BODY>");
out.println("Hello, World!");
out.println("</BODY>");
out.println("</HTML>");
out.close();
}
Hello World! servlet
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
Printed content
Configure a web application
• A web application is made of a set of servlets
that are stored in a project
• The description of the web application
content is contained in the web.xml file
• This file contains:
– The description of each servlet (name, class)
– The mapping of the servlet (used to reference the
servlet when accessing to the server)
Configuration file for HelloWorldServlet
Container for the servlets’ descriptions
<web-app>
<servlet>
<servlet-name>
HelloWorldServlet
</servlet-name>
<servlet-class>
it.polimi.tiw.examples.HelloWorldServlet
</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>
HelloWorldServlet
</servlet-name>
Mapping to a specific
path on the server <url-pattern>
/HelloWorld
</url-pattern>
</servlet-mapping>
</web-app>