1 / 18

CS320 Web and Internet Programming Introduction to Java Servlets

CS320 Web and Internet Programming Introduction to Java Servlets. Chengyu Sun California State University, Los Angeles. Java Web Application Components. Compiled Java classes (.class files) Servlets , beans, filters, ... Addtional Java libraries (.jar files) JavaServer Pages (JSPs)

schroederm
Download Presentation

CS320 Web and Internet Programming Introduction to Java Servlets

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. CS320 Web and Internet ProgrammingIntroduction to Java Servlets Chengyu Sun California State University, Los Angeles

  2. 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, ...

  3. Directory Structure of a Java Web Application Application Root Directory JSPs and static resources WEB-INF web.xml classes Compiled Java classes lib Additional Java libraries

  4. Directory Structure on CS3 Application Root Directory www JSPs and static resources WEB-INF web.xml classes Compiled Java classes lib Additional Java libraries

  5. Directory Structure of an Eclipse Dynamic Web Project Application Root Directory WebContent JSPs and static resources WebContent/WEB-INF WEB-INF web.xml build/classes classes Compiled Java classes WebContent/WEB-INF/lib lib Additional Java libraries

  6. 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” ); } }

  7. Some Simple Observations • Inherits from HttpServlet • http://java.sun.com/products/servlet/2.5/docs/servlet-2_5-mr2/javax/servlet/http/HttpServlet.html • There’s no main() method • doGet() • Input: HttpServletRequest • Output: HttpServletResponse  sent back to the client browser

  8. About web.xml • Web application deployment descriptor • <welcome-file-list> • <servlet> and <servlet-mapping> • More about web.xml in Java Servlet Specification

  9. Example: HelloWorld in HTML • Modify the HelloWorld servlet to output in HTML

  10. Generating HTML • HttpServletResponse • Set content type to “text/html” • setContentType() • Generate an HTML page • getWriter().println() • <html>, <head>, <body> ...

  11. Example: RequestCounter • Display the number of times a servlet is requested

  12. Servlet Life Cycle • When the servlet is loaded – init() • Executed only once • Per request – service() • dispatch to doXxx() • When the servlet is unloaded – destroy()

  13. Example: SharedRequestCounter • Use one servlet to count the number of requests, and another servlet to display the count

  14. Sharing Data among Servlets • HttpServlet • getServletContext() • HttpServletContext • setAttribute(String name, Object value) • getAttribute(String name)

  15. <load-on-startup> in web.xml • By default, a servlet is not created until it is accessed for the first time • Could cause problem if one servlet must run before another servlet • Use <load-on-startup> to have a servlet created during application startup

  16. <load-on-startup> Example <servlet> <servlet-name>ServletA</servlet-name> <servlet-class>cs320.cysun.ServletA</servlet-class> <load-on-startup>1</load-on-startup> </servlet> <servlet> <servlet-name>ServletA</servlet-name> <servlet-class>cs320.cysun.ServletB</servlet-class> <load-on-startup>2</load-on-startup> </servlet>

  17. <error-page> Example <error-page> <error-code>404</error-code> <location>/404.html</location> </error-page> <error-page> <error-code>403</error-code> <location>/403.html</location> </error-page>

  18. 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/

More Related