1 / 18

Java Servlets

Java Servlets. Lec 27. Creating a Simple Web Application in Tomcat. Tomcat Directory Structure. myapp. Tomcat Setup. Folder for HTML and JSP pages. Folder for servlet and other java classes. Servlet Types. Servlets are based on two main packages javax.servlet javax.servlet.http

cormac
Télécharger la présentation

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. Java Servlets Lec 27

  2. Creating a SimpleWeb Application in Tomcat

  3. Tomcat Directory Structure myapp

  4. Tomcat Setup Folder for HTML and JSP pages Folder for servlet and other java classes

  5. Servlet Types • Servlets are based on two main packages • javax.servlet • javax.servlet.http • GenericServlet • For writing protocol independent servlets • HttpServlet • Extends from GenericServlet class • Adds functionality for writing HTTP specific servlets

  6. Servlet class hierarchy Object GenericServlet ServerRequest ServletResponse javax.servlet HttpServlet HttpServletRequest HttpServletResponse javax.servlet.http

  7. Writing Servlet • Every servlet must implement the javax.servlet.Servlet interface • Contains the servlet’s life cycle methods etc. • These are implemented by GenericServlet and HttpServlet classes • Extend your servlet from one of these classes and add your own functionality public class MyServlet extends GenericServlet public class HelloServlet extends HttpServlet

  8. Types of HTTP Requests • Get • Post • Delete • Options • Put • Trace

  9. Servlets How HTTP Sends Request • Some HTTP request types • Get -- Attr/Val pairs attached after ? of URL • E.g. http://www.gmail.com/register?name=ali • Post -- Attr/Val pairs attatched with the request body Client Server

  10. HTTP Request Example Request parameters etc

  11. Writing Servlet Steps for making a HelloWorldServlet • Create a directory structure for your application (myapp). This is a one time process for any application • Create a HelloWorldServlet source file by extending this class from HttpServlet and override your desired method • Compile it (If compiler complains of not having required packages, check your class paths)

  12. Writing Servlet Steps for making a HelloWorldServlet • Place the class file in the classes folder of your webapplication. (myapp) If you are using packages (recommended) then create a complete directory structure under classes folder • Create a deployment descriptor (web.xml) and put it inside WEB-INF folder • Restart your server, if already running • Access your application using Web browser

  13. HelloServlet Example Code

  14. HelloWorldServlet import java.io.*; import javax.servlet.*; import javax.servlet.http.*; public class HelloWorldServlet extends HttpServlet { public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { PrintWriter out = response.getWriter(); out.println(“Hello World!”); } }

  15. web.xml <?xml version="1.0" encoding="ISO-8859-1"?> <web-app> <servlet> <servlet-name>HelloWorldServlet</servlet-name> <servlet-class>HelloWorldServlet</servlet-class> </servlet> <servlet-mapping> <servlet-name>HelloWorldServlet</servlet-name> <url-pattern>/myfirstservlet</url-pattern> </servlet-mapping> </web-app>

  16. Compiling and Invoking Servlets • Compile HelloWorldServlet.javausing javac command • Put HelloWorldServlet class in install_dir/webapps/myapp/WEB-INF/classes • Put web.xml file in install_dir/webapps/myapp/WEB-INF • Invoke your servlet by writing following command in the web browser http://localhost:8080/myapp/myfirstservlet

  17. Lets do it LIVE

  18. Free Servlet and JSP Engines • Apache Tomcat • http://jakarta.apache.org/tomcat/ • Allaire/Macromedia JRun • http://www.allaire.com/products/jrun • New Atlanta ServletExec • http://www.servletexec.com • Causho’s Resin • http://www.caucho.com

More Related