cs320 web and internet programming introduction to java servlets chengyu sun california state...
TRANSCRIPT
![Page 1: CS320 Web and Internet Programming Introduction to Java Servlets Chengyu Sun California State University, Los Angeles](https://reader036.vdocument.in/reader036/viewer/2022083009/5697bff31a28abf838cbc85a/html5/thumbnails/1.jpg)
CS320 Web and Internet ProgrammingIntroduction to Java Servlets
Chengyu SunCalifornia State University, Los Angeles
![Page 2: CS320 Web and Internet Programming Introduction to Java Servlets Chengyu Sun California State University, Los Angeles](https://reader036.vdocument.in/reader036/viewer/2022083009/5697bff31a28abf838cbc85a/html5/thumbnails/2.jpg)
Java Web Application Components
Compiled Java classes (.class files) Servlets, beans, filters, ...
Addtional Java libraries (.jar files)JavaServer Pages (JSPs)Static resources HTML, CSS, images, ...
Metadata files web.xml, ...
![Page 3: CS320 Web and Internet Programming Introduction to Java Servlets Chengyu Sun California State University, Los Angeles](https://reader036.vdocument.in/reader036/viewer/2022083009/5697bff31a28abf838cbc85a/html5/thumbnails/3.jpg)
Directory Structure of a Java Web Application
Application Root Directory
WEB-INF
JSPs and static resources
classes
web.xml
lib
Compiled Java classes
Additional Java libraries
![Page 4: CS320 Web and Internet Programming Introduction to Java Servlets Chengyu Sun California State University, Los Angeles](https://reader036.vdocument.in/reader036/viewer/2022083009/5697bff31a28abf838cbc85a/html5/thumbnails/4.jpg)
Directory Structure on CS3
Application Root Directory
WEB-INF
JSPs and static resources
classes
web.xml
lib
Compiled Java classes
Additional Java libraries
www
![Page 5: CS320 Web and Internet Programming Introduction to Java Servlets Chengyu Sun California State University, Los Angeles](https://reader036.vdocument.in/reader036/viewer/2022083009/5697bff31a28abf838cbc85a/html5/thumbnails/5.jpg)
Directory Structure of an Eclipse Dynamic Web Project
Application Root Directory
WEB-INF
JSPs and static resources
classes
web.xml
lib
Compiled Java classes
Additional Java libraries
WebContent
WebContent/WEB-INF
build/classes
WebContent/WEB-INF/lib
![Page 6: CS320 Web and Internet Programming Introduction to Java Servlets Chengyu Sun California State University, Los Angeles](https://reader036.vdocument.in/reader036/viewer/2022083009/5697bff31a28abf838cbc85a/html5/thumbnails/6.jpg)
Servlet HelloWorld
import java.io.*;import javax.servlet.*;import javax.servlet.http.*;
public class HelloWorld extends HttpServlet { public void doGet( HttpServletRequest request,
HttpServletResponse response ) throws ServletExceptoin, IOException { PrintWriter out = response.getWriter(); out.println( “Hello World” ); }}
![Page 7: CS320 Web and Internet Programming Introduction to Java Servlets Chengyu Sun California State University, Los Angeles](https://reader036.vdocument.in/reader036/viewer/2022083009/5697bff31a28abf838cbc85a/html5/thumbnails/7.jpg)
Some Simple Observations
Inherits from HttpServlet http://java.sun.com/products/servlet/2.5/doc
s/servlet-2_5-mr2/javax/servlet/http/HttpServlet.html
There’s no main() methoddoGet() Input: HttpServletRequest Output: HttpServletResponse sent back
to the client browser
![Page 8: CS320 Web and Internet Programming Introduction to Java Servlets Chengyu Sun California State University, Los Angeles](https://reader036.vdocument.in/reader036/viewer/2022083009/5697bff31a28abf838cbc85a/html5/thumbnails/8.jpg)
About web.xml
Web application deployment descriptor <welcome-file-list> <servlet> and <servlet-mapping>
More about web.xml in Java Servlet Specification
![Page 9: CS320 Web and Internet Programming Introduction to Java Servlets Chengyu Sun California State University, Los Angeles](https://reader036.vdocument.in/reader036/viewer/2022083009/5697bff31a28abf838cbc85a/html5/thumbnails/9.jpg)
Example: HelloWorld in HTML
Modify the HelloWorld servlet to output in HTML
![Page 10: CS320 Web and Internet Programming Introduction to Java Servlets Chengyu Sun California State University, Los Angeles](https://reader036.vdocument.in/reader036/viewer/2022083009/5697bff31a28abf838cbc85a/html5/thumbnails/10.jpg)
Generating HTML
HttpServletResponseSet content type to “text/html” setContentType()
Generate an HTML page getWriter().println()
<html>, <head>, <body> ...
![Page 11: CS320 Web and Internet Programming Introduction to Java Servlets Chengyu Sun California State University, Los Angeles](https://reader036.vdocument.in/reader036/viewer/2022083009/5697bff31a28abf838cbc85a/html5/thumbnails/11.jpg)
Example: Request Counter
Display the number of times a servlet is requested
![Page 12: CS320 Web and Internet Programming Introduction to Java Servlets Chengyu Sun California State University, Los Angeles](https://reader036.vdocument.in/reader036/viewer/2022083009/5697bff31a28abf838cbc85a/html5/thumbnails/12.jpg)
Servlet Life Cycle
When the servlet is loaded – init() Executed only once
Per request – service() dispatch to doXxx()
When the servlet is unloaded – destroy()
![Page 13: CS320 Web and Internet Programming Introduction to Java Servlets Chengyu Sun California State University, Los Angeles](https://reader036.vdocument.in/reader036/viewer/2022083009/5697bff31a28abf838cbc85a/html5/thumbnails/13.jpg)
Example: Request Counter Display
Use one servlet to count the number of requests, and another servlet to display the count
![Page 14: CS320 Web and Internet Programming Introduction to Java Servlets Chengyu Sun California State University, Los Angeles](https://reader036.vdocument.in/reader036/viewer/2022083009/5697bff31a28abf838cbc85a/html5/thumbnails/14.jpg)
Sharing Data among Servlets
HttpServlet getServletContext()
HttpServletContext setAttribute(String name, Object
value) getAttribute(String name)
![Page 15: CS320 Web and Internet Programming Introduction to Java Servlets Chengyu Sun California State University, Los Angeles](https://reader036.vdocument.in/reader036/viewer/2022083009/5697bff31a28abf838cbc85a/html5/thumbnails/15.jpg)
Debugging Servlets
Using the Eclipse debugger Set break points Debug As Debug on Server
View the source of the generated HTML Validation - http://validator.w3.org/