Maharana Pratap College of Technology &management: A Seminar On Java Servlet & Applet
Maharana Pratap College of Technology &management: A Seminar On Java Servlet & Applet
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.
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.
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
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)
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
Hello