51jsp-2up - 51 JSP 04 Nov 2010 CSCC09F Programming on the...

Info iconThis preview shows pages 1–4. Sign up to view the full content.

View Full Document Right Arrow Icon
51 JSP 04 Nov 2010 CSCC09 Programming on the Web 1 51 JSP CSCC09 Programming on the Web 1 JavaServer Pages: JSP CSCC09F Programming on the Web 51 JSP CSCC09 Programming on the Web 2 How Did We Get to Servlets? b We began the journey into server-side programming with CGI, an open and flexible, but non-scalable solution. Good for quick hacks, but not for enterprise (large scale, business) solutions. b Servlets are a Java-based alternative that benefits from Java’s widespread deployment, extensive libraries including JDBC for DB access, support for session management, high-performance threading model, and high quality container implementations as in Apache Tomcat, IBM WebSphere, etc. b A Servlet is a Java (Web) application that is run by a server in response to user requests.
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
51 JSP 04 Nov 2010 CSCC09 Programming on the Web 2 51 JSP CSCC09 Programming on the Web 3 How We Got Here b A Servlet implements life cycle methods that are invoked by the server at certain times and in response to certain events, e.g. init(), doGet(). b Write a Servlet by subclassing (extends) HttpServlet class c Override doGet() and doPost() c Find out about the user request from the HttpServletRequest param, and write output to the HttpServletResponse param. b API provides a convenient session abstraction: HttpSession session = request.getSession(); into which we can save and retrieve objects for shared use by all requests within a session. 51 JSP CSCC09 Programming on the Web 4 How We Got Here b The server is implemented as a Servlet “container” (in our case Tomcat) c manages Servlet class loading on demand c calls lifecycle methods implemented by Servlets in response to user (browser) requests c relays data between the client (e.g. browser) and Servlets c provides a shared ServletContext for all Servlets in a web app b can read and write attribute objects to/from this context c supports deployment config files (web.xml)
Background image of page 2
51 JSP 04 Nov 2010 CSCC09 Programming on the Web 3 51 JSP CSCC09 Programming on the Web 5 Where Servlets Don’t Excel b What’s lacking? b Let’s revisit the Hello World example … import java.io.*; import javax.servlet.*; import javax.servlet.http.*; public class HelloWorld extends HttpServlet { public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException { response.setContentType("text/html"); PrintWriter out = response.getWriter(); out.println("<html>"); out.println("<head>"); out.println("<title>Hello World!</title>"); out.println("</head>"); out.println("<body>"); out.println("<h1>Hello World!</h1>"); out.println("</body>"); out.println("</html>"); } } 51 JSP CSCC09 Programming on the Web 6 JavaServer Pages (JSP) b Servlets excel at tasks requiring significant programming effort. b
Background image of page 3

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Image of page 4
This is the end of the preview. Sign up to access the rest of the document.

This note was uploaded on 11/09/2010 for the course CS CSCC09 taught by Professor Alan during the Spring '10 term at University of Toronto- Toronto.

Page1 / 14

51jsp-2up - 51 JSP 04 Nov 2010 CSCC09F Programming on the...

This preview shows document pages 1 - 4. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online