Servlet

25
Servlet Mr. Yuba Raj Kalathoki BScIT 4 th LBEF Campus, Kathmandu Nepal.

description

Simple and easy way to start JAVA servlet application .The main objective of this presentation is understand about servlet, configure tomcat server on the machine, run servlet application and understand the follow of servlet request and response....

Transcript of Servlet

Page 1: Servlet

Servlet

Mr. Yuba Raj Kalathoki BScIT 4th LBEF Campus,

Kathmandu Nepal.

E-Mail : [email protected]

Page 2: Servlet

Introduction• Servlet is a web component managed by a Web container, which generates

dynamic content in Web pages.

• A Servlet is a Java application that runs in a Web server or an application server and provides server-side processing such as accessing a database.

• Servlets are protocol and platform independent server side components, written in Java, which dynamically extend Java-enabled Web Servers.

• Servlet, in simple terms, is a Java program running under a Web server taking a ‘request’ object as an input and responding back by a ‘response’ object.

• A Java Servlet is an application, which runs in a Servlet container using a framework for servicing data requests.

Page 3: Servlet

Installation, Configuration and Execution of a Servlet

• Step 1: Download the Java Software from: http://java.sun.com/javases/download/index.jsp and install in a preferred drive of your hard disk. Example: D:\Java\jdk1.7.0_03\ and D:\Java\jre7\

• Step 2: Download the apache tomcat web server software from: http://tomcat.apache.org/download/ and install in a preferred drive of your hard disk. Example: D:\tomcat7\

• Step 3: Configure the following environmental variables:

a) PATH: D:\Java\jdk1.7.0_03\bin

b) CLASSPATH: .;D:\tomcat7\lib\servlet-api.jar;

c) JAVA_HOME: D:\Java\jdk1.7.0_03

d) JRE_HOME: D:\Java\jre7

e) CATALINA_HOME: D:\tomcat7\

Page 4: Servlet

Steps to configure environmental variables

1. On the Desktop, Right-Click on Computer Icon as shown in figure. Now Click on the Properties tab on the pop up window.

Page 5: Servlet

Steps to configure environmental variables Continue…..

2. After clicking the Properties window, then the following window will be displayed.

Page 6: Servlet

Steps to configure environmental variables Continue…..

3. Now select the Advanced System Setting tab of the window which was shown in Step:2 then following window would be displayed and select Advanced Tab of the window.

Page 7: Servlet

Steps to configure environmental variables Continue…..

4. Now click on the Environment Variables button at the bottom of the window which is shown in Step 3 and the following window will be displayed. Then select path variable from System variables menu which is showing in flowing figure.

Page 8: Servlet

Steps to configure environmental variables Continue…..

5. Now click on the Edit button at bottom of the window which is shown in Step 4 and the following window will be displayed. Then leave Variable name field and set Variable value: D:\Java\jdk1.7.0_03\bin then click Ok button.

Page 9: Servlet

Steps to configure environmental variables Continue…..

6. Now, go User variables for Kalathoki menu (Kalathoki is Username) which is showing in following figure.

Page 10: Servlet

Steps to configure environmental variables Continue…..

7. Now click New button which is showing in Step 6 and set Variable name: CLASSPATH and Variable value: .;D:\tomcat7\lib\servlet-api.jar; {You should set dot semicolon(.;) before set the value of variable.} which is showing in following figure. Then click Ok button and set variable name JAVA_HOME, JRE_HOME, CATALINA_HOME and its value D:\Java\jdk1.7.0_03 , D:\Java\jre7 , D:\tomcat7 respectively.

Page 11: Servlet

How to run Apache Tomcat server ?

1. Open Windows Command Prompt (To open command prompt click start button which is in left corner of your desktop window like showing in fig.1)

then type cmd and press Enter from your keyboard and following window will be displayed.

Page 12: Servlet

How to run Apache Tomcat server ? Continue…..

2. a) Type d: then press enter,

b) Type cd tomcat7\bin then press enter. Example: Showing in following figure.

c) Then type startup.bat or only startup then press enter. And following window will be displayed showing the INFO: server startup in … ms.

Page 13: Servlet

How to run Apache Tomcat server ? Continue…..

3. Open your browser and type on search bar http://localhost:8080 and press enter then you will see following window. If following window displayed then your tomcat configuration is successfully completed.

Page 14: Servlet

Fundamental parts of a Servlet1. import javax.servlet.*; and import javax.servlet.http.*;

- packages of servlet classes that implement the Java Servlet API2. public class HelloWorld extends HttpServlet {

- extends the HTTPServlet class3. init()

-intializes servlet after loading into memory- place to perform operations done only once at start-up

- reading a current properties - clearing log files, notifying other services that the servlet is

running4. service(), doGet(), doPost()

- this is where work is done- each time the servlet is called a new thread of execution begins- input is passed to the Java code via either HTTP GET or POST commands

5. destoy()- executed when server engine calls servlet to terminate- used to flush I/O, disconnect from database

Page 15: Servlet

Structure ofa Servlet:

HelloWorld

import java.io.*;import javax.servlet.*;import javax.servlet.http.*;

public class HelloWord extends HttpServlet { public void doGet(HttpServletRequest request,

HttpServletResponse response) throws IOException, ServletException { response.setContentType("text/html"); PrintWriter out = response.getWriter(); out.println("<html>");

out.println("<head>"); out.println("<title>Hello Word!</title>"); out.println("</head>"); out.println("<body>"); out.println("<h1>Hello Word!</h1>"); out.println("</body>"); out.println("</html>"); }}

HelloWord.java

Page 16: Servlet

<web-app><servlet>

<servlet-name>test</servlet-name><servlet-class>HelloWord</servlet-class>

</servlet><servlet-mapping> <servlet-name>test</servlet-name> <url-pattern>/helloword</url-pattern></servlet-mapping> </web-app>

Web.xml

Page 17: Servlet

Site Map To Store Files

Page 18: Servlet

OutPut

Type URL on the browser’s search bar http://localhost:8080/kalathoki/helloword and press enter then you will get output as: following figure:

Protocol

Server

Port No

Project Name Url-pattern which plays main role to find correct

servlet program

Page 19: Servlet

Working mechanism to run Servlet program

Request

URL

Web Server Machine

Client

WebServer

App(e.g.

Apache)

Web Container

App(e.g.

Tomcat)

Servlet

Page 20: Servlet

Web Server Machine

Client

WebServer

App(e.g.

Apache)

Web Container

App(e.g.

Tomcat)

Servlet

Working mechanism to run Servlet program Continue…

Request

URL

Apache forwards HTTP request to Tomcat Using AJP protocol

Page 21: Servlet

Web Server Machine

Client

WebServer

App(e.g.

Apache)

Web Container

App(e.g.

Tomcat)

Servlet

Working mechanism to run Servlet program Continue…

Request

Tomcat uses the mapping info in a configuration file called web.xml of the web application to find the right servlet

Page 22: Servlet

Web Server Machine

Client

WebServer

App(e.g.

Apache)

Web Container

App(e.g.

Tomcat)

Servlet

Working mechanism to run Servlet program Continue…

Response

Tomcat uses the mapping info in a configuration file called web.xml of the web application to find the right servlet

Page 23: Servlet

Web Server Machine

Client

WebServer

App(e.g.

Apache)

Web Container

App(e.g.

Tomcat)

Servlet

Working mechanism to run Servlet program Continue…

Response

Page 24: Servlet

Web Server Machine

Client

WebServer

App(e.g.

Apache)

Web Container

App(e.g.

Tomcat)

Servlet

Working mechanism to run Servlet program Continue…

Response

Page 25: Servlet

Thank You !