0% found this document useful (0 votes)
98 views20 pages

Maharana Pratap College of Technology &management: A Seminar On Java Servlet & Applet

This document summarizes key concepts about Java servlets and applets. It defines servlets as server-side Java components that extend the functionality of web servers to power dynamic web applications. It describes the lifecycle methods of servlets including init(), service(), and destroy(). It also outlines the advantages of using servlets and provides a sample servlet program. The document then defines applets as Java programs embedded in web pages that run in web browsers. It lists the lifecycle methods of applets and provides a simple applet example to draw text on the screen.

Uploaded by

Pushp Pallavi
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
98 views20 pages

Maharana Pratap College of Technology &management: A Seminar On Java Servlet & Applet

This document summarizes key concepts about Java servlets and applets. It defines servlets as server-side Java components that extend the functionality of web servers to power dynamic web applications. It describes the lifecycle methods of servlets including init(), service(), and destroy(). It also outlines the advantages of using servlets and provides a sample servlet program. The document then defines applets as Java programs embedded in web pages that run in web browsers. It lists the lifecycle methods of applets and provides a simple applet example to draw text on the screen.

Uploaded by

Pushp Pallavi
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 20

Maharana Pratap College Of Technology &Management

A Seminar On Java Servlet & Applet

Submitted To: Mrs.Madhu Sharma Dept. of CS/IT

Submitted By: Aashish Jain (0903it081001)

Introduction:
Servlets are server side components that provide a powerful mechanism for developing server side programs. Servlets provide component-based, platform-independent methods for building Webbased applications. servlets are server as well as platform-independent. Servlets run entirely inside the Java Virtual Machine. Since the Servlet runs at server side so it does not checks the browser for compatibility. Servlets can access the entire family of Java APIs, including the JDBC API to access enterprise databases

Servlets are not designed for a specific protocols. It is different thing that they are most commonly used with the HTTP protocols. 1.Servlets uses the classes in the java packages javax.servlet and javax.servlet.http. Servlets provides a way of creating the sophisticated server side extensions in a server as they follow the standard framework and use the highly portable java language. A servlet is a Java programming language class used to extend the capabilities of serversthat host applications access via a request-response programming model. Although servlets can respond to any type of request, they are commonly used to extend the applications hosted by Web servers

init(): The init() method is called only once by the servlet container throughout the life of a servlet. By this init() method the servlet get to know that it has been placed into service. service(): Once the servlet starts getting the requests, the service() method is called by the servlet container to respond. The servlet services the client's request with the help of two objects. These two objectsjavax.servlet.ServletRequest and javax. servlet.ServletResponse are passed by the servlet container.

Methods of Servlets

destroy(): This method is called when we need to close the servlet. That is before removing a servlet instance from service, the servlet container calls the destroy() method. Once the servlet container calls the destroy() method, no service methods will be then called . That is after the exit of all the threads running in the servlet, the destroy() method is called. Hence, the servlet gets a chance to clean up all the resources like memory, threads etc which are being held.

Life cycle of Servlet


Loading and Inatantiation: The servlet container loads the servlet during startup or when the first request is made. The loading of the servlet depends on the attribute <load-on-startup> of web.xml file. If the attribute <load-on-startup> has a positive value then the servlet is load with loading of the container otherwise it load when the first request comes for service. Initialization: After creating the instances, the servlet container calls the init() method and passes the servlet initialization parameters to the init() method. The init() must be called by the servlet container before the servlet can service any request.

Servicing the Request:After successfully completing the initialization process, the servlet will be available for service. Servlet creates seperate threads for each request. The sevlet container calls the service() method for servicing any request. The service() method determines the kind of request and calls the appropriate method (doGet() or doPost()) for handling the request and sends response to the client using the methods of the response object. Destroying the Servlet:If the servlet is no longer needed for servicing any request, the servlet container calls the destroy() method

1.garbage collection.

Advantages of Java Servlets


Portability Powerful Efficiency Safety Integration Extensibilty Inexpensive

To Run A Servlet Program


Download and Install the tomcat server: Install the tomcat server in a directory in which you want to install and set the classpath.for the variable JAVA_HOME in the environment variable. Set the class for the jar file: Set the classpath of the servlet-api.jar file in the For Windows XP, Go to Start->Control Panel->System>Advanced->Environment Variables->New button and Set the values as Variable Name: CLASSPATH Variable Value: C:\Program Files\Java\Tomcat 6.0\lib\servlet-api.jar

Create a java source file Compile the java source file, put the compiled file (.class file) in the classes folder of your application and deploy the directory of your application in the webapps folder inside the tomcat directory. Start the tomcat server, open a browser window and type the URL http://localhost:8080/directory (folder name of your application) name/servlet nameand press enter.

IT is very simple Servlet program import javax.servlet.*; import javax.servlet.http.*; import java.io.*; public class welcome extends HttpServlet { public void doGet(HttpServletRequest req,HttpServletResponse res)throws IOException,ServletException { res.setContentType("text/html"); PrintWriter out=res.getWriter(); String yn=req.getParameter("yn"); out.println("<h1>welcome"+yn+"</h1>"); out.println("</html>"); out.flush(); } }

Applets
Introduction:

Applet is java program that can be embedded into HTML pages. Java applets runs on the java enables web browsers such as mozila and internet explorer. Applet is designed to run remotely on the client browser, so there are some restrictions on it. Applet can't access system resources on the local computer. Applets are used to make the web site more dynamic and entertaining.

Advantages of Applet
Applets are cross platform and can run on Windows, Mac OS and Linux platform Applets can work all the version of Java Plugin Applets runs in a sandbox, so the user does not need to trust the code, so it can work without security approval Applets are supported by most web browsers Applets are cached in most web browsers, so will be quick to load when returning to a web page User can also have full access to the machine if user allows

Lifecycle methods of an Applet


init(): This method is called to initialized an applet start(): This method is called after the initialization of the applet. stop(): This method can be called multiple times in the life cycle of an Applet. destroy(): This method is called only once in the life cycle of the applet when applet is destroyed.

The simplest reasonable applet


import java.awt.*; import java.applet.Applet; public class HelloWorld extends Applet { public void paint( Graphics g ) { g.drawString( "Hello World!", 30, 30 ); } }

Applet methods
public public public public public Also: public public public public void init () void start () void stop () void destroy () void paint (Graphics) void repaint() void update (Graphics) void showStatus(String) String getParameter(String)

Methods are called in this order


init() start()
do some work

stop() destroy()

init and destroy are only called once each start and stop are called whenever the browser enters and leaves the page do some work is code called by your listeners paint is called when the applet needs to be repainted

Sample Graphics methods


A Graphics is something you can paint on
g.drawString(Hello, 20, 20); g.drawRect(x, y, width, height); g.fillRect(x, y, width, height); g.drawOval(x, y, width, height); g.fillOval(x, y, width, height); g.setColor(Color.red);

Hello

You might also like