Presentation is loading. Please wait.

Presentation is loading. Please wait.

Introduction to Java Servlets

Similar presentations


Presentation on theme: "Introduction to Java Servlets"— Presentation transcript:

1 Introduction to Java Servlets
Dave Bryson Informs Inc.

2 Contents 1. What's a Servlet? 2. How's it different from CGI?
3. Lifecycle of a Servlet 4. Scope of a Web application 5. Http Request 6. Http Response 7. Session Management 8. Step by Step 8.1 What you'll need 8.2 Create webapp 8.3 Create the servlet 8.4 Configure web.xml 9. Resources

3 . What's a Servlet? Java class building off javax.servlet package
Extends the capabilites of a web server Gateway into your application

4 2. How's it different from CGI?
Multithreaded Persistent connection J2EE servers everywhere

5 3. Lifecycle of a Servlet Initialization Service Destroy

6 4. Scope of a Web application
Servlet Context Session Request Page (JSP)

7 5. Http Request Wrapper object for the incoming request.
Provides a single object to access: Parameters Session information Header data

8 6. Http Response Wrapper object for the outgoing response.
Provides a single object to: Get an output stream Add a cookie Set content type Set status codes

9 7. Session Management Provides state management across
requests from the same user. Can be used to: Store objects across requests Used for application security Time out a session

10 8. Step by Step Subsections 8.1 What you'll need 8.2 Create webapp
8.3 Create the servlet 8.4 Configure web.xml

11 8.1 What you'll need Java Developer's Kit from Sun - Servlet Engine - Something to write code in -

12 8.2 Create webapp Under webapps directory, create the following directory structure: mydemo |-- WEB-INF | |- classes | | | |-mydemo web.xml

13 8.3 Create the servlet Under the classes/mydemo directory create the DemoServlet class: package mydemo; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import javax.servlet.ServletException; import java.io.IOException; import java.io.PrintWriter; public class DemoServlet extends HttpServlet { /** * Called once at startup */ public void init() throws ServletException System.out.println("Init() the Servlet"); } public void doGet( HttpServletRequest req, HttpServletResponse resp ) throws ServletException, IOException

14 8.3 Create the servlet { String name = req.getParameter("name");
// Handle a null if ( name == null ) name = "You forget to add name."; } // Set the content type resp.setContentType("text/html"); // Generate the HTML String output = getOutput(name); // Write to the browser PrintWriter writer = null; try writer = resp.getWriter(); writer.write(output); ) catch (IOException e) e.printStackTrace();

15 8.3 Create the servlet finally { writer.flush(); writer.close(); } private String getOutput( String name ) StringBuffer buffer = new StringBuffer(); buffer.append("<html><body><h2>Hello. Your name is: "); buffer.append(name); buffer.append("</h2></body></html>"); return buffer.toString(); Compile the servlet ( make sure you have the servlet.jar in your classpath ).

16 8.4 Configure web.xml <?xml version="1.0" encoding="ISO "?> <!DOCTYPE web-app PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.2//EN" " <web-app> <servlet> <servlet-name> Demo </servlet-name> <servlet-class> mydemo.DemoServlet </servlet-class> </servlet> <servlet-mapping> <url-pattern> /Demo </url-pattern> </servlet-mapping> </web-app> Point your browser to

17 9. Resources Sun Microsystems - http://java.sun.com
Jakarta Apache - Weblogs -


Download ppt "Introduction to Java Servlets"

Similar presentations


Ads by Google