introduction to web app development allen day. notes this is a training not a presentation please...
TRANSCRIPT
![Page 1: Introduction to Web App Development Allen Day. Notes This is a training NOT a presentation Please ask questions](https://reader035.vdocument.in/reader035/viewer/2022062716/56649dc65503460f94ab9d9a/html5/thumbnails/1.jpg)
Introduction to Web App DevelopmentAllen Day
![Page 2: Introduction to Web App Development Allen Day. Notes This is a training NOT a presentation Please ask questions](https://reader035.vdocument.in/reader035/viewer/2022062716/56649dc65503460f94ab9d9a/html5/thumbnails/2.jpg)
Notes
• This is a training NOT a presentation• Please ask questions• https://tech.lds.org/wiki/Java_Stack_Training• Prerequisites
– Basic Java and HTML skills.– Installed LDSTech IDE (or other equivalent).– Installed App Server (such as Tomcat).
![Page 3: Introduction to Web App Development Allen Day. Notes This is a training NOT a presentation Please ask questions](https://reader035.vdocument.in/reader035/viewer/2022062716/56649dc65503460f94ab9d9a/html5/thumbnails/3.jpg)
Overview
• Basic Web App Architecture• HTTP• CGI Overview• Understanding the role of servlets• Maven Project Directory Structure• Servlet Life Cycle• Event Listeners• Servlet Filters• Servlet Response (Redirect, Request Dispatch)
![Page 4: Introduction to Web App Development Allen Day. Notes This is a training NOT a presentation Please ask questions](https://reader035.vdocument.in/reader035/viewer/2022062716/56649dc65503460f94ab9d9a/html5/thumbnails/4.jpg)
Basic Web App Architecture
Request
WWW Browser Web Server
Response
![Page 5: Introduction to Web App Development Allen Day. Notes This is a training NOT a presentation Please ask questions](https://reader035.vdocument.in/reader035/viewer/2022062716/56649dc65503460f94ab9d9a/html5/thumbnails/5.jpg)
Basic Web App Architecture
Request
WWW BrowserWeb Server
Response
![Page 6: Introduction to Web App Development Allen Day. Notes This is a training NOT a presentation Please ask questions](https://reader035.vdocument.in/reader035/viewer/2022062716/56649dc65503460f94ab9d9a/html5/thumbnails/6.jpg)
HTTP
Request
WWW BrowserWeb Server
Response
HTTP
![Page 7: Introduction to Web App Development Allen Day. Notes This is a training NOT a presentation Please ask questions](https://reader035.vdocument.in/reader035/viewer/2022062716/56649dc65503460f94ab9d9a/html5/thumbnails/7.jpg)
HTTP Request Methods
• GET• POST• HEAD• TRACE• PUT• DELETE• OPTIONS• CONNECT
![Page 8: Introduction to Web App Development Allen Day. Notes This is a training NOT a presentation Please ask questions](https://reader035.vdocument.in/reader035/viewer/2022062716/56649dc65503460f94ab9d9a/html5/thumbnails/8.jpg)
GET Method
• Simple• The total amount of characters in a GET is
limited.• The data you send with the GET is appended to
the URL, so whatever you send is exposed.
![Page 9: Introduction to Web App Development Allen Day. Notes This is a training NOT a presentation Please ask questions](https://reader035.vdocument.in/reader035/viewer/2022062716/56649dc65503460f94ab9d9a/html5/thumbnails/9.jpg)
POST Method
• Used for complex requests, such as form submissions.
• Parameters are stored in the body.
![Page 10: Introduction to Web App Development Allen Day. Notes This is a training NOT a presentation Please ask questions](https://reader035.vdocument.in/reader035/viewer/2022062716/56649dc65503460f94ab9d9a/html5/thumbnails/10.jpg)
CGI Overview
1. Submit Form
WWW Browser Web Server Application Server
2. Call CGI
3. CGI Program’s response4. CGI Program’s response
![Page 11: Introduction to Web App Development Allen Day. Notes This is a training NOT a presentation Please ask questions](https://reader035.vdocument.in/reader035/viewer/2022062716/56649dc65503460f94ab9d9a/html5/thumbnails/11.jpg)
CGI Process Formuse strict;main();sub main (){my $query;read( STDIN, $query, $ENV{CONTENT_LENGTH} );my @param = split( /&/, $query );my %pairs = ();foreach my $item ( @param ){my ($key, $value) = split( /=/, $item );$key =~ tr/+/ /;$value =~ tr/+/ /;$key =~ s/%([A-F\d]{2})/chr(hex($1))/ieg;$value =~ s/%([A-F\d]{2})/chr(hex($1))/ieg;$pairs{$key} = $value;}my $name = $pairs{name};my $email = $pairs{email};my $machine = $ENV{REMOTE_HOST};
print( STDOUT "Content-Type:text/html\r\n" );print( STDOUT "Status: 200 Ok\r\n" );print( STDOUT "\r\n" );print( STDOUT <<HTML );<html><head> <title>Form example output</title> </head><body><h1>welcome</h1><hr><p> Hi <em>$name</em> of <em>$email</em> from machine <em>$machine</em> </p><hr></body></html>HTML}
![Page 12: Introduction to Web App Development Allen Day. Notes This is a training NOT a presentation Please ask questions](https://reader035.vdocument.in/reader035/viewer/2022062716/56649dc65503460f94ab9d9a/html5/thumbnails/12.jpg)
CGI Issues
• May intentionally or unintentionally leak information about the host system that will help hackers break in.
• Scripts may be vulnerable to attacks in which the remote user tricks them into executing commands.
• Susceptible to Buffer overflows.• Insufficient input validation.• Each call to a CGI script runs as a separate process.• Simultaneous CGI requests cause the CGI script to be
copied and loaded into memory as many times as there are requests.
![Page 13: Introduction to Web App Development Allen Day. Notes This is a training NOT a presentation Please ask questions](https://reader035.vdocument.in/reader035/viewer/2022062716/56649dc65503460f94ab9d9a/html5/thumbnails/13.jpg)
Servlet Overview
Client Servlet ContainerWeb Server
Req
uest
Res
pons
e
![Page 14: Introduction to Web App Development Allen Day. Notes This is a training NOT a presentation Please ask questions](https://reader035.vdocument.in/reader035/viewer/2022062716/56649dc65503460f94ab9d9a/html5/thumbnails/14.jpg)
Advantages of Servlets
• Efficient• Convenient• Powerful• Portable• Inexpensive• Secure• Mainstream
![Page 15: Introduction to Web App Development Allen Day. Notes This is a training NOT a presentation Please ask questions](https://reader035.vdocument.in/reader035/viewer/2022062716/56649dc65503460f94ab9d9a/html5/thumbnails/15.jpg)
Advantages of Servlets
• Servlets stay loaded and client requests for a Servlet resource are handled as separate threads of a single running Servlet.
• A servlet can be run by a servlet engine in a restrictive environment, called a sandbox. This reduces security risks.
![Page 16: Introduction to Web App Development Allen Day. Notes This is a training NOT a presentation Please ask questions](https://reader035.vdocument.in/reader035/viewer/2022062716/56649dc65503460f94ab9d9a/html5/thumbnails/16.jpg)
Maven Project Directory Structure
pom.xml
web.xml
![Page 17: Introduction to Web App Development Allen Day. Notes This is a training NOT a presentation Please ask questions](https://reader035.vdocument.in/reader035/viewer/2022062716/56649dc65503460f94ab9d9a/html5/thumbnails/17.jpg)
pom.xml
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>org.lds.training</groupId> <artifactId>MyServlet</artifactId> <packaging>war</packaging> <version>1.0</version> <dependencies> <dependency> <groupId>javax.servlet</groupId> <artifactId>servlet-api</artifactId> <version>2.5</version> </dependency> </dependencies> </project>
![Page 18: Introduction to Web App Development Allen Day. Notes This is a training NOT a presentation Please ask questions](https://reader035.vdocument.in/reader035/viewer/2022062716/56649dc65503460f94ab9d9a/html5/thumbnails/18.jpg)
web.xml
<web-app xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" version="2.5">
<display-name>Welcome to Java Stack Training</display-name> <description>Introduction to Servlets</description>
<servlet> <display-name>HelloWorldServlet</display-name> <servlet-name>HelloWorldServlet</servlet-name> <servlet-class>org.lds.training.HelloWorldServlet</servlet-class> </servlet> <servlet-mapping> <servlet-name>HelloWorldServlet</servlet-name> <url-pattern>/HelloWorldServlet</url-pattern> </servlet-mapping>
</web-app>
![Page 19: Introduction to Web App Development Allen Day. Notes This is a training NOT a presentation Please ask questions](https://reader035.vdocument.in/reader035/viewer/2022062716/56649dc65503460f94ab9d9a/html5/thumbnails/19.jpg)
Lab 1: Simple Servlet
https://tech.lds.org/wiki/Introduction_To_Servlets#Lab_1_Simple_Servlet
![Page 20: Introduction to Web App Development Allen Day. Notes This is a training NOT a presentation Please ask questions](https://reader035.vdocument.in/reader035/viewer/2022062716/56649dc65503460f94ab9d9a/html5/thumbnails/20.jpg)
Servlet Life Cycle
1. Load class2. Instantiate servlet3. init4. service5. doGet, doPost, doTrace, doDelete, doPut…6. destroy
![Page 21: Introduction to Web App Development Allen Day. Notes This is a training NOT a presentation Please ask questions](https://reader035.vdocument.in/reader035/viewer/2022062716/56649dc65503460f94ab9d9a/html5/thumbnails/21.jpg)
Servlet Container
Client Servlet ContainerWeb Server
![Page 22: Introduction to Web App Development Allen Day. Notes This is a training NOT a presentation Please ask questions](https://reader035.vdocument.in/reader035/viewer/2022062716/56649dc65503460f94ab9d9a/html5/thumbnails/22.jpg)
Servlet Container
• Context (Web Application)• Session• Request
![Page 23: Introduction to Web App Development Allen Day. Notes This is a training NOT a presentation Please ask questions](https://reader035.vdocument.in/reader035/viewer/2022062716/56649dc65503460f94ab9d9a/html5/thumbnails/23.jpg)
Servlet Container
1. Loads the servlet class.2. Creates an instance of the servlet class.3. Initializes the servlet instance by calling the init
method.4. Handles client requests.5. If the container needs to remove the servlet it
finalizes the servlet by calling the servlet's destroy method.
![Page 24: Introduction to Web App Development Allen Day. Notes This is a training NOT a presentation Please ask questions](https://reader035.vdocument.in/reader035/viewer/2022062716/56649dc65503460f94ab9d9a/html5/thumbnails/24.jpg)
Servlet Container
• Communications support• Lifecycle Management• Multithreading Support• Declarative Security• JSP Support
![Page 25: Introduction to Web App Development Allen Day. Notes This is a training NOT a presentation Please ask questions](https://reader035.vdocument.in/reader035/viewer/2022062716/56649dc65503460f94ab9d9a/html5/thumbnails/25.jpg)
Servlet Container
Servlet ContainerWeb Server
request
response
Servlet
![Page 26: Introduction to Web App Development Allen Day. Notes This is a training NOT a presentation Please ask questions](https://reader035.vdocument.in/reader035/viewer/2022062716/56649dc65503460f94ab9d9a/html5/thumbnails/26.jpg)
Servlet Container
Servlet Container
request response
Servlet thread
![Page 27: Introduction to Web App Development Allen Day. Notes This is a training NOT a presentation Please ask questions](https://reader035.vdocument.in/reader035/viewer/2022062716/56649dc65503460f94ab9d9a/html5/thumbnails/27.jpg)
Servlet Container
Servlet Container
request
response
Servlet thread
Service()
![Page 28: Introduction to Web App Development Allen Day. Notes This is a training NOT a presentation Please ask questions](https://reader035.vdocument.in/reader035/viewer/2022062716/56649dc65503460f94ab9d9a/html5/thumbnails/28.jpg)
Servlet Container
Servlet Container
response
Servlet thread
Service()
doGet()
![Page 29: Introduction to Web App Development Allen Day. Notes This is a training NOT a presentation Please ask questions](https://reader035.vdocument.in/reader035/viewer/2022062716/56649dc65503460f94ab9d9a/html5/thumbnails/29.jpg)
Servlet Container
Servlet ContainerWeb Server
request
response
X
![Page 30: Introduction to Web App Development Allen Day. Notes This is a training NOT a presentation Please ask questions](https://reader035.vdocument.in/reader035/viewer/2022062716/56649dc65503460f94ab9d9a/html5/thumbnails/30.jpg)
HttpServletRequest
Method Description
getCookies() Obtain array of cookies
getHeader() Returns the value of the specified request header as a String.
getParameter() Returns the value of a request parameter as a String.
getRequestURL() Reconstructs the URL the client used to make the request.
getSession() Returns the current valid session associated with this request or creates a new session
![Page 31: Introduction to Web App Development Allen Day. Notes This is a training NOT a presentation Please ask questions](https://reader035.vdocument.in/reader035/viewer/2022062716/56649dc65503460f94ab9d9a/html5/thumbnails/31.jpg)
HttpServletRequest
String name = request.getParameter("fullName“);
String requestMethod = request.getMethod();
String userAgent = request.getHeader("User-Agent");
String host = request.getHeader("host");
![Page 32: Introduction to Web App Development Allen Day. Notes This is a training NOT a presentation Please ask questions](https://reader035.vdocument.in/reader035/viewer/2022062716/56649dc65503460f94ab9d9a/html5/thumbnails/32.jpg)
HttpServletResponse
Method Description
addCookie() Adds the specified cookie to the response
encodeURL() Encodes the URL by including the session id in it if needed
sendError() Sends an error response to the user with the specified error code
sendRedirect() Sends a redirect request to the user
![Page 33: Introduction to Web App Development Allen Day. Notes This is a training NOT a presentation Please ask questions](https://reader035.vdocument.in/reader035/viewer/2022062716/56649dc65503460f94ab9d9a/html5/thumbnails/33.jpg)
HttpServletResponse
response.setContentType("text/html"); PrintWriter out = response.getWriter(); Date today = new Date();
out.print("<html> " + "<body> " + "<h1 align=center>Hello World</h1> " + "<br> " + today + "</body> " +"</html>");
![Page 34: Introduction to Web App Development Allen Day. Notes This is a training NOT a presentation Please ask questions](https://reader035.vdocument.in/reader035/viewer/2022062716/56649dc65503460f94ab9d9a/html5/thumbnails/34.jpg)
Servlet Class
Extends java.servlet.http.HttpServlet
• init()• service()• doGet()• doPost()• destroy()
![Page 35: Introduction to Web App Development Allen Day. Notes This is a training NOT a presentation Please ask questions](https://reader035.vdocument.in/reader035/viewer/2022062716/56649dc65503460f94ab9d9a/html5/thumbnails/35.jpg)
init()
public void init() throws ServletException { // custom code goes here}
public void init(ServletConfig config) throws ServletException { super.init(ServletConfig) // custom code goes here}
![Page 36: Introduction to Web App Development Allen Day. Notes This is a training NOT a presentation Please ask questions](https://reader035.vdocument.in/reader035/viewer/2022062716/56649dc65503460f94ab9d9a/html5/thumbnails/36.jpg)
service()
public void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // Custom code goes here}
![Page 37: Introduction to Web App Development Allen Day. Notes This is a training NOT a presentation Please ask questions](https://reader035.vdocument.in/reader035/viewer/2022062716/56649dc65503460f94ab9d9a/html5/thumbnails/37.jpg)
doGet()
public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // Custom Code goes here}
![Page 38: Introduction to Web App Development Allen Day. Notes This is a training NOT a presentation Please ask questions](https://reader035.vdocument.in/reader035/viewer/2022062716/56649dc65503460f94ab9d9a/html5/thumbnails/38.jpg)
doPost()
public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // Custom Code goes here}
![Page 39: Introduction to Web App Development Allen Day. Notes This is a training NOT a presentation Please ask questions](https://reader035.vdocument.in/reader035/viewer/2022062716/56649dc65503460f94ab9d9a/html5/thumbnails/39.jpg)
destroy()
public void destroy() { // custom code goes here}
![Page 40: Introduction to Web App Development Allen Day. Notes This is a training NOT a presentation Please ask questions](https://reader035.vdocument.in/reader035/viewer/2022062716/56649dc65503460f94ab9d9a/html5/thumbnails/40.jpg)
Lab 2: Page Hit Counter
https://tech.lds.org/wiki/Introduction_To_Servlets#Lab_2_Page_Hit_Coun
ter
![Page 41: Introduction to Web App Development Allen Day. Notes This is a training NOT a presentation Please ask questions](https://reader035.vdocument.in/reader035/viewer/2022062716/56649dc65503460f94ab9d9a/html5/thumbnails/41.jpg)
Event Listeners
![Page 42: Introduction to Web App Development Allen Day. Notes This is a training NOT a presentation Please ask questions](https://reader035.vdocument.in/reader035/viewer/2022062716/56649dc65503460f94ab9d9a/html5/thumbnails/42.jpg)
Event Listeners
• javax.servlet.ServletContextListener• javax.servlet.ServletContextAttributeListener• javax.servlet.http.HttpSessionListener• javax.servlet.http.HttpSessionAttributeListener• javax.servlet.http.HttpSessionActivationListener• javax.servlet.http.HttpSessionBindingListener• javax.servlet.http.HttpRequestListener• javax.servlet.http.HttpRequestAttributeListener
![Page 43: Introduction to Web App Development Allen Day. Notes This is a training NOT a presentation Please ask questions](https://reader035.vdocument.in/reader035/viewer/2022062716/56649dc65503460f94ab9d9a/html5/thumbnails/43.jpg)
Event Listeners
• javax.servlet.ServletContextListener• javax.servlet.http.HttpSessionListener• javax.servlet.http.HttpSessionActivationListener• javax.servlet.http.HttpRequestListener
![Page 44: Introduction to Web App Development Allen Day. Notes This is a training NOT a presentation Please ask questions](https://reader035.vdocument.in/reader035/viewer/2022062716/56649dc65503460f94ab9d9a/html5/thumbnails/44.jpg)
web.xml
<listener> <listener-class>org.lds.training.HelloWorldSessionListener</listenerclass> </listener> <listener> <listener-class>org.lds.training.HelloWorldContextListener</listener-class> </listener>
![Page 45: Introduction to Web App Development Allen Day. Notes This is a training NOT a presentation Please ask questions](https://reader035.vdocument.in/reader035/viewer/2022062716/56649dc65503460f94ab9d9a/html5/thumbnails/45.jpg)
Servlet Filters
![Page 46: Introduction to Web App Development Allen Day. Notes This is a training NOT a presentation Please ask questions](https://reader035.vdocument.in/reader035/viewer/2022062716/56649dc65503460f94ab9d9a/html5/thumbnails/46.jpg)
Servlet Filters
Client Servlet ContainerWeb Server
Re
qu
est
Re
spo
nse
Filter 1
Filter 2
![Page 47: Introduction to Web App Development Allen Day. Notes This is a training NOT a presentation Please ask questions](https://reader035.vdocument.in/reader035/viewer/2022062716/56649dc65503460f94ab9d9a/html5/thumbnails/47.jpg)
Servlet Filter
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
// preprocessing code goes here HttpServletResponse res = (HttpServletResponse)response; String name = request.getParameter("fullName");
if (name.equals("")) { res.sendRedirect("index.html"); return; }
// pass the request along the filter chain chain.doFilter(request, response);
// postprocessing code goes here}
![Page 48: Introduction to Web App Development Allen Day. Notes This is a training NOT a presentation Please ask questions](https://reader035.vdocument.in/reader035/viewer/2022062716/56649dc65503460f94ab9d9a/html5/thumbnails/48.jpg)
web.xml
<filter> <filter-name>timer</filter-name> <filter-class>filter.TimerFilter</filter-class> </filter>
<filter-mapping> <filter-name>timer</filter-name> <servlet-name>myservlet</servlet-name> <url-pattern>/mypath/*</url-pattern> </filter-mapping>
![Page 49: Introduction to Web App Development Allen Day. Notes This is a training NOT a presentation Please ask questions](https://reader035.vdocument.in/reader035/viewer/2022062716/56649dc65503460f94ab9d9a/html5/thumbnails/49.jpg)
Redirect
response.sendRedirect(http://lds.org/?lang=eng);
![Page 50: Introduction to Web App Development Allen Day. Notes This is a training NOT a presentation Please ask questions](https://reader035.vdocument.in/reader035/viewer/2022062716/56649dc65503460f94ab9d9a/html5/thumbnails/50.jpg)
Request Dispatch
// from a ServletRequestRequestDispatcher view = request.getRequestDispatcher(“MyOtherServlet”);
// from a ServletContextRequestDispatcher view = getServletContext().getRequestDispatcher(“/MyOtherServlet”);
view.forward(request, response);
![Page 51: Introduction to Web App Development Allen Day. Notes This is a training NOT a presentation Please ask questions](https://reader035.vdocument.in/reader035/viewer/2022062716/56649dc65503460f94ab9d9a/html5/thumbnails/51.jpg)
Lab 3: Login Filter
https://tech.lds.org/wiki/Introduction_To_Servlets#Lab_3_Login_Filter
![Page 52: Introduction to Web App Development Allen Day. Notes This is a training NOT a presentation Please ask questions](https://reader035.vdocument.in/reader035/viewer/2022062716/56649dc65503460f94ab9d9a/html5/thumbnails/52.jpg)
Credit where credit is due
• http://en.wikipedia.org/wiki/Common_Gateway_Interface• http://en.wikipedia.org/wiki/Java_Servlet• Head First Servlets & JSP Bryan Basham, Kathy Sierra & Bert Bates
• More Servlets and JavaServer Pages Marty Hall• http://maven.apache.org/guides/introduction/introduction-to-the-standard-directory-layout.html
• http://download.oracle.com/javaee/5/api/• http://download.oracle.com/docs/cd/B32110_01/web.1013/b28959/filters.htm
• Images from the Microsoft Clip Art gallery