Java Servlets Lec 27 Creating a Simple Web
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 n Servlets are based on two main packages q q n Generic. Servlet q n javax. servlet. http For writing protocol independent servlets Http. Servlet q q Extends from Generic. Servlet class Adds functionality for writing HTTP specific servlets
Servlet class hierarchy Object Generic. Servlet Server. Request Servlet. Response Http. Servlet. Request Http. Servlet. Response javax. servlet Http. Servlet javax. servlet. http
Writing Servlet n Every servlet must implement the javax. servlet. Servlet interface q Contains the servlet’s life cycle methods etc. n These are implemented by Generic. Servlet and Http. Servlet classes n Extend your servlet from one of these classes and add your own functionality public class My. Servlet extends Generic. Servlet public class Hello. Servlet extends Http. Servlet
Types of HTTP Requests q q q Get Post Delete Options Put Trace
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 Servlets Client Server • Post -- Attr/Val pairs attatched with the request body
HTTP Request Example Request parameters etc
Writing Servlet Steps for making a Hello. World. Servlet 1. Create a directory structure for your application (myapp). This is a one time process for any application 2. Create a Hello. World. Servlet source file by extending this class from Http. Servlet and override your desired method 3. Compile it (If compiler complains of not having required packages, check your class paths)
Writing Servlet Steps for making a Hello. World. Servlet 1. 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 2. Create a deployment descriptor (web. xml) and put it inside WEBINF folder 3. 4. Restart your server, if already running Access your application using Web browser
Hello. Servlet Example Code
Hello. World. Servlet import java. io. *; import javax. servlet. http. *; public class Hello. World. Servlet extends Http. Servlet { public void do. Get(Http. Servlet. Request request, Http. Servlet. Response response) throws Servlet. Exception, IOException { Print. Writer out = response. get. Writer(); out. println(“Hello World!”); } }
web. xml <? xml version="1. 0" encoding="ISO-8859 -1"? > <web-app> <servlet-name>Hello. World. Servlet</servlet-name> <servlet-class>Hello. World. Servlet</servlet-class> </servlet> <servlet-mapping> <servlet-name>Hello. World. Servlet</servlet-name> <url-pattern>/myfirstservlet</url-pattern> </servlet-mapping> </web-app>
Compiling and Invoking Servlets n Compile Hello. World. Servlet. java using javac command n Put Hello. World. Servlet class in install_dir/webapps/myapp/WEB-INF/classes n Put web. xml file in install_dir/webapps/myapp/WEB-INF n Invoke your servlet by writing following command in the web browser http: //localhost: 8080/myapp/myfirstservlet
Lets do it LIVE
Free Servlet and JSP Engines n Apache Tomcat q n Allaire/Macromedia JRun q n http: //www. allaire. com/products/jrun New Atlanta Servlet. Exec q n http: //jakarta. apache. org/tomcat/ http: //www. servletexec. com Causho’s Resin q http: //www. caucho. com
- Slides: 18