Anda di halaman 1dari 25

Servlet

Mr. Yuba Raj Kalathoki


BScIT 4th LBEF Campus, Kathmandu Nepal. E-Mail : yrkt09@yahoo.com

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.

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\

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.

Steps to configure environmental variables


Continue..

2.

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

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.

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.

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.

Steps to configure environmental variables


Continue..

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

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\servletapi.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.

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.

How to run Apache Tomcat server ?


2.

Continue..

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.

How to run Apache Tomcat server ?


3.

Continue..

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.

Fundamental parts of a Servlet


1. import javax.servlet.*; and import javax.servlet.http.*; - packages of servlet classes that implement the Java Servlet API 2. public class HelloWorld extends HttpServlet { - extends the HTTPServlet class 3. 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 running 4. 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

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

HelloWord.java

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>"); } }

Structure of a Servlet: HelloWorld

Web.xml

<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>

Site Map To Store Files

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

Port No
Protocol

Server

Project Name

Url-pattern which plays main role to find correct servlet program

Working mechanism to run Servlet program

URL
Request

Client

Web Server App (e.g. Apache)

Web Container App (e.g. Tomcat)

Web Server Machine Servlet

Working mechanism to run Servlet program


Continue

Apache forwards HTTP request to Tomcat Using AJP protocol

URL
Request

Client

Web Server App (e.g. Apache)

Web Container App (e.g. Tomcat)

Web Server Machine Servlet

Working mechanism to run Servlet program


Continue

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

Request

Client

Web Server App (e.g. Apache)

Web Container App (e.g. Tomcat)

Web Server Machine Servlet

Working mechanism to run Servlet program


Continue

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

Client

Web Server App (e.g. Apache)

Web Container App (e.g. Tomcat)

Web Server Machine Servlet


Response

Working mechanism to run Servlet program


Continue

Client

Web Server App (e.g. Apache)

Web Container App (e.g. Tomcat)


Response

Web Server Machine Servlet

Working mechanism to run Servlet program


Continue

Client

Web Server App (e.g. Apache)


Response

Web Container App (e.g. Tomcat)

Web Server Machine Servlet

Thank You !

Anda mungkin juga menyukai