Assignment No:5: WAP To Execute The Servlet Life Cycle Methods

Download as ppt, pdf, or txt
Download as ppt, pdf, or txt
You are on page 1of 26

Assignment No:5

WAP to execute the Servlet life


cycle methods
Session Plan

 Servlet Fundamentals
 Basic Servlet Structure

 A Servlet’s Life Cycle

 Writing Servlet Hello World

 Installation, Configuration and running


Servlets
 A Servlet Program

 Output Screens
2
Servlet
 Servlets are server side Java programs which extends the
functionality of web server
 Servlet are protocol independent that means it can be used
virtually with any protocol to process the request and
generate the response
 However in practice Servlets are used to process the HTTP
requests and generate the HTML response
Servlet Fundamentals
 Java Servlet is a server side web application service
technology
 They are loaded by a servlet server such as Tomcat, Jetty,
BEA's WebLogic which maps them to some location on the
web server's web space
Basic Servlet Structure
 As seen earlier, Java servlets are server side programs or to
be more specific; web applications that run on servers that
comply HTTP protocol
 The javax.servlet and javax.servlet.http packages provide
the necessary interfaces and classes to work with Servlets
 Servlets generally extend the HttpServlet class and
override the doGet or the doPost methods
 Servlets are Java objects that implement the
javax.servlet.Servlet interface
 In addition, other methods such as init, service and destroy
also called as life cycle methods
The skeleton of a Servlet is given in Figure
A Servlet’s Life Cycle
A Servlet’s Life Cycle
 The first time a Servlet is invoked, it is the init method
which is called. This is called only once during the lifetime
of a Servlet. So, can put all initialization code here. This
method next calls the service method
 The service method in turn calls the doGet or doPost
methods (whichever the user has overridden)
 Finally, the Servlet calls the destroy method. This is used to
reset or close references / connections done earlier in the
servlet’s methods (e.g. init, service or doGet /doPost). After
this method is called, the Servlet ceases to exist for all
practical purposes
 However, please note that it is not mandatory to override all
these methods. More often than not, it is the doGet or
doPost method used with one or more of the other life cycle
methods
Difference between GET and POST
request methods
 GET request sends the request parameters as query string
appended at the end of the request URL, whereas POST
request sends the request parameters as part of the HTTP
request body
 Since GET request sends the parameters as query string,
parameters are displayed in browser address bar. User can
bookmark the URL. Depending on how sensitive request
parameters are, it may be a security issue
 One another issue with GET request is, some servers limits
the length of the URL to 240 characters, so if the too many
parameters are appended in query string and URL exceeds
this length, some web servers might not accept the request.
These restrictions don’t apply to POST method as it sends
the parameters as request body. POST requests cannot be
bookmarked
The javax.servlet package

 To the developers, it provides the library to develop the


Servlet based applications
Servlet HelloWorld Program
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;

public class HelloWorld extends HttpServlet


{
public void doGet(HttpServletRequest request, HttpServletResponse
response) throws ServletException,IOException
{
response.setContentType("text/html");
PrintWriter pw = response.getWriter();
pw.println("<html>");
pw.println("<head><title>Hello World</title></title>");
pw.println("<body>");
pw.println("<h1>Hello World</h1>");
pw.println("</body></html>");
}
}
Installation, Configuration and running
Servlets
1. Installation of Tomcat Server and JDK
2. Configuring Tomcat Server
Variable Name
 JAVA_HOME:C:\Program Files\Java\jdk1.6.0

System Variable
 CLASSPATH:C:\Program Files\Apache Software
Foundation\Tomcat 6.0\lib\servlet-api.jar
Installation, Configuration and running
Servlets
3. Run Tomcat Server

Step 1
• Go to C:\Program Files\Apache Software Foundation\Tomcat
6.0\bin and double click on tomcat6
OR
• Go to Start->Programs->Apache Tomcat 6.0 -> Monitor
Tomcat. You will notice an icon appear on the right side of
your Status Bar.   Right click on this icon and click on Start
service
Installation, Configuration and running
Servlets
3. Run Tomcat Server
Step 2

• Open your Browser (e.g. MS Internet Explorer) and type the


following URL :http://localhost/ (If you have changed to port # to 80)

OR

• Open your Browser (e.g. MS Internet Explorer) and type the


following URL :

http://localhost:8080/ (If you have NOT changed the default port #)

In either case, you should get a same page that the Tomcat Server
is successfully running on your machine.
Installation, Configuration and running
Servlets
4. Compile and Execute Servlet program
[A] Compile your Servlet program

 The first step is to compile your Servlet program


 The procedure is no different from that of writing and compiling a
java program
 But, the point to be noted is that neither the javax.servlet.* nor the
javax.servlet.http.* is part of the standard JDK
 It has to be exclusively added in the CLASSPATH
 The set of classes required for writing Servlets is available in a jar
file called servlet-api.jar
 Include the following path in CLASSPATH Variable
CLASSPATH:- C:\Program Files\Apache Software
Foundation\Tomcat 6.0\lib\servlet-api.jar
Installation, Configuration and running
Servlets
4. Compile and Execute Servlet program
[B] Create your Web application folder

 The next step is to create your web


application folder
 The name of the folder can be any valid
and logical name that represents your
application
(e.g. bank_apps, airline_tickets_booking,
shopping_cart,etc)
 But the most important criterion is that
this folder should be created under
webapps folder
 The path would be similar or close to
this - C:\Program Files\Apache Software
Foundation\Tomcat 6.0\webapps
 For demo purpose, let us create a folder
called demo-examples under the
webapps folder  Fig: Depicts the same
Installation, Configuration and running
Servlets
4. Compile and Execute Servlet program

[C] Create the WEB-INF folder

 The third step is to create


the WEB-INF folder.
 This folder should be created
under your web application
folder that you created in
the previous step.
 Figure shows the WEB-INF Fig: WEB-INF folder inside web application folder

folder being placed under


the demo-examples folder
Installation, Configuration and
running Servlets
4. Compile and Execute Servlet program

[D] Create the web.xml file and the classes folder

 The fourth step is to create


the web.xml file and the
classes folder
 Ensure that the web.xml and
classes folder are created
under the WEB-INF folder
 Fig shows this file and
folder being placed under
the WEB-INF folder Fig: web.xml file and the classes folder
Installation, Configuration and
running Servlets
4. Compile and Execute Servlet program

[E] Copy the Servlet class to the classes folder

 We need to copy the Servlet


class file to the classes folder
in order to run the Servlet that
we created
 All you need to do is copy the
Servlet class file
(the file we obtained from Step 1)
to this folder
 Fig shows the HelloWorld Fig: Servlet class file placed under classes folder
class being placed in the
classes folder
Installation, Configuration and
running Servlets
4. Compile and Execute
Servlet program

[F] Edit web.xml to include


Servlet’s name and url pattern

 This step involves two


actions viz. including the
Servlet’s name and then
mentioning the url pattern
 First include the Servlet’s
name in the web.xml file.
For this open the web.xml
file and include the Fig: Include servlet’s name using the <servlet> </servlet>
Servlet’s name as shown tag
in Fig
Note – The servlet-name need not be the same as that of the
class name. You can give a different name (or alias) to the
actual Servlet. This is one of the main reasons as why this
tag is used for.
Installation, Configuration and
running Servlets
4. Compile and Execute
Servlet program

[G] Run Tomcat server and then


execute your Servlet

 This step again involves two actions


viz. running the Web Server and
then executing the servlet.
 Run the server & ensures that the
web server is running successfully
then run your servlet program.
 To do this, open the web browser
and enter the url as specified in the
web.xml file.
 The complete url that needs to be
entered in the browser is: Next, include the url pattern using the <servlet-
mapping> </servlet-mapping> tag. The url pattern
http://localhost:8080/demo-examples/ defines as how a user can access the servlet from the
HelloWorld browser.

Fig shows the url pattern entry for our current


servlet.
Servlet’s output

Here’s the output of first Servlet

Fig: Servlet’s output!


Show Servlet lifecycle methods
Servlet’s output
As mentioned we can keep refreshing the browser
window and see as how i value is incremented.
(a proof that the doGet is called every time you re-
invoke a servlet)
Fig: Servlet’s output!

You might also like