Download presentation
Presentation is loading. Please wait.
1
Server Side Programming
Java servlets are server-side programs (running inside a web server) that handle clients' requests and return a customized or dynamic response for each request. ... Java servlets typically run on the HTTP protocol. HTTP is an asymmetrical request-response protocol. A bank having 10,000 customers would mean that each customer should have a copy of the program(s) in his or her PC which translates to 10,000 programs! In addition, there are issues like security, resource pooling, concurrent access and manipulations to the database which simply cannot be handled by client side programs.
2
Advantages of Server Side Programs
All programs reside in one machine called the Server. Any number of remote machines (called clients) can access the server programs. New functionalities to existing programs can be added at the server side which the clients can advantage without having to change anything from their side. Migrating to newer versions, architectures, design patterns, adding patches, switching to new databases can be done at the server side without having to bother about clients hardware or software capabilities. Issues relating to enterprise applications like resource management, concurrency, session management, security and performance are managed by service side applications. They are portable and possess the capability to generate dynamic and user-based content (e.g. displaying transaction information of credit card or debit card depending on users choice).
3
Types of Server Side Programs
Active Server Pages (ASP) Java Servlets Java Server Pages (JSPs) Enterprise Java Beans (EJBs) PHP The objective of server side programs is to centrally manage all programs relating to a particular application (e.g. Banking, Insurance, e-shopping, etc). Clients with bare minimum requirement (e.g. Pentium II, Windows XP Professional, MS Internet Explorer and an internet connection) can experience the power and performance of a Server (e.g. IBM Mainframe, Unix Server, etc) from a remote location without having to compromise on security or speed. More importantly, server programs are not only portable but also possess the capability to generate dynamic responses based on users request.
4
JNI(Java Native Interface)
The Java Native Interface (JNI) is a programming framework that enables Java code running in a Java Virtual Machine (JVM) to call and be called by native applications (programs specific to a hardware and operating system platform) and libraries written in other languages such as C, C++ and assembly. At times, it is necessary to use native codes (C/C++) to overcome the memory management and performance constraints in Java. Java supports native codes via the Java Native Interface (JNI).JNI is difficult, as it involves two languages and runtimes.
5
The main() method allocate an instance of HelloJNI and invoke the native method sayHello().
Compile the "HelloJNI.java" into "HelloJNI.class". > javac HelloJNI.java
6
The static initializer invokes System
The static initializer invokes System.loadLibrary() to load the native library "Hello" (which contains the native method sayHello()) during the class loading. It will be mapped to "hello.dll" in Windows; or "libhello.so" in Unixes. This library shall be included in Java's library path (kept in Java system variable java.library.path); otherwise, the program will throw a UnsatisfiedLinkError. You could include the library into Java Library's path via VM argument -Djava.library.path=path_to_lib. Next, we declare the method sayHello() as a native instance method, via keyword native, which denotes that this method is implemented in another language. A native method does not contain a body. The sayHello() is contained in the native library loaded. The main() method allocate an instance of HelloJNI and invoke the native method sayHello(). Compile the "HelloJNI.java" into "HelloJNI.class".
8
Step 4: Run the Java Program
> java HelloJNI or > java -Djava.library.path=. HelloJNI
9
Servlets A Java servlet is a Java program that extends the capabilities of a server. Although servlets can respond to any types of requests, they most commonly implement applications hosted on Web servers. Such Web servlets are the Java counterpart to other dynamic Web content technologies such as PHP and ASP.NET. Servlet can be described in many ways, depending on the context. Servlet is a technology i.e. used to create web application. Servlet is an API that provides many interfaces and classes including documentations. Servlet is an interface that must be implemented for creating any servlet. Servlet is a class that extend the capabilities of the servers and respond to the incoming request. It can respond to any type of requests. Servlet is a web component that is deployed on the server to create dynamic web page. There are many interfaces and classes in the servlet API such as Servlet, GenericServlet, HttpServlet, ServletRequest, ServletResponse etc.
12
Servlets A web application is an application accessible from the web. A web application is composed of web components like Servlet, JSP, Filter etc. and other components such as HTML. The web components typically execute in Web Server and respond to HTTP request.
13
better performance: because it creates a thread for each request not process.
Portability: because it uses java language. Robust: Servlets are managed by JVM so we don't need to worry about memory leak, garbage collection etc. Secure: because it uses java language..
14
DemoServer.java import javax.servlet.http.*; import javax.servlet.*; import java.io.*; public class DemoServ extends HttpServlet{ public void doGet(HttpServletRequest req,HttpServletResponse res)throws ServletException,IOException { res.setContentType("text/html"); PrintWriter pw=res.getWriter(); String name=req.getParameter("name"); pw.println("Welcome "+name); }}
15
<html><body> <form action="welcome" method="get"> Enter your name<input type="text" name="name"><br> <input type="submit" value="login"> <web-app> <servlet> <servlet-name>sonoojaiswal</servlet-name> <servlet-class>DemoServ</servlet-class> </servlet> <servlet-mapping> <url-pattern>/welcome</url-pattern> </servlet-mapping> </web-app>
16
Life Cycle of a Servlet (Servlet Life Cycle)
The web container maintains the life cycle of a servlet instance. The life cycle of the servlet: Servlet class is loaded. Servlet instance is created. init method is invoked. service method is invoked. destroy method is invoked.
18
1) Servlet class is loaded
The classloader is responsible to load the servlet class. The servlet class is loaded when the first request for the servlet is received by the web container. 2) Servlet instance is created The web container creates the instance of a servlet after loading the servlet class. The servlet instance is created only once in the servlet life cycle. 3) init method is invoked The web container calls the init method only once after creating the servlet instance. The init method is used to initialize the servlet. It is the life cycle method of the javax.servlet.Servlet interface. Syntax of the init method is given below: public void init(ServletConfig config) throws ServletException 4) service method is invoked The web container calls the service method each time when request for the servlet is received. If servlet is not initialized, it follows the first three steps as described above then calls the service method. If servlet is initialized, it calls the service method. Notice that servlet is initialized only once. The syntax of the service method of the Servlet interface is given below: public void service(ServletRequest request, ServletResponse response) throws ServletException, IOException
19
5) destroy method is invoked
The web container calls the destroy method before removing the servlet instance from the service. It gives the servlet an opportunity to clean up any resource for example memory, thread etc. The syntax of the destroy method of the Servlet interface is given below: public void destroy()
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.