java servlets lec 27. creating a simple web application in tomcat
TRANSCRIPT
![Page 1: Java Servlets Lec 27. Creating a Simple Web Application in Tomcat](https://reader034.vdocuments.site/reader034/viewer/2022051417/56649ee15503460f94bf2602/html5/thumbnails/1.jpg)
Java Servlets
Lec 27
![Page 2: Java Servlets Lec 27. Creating a Simple Web Application in Tomcat](https://reader034.vdocuments.site/reader034/viewer/2022051417/56649ee15503460f94bf2602/html5/thumbnails/2.jpg)
Creating a SimpleWeb Application in Tomcat
![Page 3: Java Servlets Lec 27. Creating a Simple Web Application in Tomcat](https://reader034.vdocuments.site/reader034/viewer/2022051417/56649ee15503460f94bf2602/html5/thumbnails/3.jpg)
Tomcat Directory Structure
myapp
![Page 4: Java Servlets Lec 27. Creating a Simple Web Application in Tomcat](https://reader034.vdocuments.site/reader034/viewer/2022051417/56649ee15503460f94bf2602/html5/thumbnails/4.jpg)
Tomcat Setup
Folder for HTML and JSP pages
Folder for servlet and other java classes
![Page 5: Java Servlets Lec 27. Creating a Simple Web Application in Tomcat](https://reader034.vdocuments.site/reader034/viewer/2022051417/56649ee15503460f94bf2602/html5/thumbnails/5.jpg)
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
![Page 6: Java Servlets Lec 27. Creating a Simple Web Application in Tomcat](https://reader034.vdocuments.site/reader034/viewer/2022051417/56649ee15503460f94bf2602/html5/thumbnails/6.jpg)
Servlet class hierarchy
Object
GenericServlet ServletResponse
HttpServletRequest
ServerRequest
HttpServlet HttpServletResponse
javax.servlet
javax.servlet.http
![Page 7: Java Servlets Lec 27. Creating a Simple Web Application in Tomcat](https://reader034.vdocuments.site/reader034/viewer/2022051417/56649ee15503460f94bf2602/html5/thumbnails/7.jpg)
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
![Page 8: Java Servlets Lec 27. Creating a Simple Web Application in Tomcat](https://reader034.vdocuments.site/reader034/viewer/2022051417/56649ee15503460f94bf2602/html5/thumbnails/8.jpg)
Types of HTTP Requests
Get Post Delete Options Put Trace
![Page 9: Java Servlets Lec 27. Creating a Simple Web Application in Tomcat](https://reader034.vdocuments.site/reader034/viewer/2022051417/56649ee15503460f94bf2602/html5/thumbnails/9.jpg)
How HTTP Sends Request
ClientServer
Servlets
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
![Page 10: Java Servlets Lec 27. Creating a Simple Web Application in Tomcat](https://reader034.vdocuments.site/reader034/viewer/2022051417/56649ee15503460f94bf2602/html5/thumbnails/10.jpg)
HTTP Request Example
Request parameters etc
![Page 11: Java Servlets Lec 27. Creating a Simple Web Application in Tomcat](https://reader034.vdocuments.site/reader034/viewer/2022051417/56649ee15503460f94bf2602/html5/thumbnails/11.jpg)
Writing Servlet
Steps for making a HelloWorldServlet
1. Create a directory structure for your application (myapp). This is a one time process for any application
2. Create a HelloWorldServlet source file by extending this class from HttpServlet and override your desired method
3. Compile it (If compiler complains of not having required packages, check your class paths)
![Page 12: Java Servlets Lec 27. Creating a Simple Web Application in Tomcat](https://reader034.vdocuments.site/reader034/viewer/2022051417/56649ee15503460f94bf2602/html5/thumbnails/12.jpg)
Writing Servlet
Steps for making a HelloWorldServlet
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 WEB-INF folder
3. Restart your server, if already running
4. Access your application using Web browser
![Page 13: Java Servlets Lec 27. Creating a Simple Web Application in Tomcat](https://reader034.vdocuments.site/reader034/viewer/2022051417/56649ee15503460f94bf2602/html5/thumbnails/13.jpg)
HelloServlet Example Code
![Page 14: Java Servlets Lec 27. Creating a Simple Web Application in Tomcat](https://reader034.vdocuments.site/reader034/viewer/2022051417/56649ee15503460f94bf2602/html5/thumbnails/14.jpg)
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!”);}
}
![Page 15: Java Servlets Lec 27. Creating a Simple Web Application in Tomcat](https://reader034.vdocuments.site/reader034/viewer/2022051417/56649ee15503460f94bf2602/html5/thumbnails/15.jpg)
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>
![Page 16: Java Servlets Lec 27. Creating a Simple Web Application in Tomcat](https://reader034.vdocuments.site/reader034/viewer/2022051417/56649ee15503460f94bf2602/html5/thumbnails/16.jpg)
Compiling and Invoking Servlets
Compile HelloWorldServlet.java using 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
![Page 17: Java Servlets Lec 27. Creating a Simple Web Application in Tomcat](https://reader034.vdocuments.site/reader034/viewer/2022051417/56649ee15503460f94bf2602/html5/thumbnails/17.jpg)
Lets do it LIVE
![Page 18: Java Servlets Lec 27. Creating a Simple Web Application in Tomcat](https://reader034.vdocuments.site/reader034/viewer/2022051417/56649ee15503460f94bf2602/html5/thumbnails/18.jpg)
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