Knowledge Sharing
Agenda What is Servlet? Servlet hierarchy & lifecycle Servlet program structure Deploying servlets on Tomcat HTTP Servlets and HTTP request methods Understanding servlet API Responding to requests Accessing form input data Working with header fields URL redirecting
What is servlet? A Java class which conforms to the Java Servlet API, a protocol by which a Java class may respond to HTTP requests.  A Servlet is an object that receives a request and generates a response based on that request Servlets are the Java counterpart to non-Java dynamic Web content technologies such as CGI and ASP.NET ( http://en.wikipedia.org/wiki/Java_Servlet )
Advantages of Servlets Faster than CGI scripts because use a different process model. Use standard API that is supported by many Web servers. Have all of the advantages of the Java languages, including ease of development and platform independence. Can access the large set of APIs available for the Java platform.   ( http://java.sun.com )
Servlet V.S. CGI A  s ervlet stays in memory between requests. A CGI program   needs to be loaded and started for each CGI request . A servlet doesn’t run in a separate process.  This removes the overhead of creating a new process for each request  which CGI does. A servlet allows each request to be handled by a separate  Java thread  within the web server process (same amount of threads as request but there only be one copy of the servlet class crea ted in memory).  ( http://www.novocode.com/doc/servlet-essentials/chapter1.html )
Servlet Hierarchy Servlets (javax.servlet.Servlet interface) Generic Servlet (javax.servlet.GenericServlet) HTTP Servlet (javax.servlet.http.HttpServlet) MyServlet
Servlet Lifecycle
Servlet Lifecycle (cont.)
Servlet Program Structure
[Screencast] Create simple servlet
[Showcase] Servlet Lifecycle
[Showcase] Servlet Lifecycle (cont.)
[Showcase] Servlet Lifecycle (cont.)
Deploying Servlets on Tomcat Create a servlet Compile the servlet : No different from compiling java program  ( javac OurServlet.java ) javax.servlet.*  and  javax.servlet.http.*  must be added in Classpath. Create web application folder under webapps folder in Tomcat (web container). Create WEB-INF folder under web application folder. http://www.roseindia.net/servlets/introductiontoconfigrationservlet.shtml
Deploying Servlets on Tomcat (cont) Create ‘web.xml’ file and ‘classes’ folder under WEB-INF. Copy the servlet class file into ‘classes’ folder  Edit web.xml to include servlet’s name and url pattern .  http://www.roseindia.net/servlets/introductiontoconfigrationservlet.shtml
Deploying Servlets on Tomcat (cont) Run Tomcat server and then execute the Servlet http://www.roseindia.net/servlets/introductiontoconfigrationservlet.shtml
HTTP Servlet and HTTP request method
HTTP Servlet and HTTP request method (cont.) HTTP is a request-response oriented protocol. An HTTP request consists of : Request method ( GET, HEAD, PUT, POST, DELETE, OPTIONS and TRACE) URI Header  fields Body An HTTP response contains : Result code Header fields and body http://www.novocode.com/doc/servlet-essentials/chapter1.html
Understanding Servlet API Packages in the Java Servlet API 2.1 http://java.sun.com/products/servlet/2.1/servlet-2.1.pdf
Understanding Servlet API (cont) Packages in the Java Servlet API 2.1 http://java.sun.com/products/servlet/2.1/servlet-2.1.pdf
Request  Heade r Accept Accept-charset Accept-encoding Accept-language Authorization Connection Content-length Cookie The most common headers : From Host If-modified-since Pragma Referer User-agent UA-Pixels, UA-color, UA-OS, UA-CPU http://www.apl.jhu.edu/~hall/java/Servlet-Tutorial/Servlet-Tutorial-Request-Headers.html
Request  Heade r (cont.) Call the getHeader() method of the HttpServletRequest, which returns a String if the header was supplied on this request, null otherwise. Call the getHeaderNames() to get an Enumeration of all header names received on the particular request. Reading request header from Servlets  : http://www.apl.jhu.edu/~hall/java/Servlet-Tutorial/Servlet-Tutorial-Request-Headers.html
Accessing form input data Call getParameter() method of the HttpServletRequest, supplying the parameter name (case sensitive) as an argument.  Call getParameterValues() method to get the parameter that probably have more than one value. Call getParameterNames() to get full list of all parameters received on the particular request. http://www.apl.jhu.edu/~hall/java/Servlet-Tutorial/Servlet-Tutorial-Form-Data.html
URL Redirecting Using sendRedirect() method of HttpServletResponse class. Using forward() method of RequestDispatcher class. http://www.apl.jhu.edu/~hall/java/Servlet-Tutorial/Servlet-Tutorial-Form-Data.html
URL Redirecting (cont.) sendRedirect()  It is a new request from the client, and the way to pass data is through the session or with web parameters (url?name=value) RequestDispatcher The target servlet/JSP receives the same request/response objects as the original servlet/JSP. Therefore, can pass data between them using request.setAttribute(). http://www.theserverside.com/discussions/thread.tss?thread_id=26425
URL Redirecting (cont.) sendRedirect()  It will updates the browser history.  RequestDispatcher If use  RequestDispatcher to forward from Servlet-2 to JSP-3, the user's address bar will read http://[host]/Servlet-2. A reload/refresh will execute both Servlet-2 and JSP-3. Both kinds of redirections are useful, depending on the precise effect you want. http://www.theserverside.com/discussions/thread.tss?thread_id=26425
Thank you Fahmi Jafar (fahmijafar@fahmijafar.net)

Knowledge Sharing : Java Servlet

  • 1.
  • 2.
    Agenda What isServlet? Servlet hierarchy & lifecycle Servlet program structure Deploying servlets on Tomcat HTTP Servlets and HTTP request methods Understanding servlet API Responding to requests Accessing form input data Working with header fields URL redirecting
  • 3.
    What is servlet?A Java class which conforms to the Java Servlet API, a protocol by which a Java class may respond to HTTP requests. A Servlet is an object that receives a request and generates a response based on that request Servlets are the Java counterpart to non-Java dynamic Web content technologies such as CGI and ASP.NET ( http://en.wikipedia.org/wiki/Java_Servlet )
  • 4.
    Advantages of ServletsFaster than CGI scripts because use a different process model. Use standard API that is supported by many Web servers. Have all of the advantages of the Java languages, including ease of development and platform independence. Can access the large set of APIs available for the Java platform. ( http://java.sun.com )
  • 5.
    Servlet V.S. CGIA s ervlet stays in memory between requests. A CGI program needs to be loaded and started for each CGI request . A servlet doesn’t run in a separate process. This removes the overhead of creating a new process for each request which CGI does. A servlet allows each request to be handled by a separate Java thread within the web server process (same amount of threads as request but there only be one copy of the servlet class crea ted in memory). ( http://www.novocode.com/doc/servlet-essentials/chapter1.html )
  • 6.
    Servlet Hierarchy Servlets(javax.servlet.Servlet interface) Generic Servlet (javax.servlet.GenericServlet) HTTP Servlet (javax.servlet.http.HttpServlet) MyServlet
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
    Deploying Servlets onTomcat Create a servlet Compile the servlet : No different from compiling java program ( javac OurServlet.java ) javax.servlet.* and javax.servlet.http.* must be added in Classpath. Create web application folder under webapps folder in Tomcat (web container). Create WEB-INF folder under web application folder. http://www.roseindia.net/servlets/introductiontoconfigrationservlet.shtml
  • 15.
    Deploying Servlets onTomcat (cont) Create ‘web.xml’ file and ‘classes’ folder under WEB-INF. Copy the servlet class file into ‘classes’ folder Edit web.xml to include servlet’s name and url pattern . http://www.roseindia.net/servlets/introductiontoconfigrationservlet.shtml
  • 16.
    Deploying Servlets onTomcat (cont) Run Tomcat server and then execute the Servlet http://www.roseindia.net/servlets/introductiontoconfigrationservlet.shtml
  • 17.
    HTTP Servlet andHTTP request method
  • 18.
    HTTP Servlet andHTTP request method (cont.) HTTP is a request-response oriented protocol. An HTTP request consists of : Request method ( GET, HEAD, PUT, POST, DELETE, OPTIONS and TRACE) URI Header fields Body An HTTP response contains : Result code Header fields and body http://www.novocode.com/doc/servlet-essentials/chapter1.html
  • 19.
    Understanding Servlet APIPackages in the Java Servlet API 2.1 http://java.sun.com/products/servlet/2.1/servlet-2.1.pdf
  • 20.
    Understanding Servlet API(cont) Packages in the Java Servlet API 2.1 http://java.sun.com/products/servlet/2.1/servlet-2.1.pdf
  • 21.
    Request Header Accept Accept-charset Accept-encoding Accept-language Authorization Connection Content-length Cookie The most common headers : From Host If-modified-since Pragma Referer User-agent UA-Pixels, UA-color, UA-OS, UA-CPU http://www.apl.jhu.edu/~hall/java/Servlet-Tutorial/Servlet-Tutorial-Request-Headers.html
  • 22.
    Request Header (cont.) Call the getHeader() method of the HttpServletRequest, which returns a String if the header was supplied on this request, null otherwise. Call the getHeaderNames() to get an Enumeration of all header names received on the particular request. Reading request header from Servlets : http://www.apl.jhu.edu/~hall/java/Servlet-Tutorial/Servlet-Tutorial-Request-Headers.html
  • 23.
    Accessing form inputdata Call getParameter() method of the HttpServletRequest, supplying the parameter name (case sensitive) as an argument. Call getParameterValues() method to get the parameter that probably have more than one value. Call getParameterNames() to get full list of all parameters received on the particular request. http://www.apl.jhu.edu/~hall/java/Servlet-Tutorial/Servlet-Tutorial-Form-Data.html
  • 24.
    URL Redirecting UsingsendRedirect() method of HttpServletResponse class. Using forward() method of RequestDispatcher class. http://www.apl.jhu.edu/~hall/java/Servlet-Tutorial/Servlet-Tutorial-Form-Data.html
  • 25.
    URL Redirecting (cont.)sendRedirect() It is a new request from the client, and the way to pass data is through the session or with web parameters (url?name=value) RequestDispatcher The target servlet/JSP receives the same request/response objects as the original servlet/JSP. Therefore, can pass data between them using request.setAttribute(). http://www.theserverside.com/discussions/thread.tss?thread_id=26425
  • 26.
    URL Redirecting (cont.)sendRedirect() It will updates the browser history. RequestDispatcher If use RequestDispatcher to forward from Servlet-2 to JSP-3, the user's address bar will read http://[host]/Servlet-2. A reload/refresh will execute both Servlet-2 and JSP-3. Both kinds of redirections are useful, depending on the precise effect you want. http://www.theserverside.com/discussions/thread.tss?thread_id=26425
  • 27.
    Thank you FahmiJafar (fahmijafar@fahmijafar.net)