2. 1. In IT term a Server is a computer program that provides services to other
computer programs(or Users ) in the same or other computers.
2. The computer in which server program runs is frequently referred as
Server(though it may be used for other purposes as well).
3. In a client server programming model a server is a program that awaits
and fulfills requests from client programs in the same or other computers.
A given application in a computer may function as a client which requests
for services from other programs.
4. Specific to the web, a web server is a computer program(housed in a
computer) that serves requested HTML pages or files. A web client is the
requesting program associated with the user. The web browser in your
computer is a client that requests HTML files from Web server.
What is Server
3. A web site is a collection of web pages which are stored on a web server and
can be accessed from any where using a web browser. The purpose of a web
site is to share information over the network.
Web sites can be of two types:
Static web sites
Dynamic web sites
In a static web site, the information which is presented to the end user,
remain available on the server usually in the form of HTML pages. When a user
sends a request to a page, contents of the page are sent by the server as
response.
In a dynamic web site, server has programs which are executed when
a request is submitted by clients, these programs process requests and
generate HTML contents which are sent by the server as response to the
client.
What is static and Dynamic Web
Site
4. 1. Client side programming generally refers to the class of programs on the
web that are executed client side by the user’s web browser OR by client
side we refer to code that is executed directly on the device that the user is
using.
2. Upon request, the necessary files are sent to the user’s computer by the web
server (or server) on which they reside.
3. The user’s web browser executes the script and then displays the
document.
4. Client side programming language example include javascript.
disadvantage:
By viewing the file that contains the script users may be able to see the
source code.
What is Client side Side
Programming
5. 1. Server side programming generally refers to the class of programs that are
written to be executed on the Server.
2. A request is generated to execute some program stored on the server.
3. Server in turn serves the request i,e, executes the requested code piece on
server and returns response.
4. Some of the languages used for building Server side application are:
Java, Php, Python, Ruby, etc.
Advantage over Client Side Programming:
The User cannot see the source code and may not be even aware that
a script is executed.
What is Server Side
Programming
6. 1. In early days of the web, server side programming was almost exclusively
performed by using combination of C programs, Perl scripts and Shell
scripts using the common gateway interface(CGI).
2. CGI is a protocol which provides standard rules for server to invoke
programs, to provide requested data to them and to receive the processed
result from them.
Drawbacks of CGI based web applications:
.For each request a process is started by the server to execute CGI script, Process creation and destruction
results in extra overhead than actual processing time.
If number of requests increase, it takes more time for sending response
CGI scripts were platform dependent i,e, if the web application is to be deployed from windows to Linux
server or vice versa CGI script need to be recompiled for the target platform.
History of Server side
Programming
7.
To remove the drawbacks of CGI protocol, Sun microsystem in
1988 introduced the Servlet API.
1. To remove the overhead of process creation and destruction, a
thread based request processing model is provided by servlet.
2. By facilitating the development of web based application in
java, problem of platform dependency is removed
Introduction of Servlets
8. Servlet term is used with two different meanings in two different contexts,
1. In the broader context it represents an API which facilitates development of
dynamic web applications
2. In the narrow context it represents a java class which is defined using this
API for processing requests in a web application.
This component is responsible for processing requests in web
application.
javax.servlet.Servlet is the main interface of the API, it provides
methods which define initialization, processing and destruction phase of a
Servlet. These methods are called servlet life cycle methods.
What is Servlet
9. Various life cycle methods of servlet are discussed below:
1. init(): This method defines Initialization. This method is invoked only once
just afte servlet object is created.
public void init(ServletConfig config);
2. Service(): This method is invoked by the server each time a request is
received for the servlet.It is used by the servlet for processing requests.
public void service(ServletRequest request, ServletResponse
response)throws ServletException, IOException.
3. Destroy():This method is invoked by the server only once just after the
servlet in unloaded. It can be used by the application programmer for
defining cleanup operations.
public void destroy();
Life cycle methods of a Servlet
10. A servlet program can be created by three ways:
i. By implementing a servlet interface.
ii. By inheriting GenericServlet class.
iii. By inheriting HttpServlet class.
Server side Program to check whether a number is Prime or not?
Presentation logic:
<form action=“primeServlet” method=“post”>
<center> Enter Number<Input type=“Text” name=“num”>
<Input type=“submit” value=“check”></center>
</form>
Skeleton of a Servlet program
11.
Business logic: PrimeServlet.java
import javax.Servlet.*;
import javax.Servlet.http.*;
import java.io.*;
class PrimeServlet extends HttpServlet{
public void doPost(HttpServletRequest request, HttpServletResponse
response)throws ServletException IOException
{ response.setContentType(“text/html”) ;
PrintWriter out=response.getWriter();
//request data is read
int p=Integer.parseInt(request.getParameter);
//Business logic is defined
int d=2;
while(num%d!=0)
d++;
if(num==d)
Out.println(“number is prime”);
else
out.println(“number is not prime”);
out.close
}
}
12.
Deployment Descriptor(Web.xml): A web application’s deployment
descriptor describes the classes, resources and configuration of the application
and how the web server uses them to serve web requests. When the web server
receives a request for the application, it uses deployment descriptor to map the
URL of the request to code that ought to handle the request. The deployment
descriptor is a file named web.xml.
Web.xml for our program is as
<web-app>
<servlet>
<servlet-name>s1</servlet-name>
<servlet-class>com.Servlet.myPack.PrimeServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>s1</servlet-name>
<url-pattern>/primeServlet</url-pattern>
</servlet-mapping>
</web-app>
13. 1. Designing in Servlet is difficult and slows down the
application.
2. Writing complex business logic makes the application difficult
to understand.
3. You need a java runtime environment on the server to run
servlet.
4. Developing web application in servlet is unproductive i,e, a
lot of code need to be written even for simple tasks.
Drawbacks of Servlet
14. JSP(Java Server Page) is the extension of servlet which facilitates productive
development of dynamic web application in java.
JSP simplifies the development of dynamic web by facilitating the
embedding of request processing logic into the HTML itself with the help of
special tags.
Each JSP page is automatically translated into servlet by the server at
runtime.
Using JSP we can collect input from users, through web page forms, present
records from a database or another source and create web page
dynamically.
JSP tags can be used for variety of purposes such as retrieving information
from a database or registering user preferences, accessing javabeans
components, passing control between pages and sharing information
between requests, pages etc.
Introduction of JSP
15. 1. JSP increases the productivity of the application programmers
as they are only required to provide the processing logic. All
the repetitive tasks such as defining classes & request
processing methods, and writing static HTML contents to the
output stream, are automated.
2. Maintenance is simplified. Static HTML contents or request
processing logic can be changed on the fly because of the
automated translation.
Advantages of JSP over Servlet
16. Performance is significantly better because JSP allows embedding Dynamic
Elements in HTML Pages itself instead of having a separate CGI files.
JSP are always compiled before it's processed by the server unlike CGI/Perl
which requires the server to load an interpreter and the target script each
time the page is requested.
Java Server Pages are built on top of the Java Servlets API, so like Servlets,
JSP also has access to all the powerful Enterprise Java APIs, including JDBC,
JNDI, EJB, JAXP etc.
JSP pages can be used in combination with servlets that handle the business
logic, the model supported by Java servlet template engines.
Why Jsp?
18. 1. Client first sends request for a JSP page
2. Using a Jsp page a servlet is generated by server component. This servlet
contains the request processing logic of the JSP and auto generated
statement to write the static HTML content to the output stream.
3. The translated servlet is compiled.
4. An object of the generated servlet class is created and initialized.
5. Request processing method _jspService(request,response)is invoked on the
servlet object.
6. Contents dynamically generated by _jspService(request,response)method
are sent as response to the client.
Working of JSP
19. Jsp life cycle is defined by JspPage and HttpJspPage interfaces
Javax.servlet.jsp package contains classes and interfaces of JSP API.
Main classes and interfaces of JSP API are:
1. JspPage:It extends Servlet interface and adds following JSP life cycle
methods:
jspInit():This method is provided to define initialization operations of
JSP. Syntax:
public void jspInit();
jspDestroy(): This method is provided to define clean up operations
of the jsp. Syntax:
public void JspDestroy();
2. HttpJspPage: It extends Jsp page interface and provides following life
cycle method:
JSP Life Cycle
20.
3. _jspService(): This method is provided to define request
processing logic of the JSP. Syntax
public void _jspService(HttpServleRequest request,
HttpServletResponse response )throws ServletException,
IOException;
21. Jsp supports following types of tags:
1. Scriptlet.
2. Declaration.
3. Expression.
4. Directives.
5. Actions
Tags supported by Jsp
22.
1. Scriptlet is the main tag of JSP. It is used to add request processing logic to
a JSP page. A JSP page may contain any number of scriptlet tags.Syntax
<%requestprocessing logic%>
At the time of translation scriptlet contents are used by the server for defining
body of _JspService method.
Within Scriptlet following implicit objects are made available to a Jsp
programmer.
Out JspWriter.
Request HttpServletRequest object which contains
request parameters and attributes
Response Is HttpServletResponse object.
Config Is ServletConfig object of the servlet.
Session Is HttpSession object of the user.
Application Is the ServletContext object of the application
Page is the current servlet object of the jsp.
pageContext Is an object of type PageContext. It acts as a container of a
all the other servlet and JSP API objects which participate
request processing.
Exception is an optional object which is available only when exc. ocr
23.
An Example demonstrating use of Scriptlet tag:
<form method="post" action="adder.jsp">
First No: <input type="text" name="num1"><br/>
Second No: <input type="text" name="num2"><br/>
<input type="submit" value="add"> </form>
<% int a=Integer.parseInt(request.getParameter("num1"));
int b=Integer.parseInt(request.getParameter("num2"));
int c=a+b;
out.println("sum is: "+c); %>
with this example we have understood the simplication provided, Here
no class or method is defined, no object is declared, no content type is set, no
web.xml file is required
24.
2. Declaration tag in Jsp facilitate data members and method definition in the
auto generated servlet.. Main use of this tag is the overriding of jspInit() and
jspDestroy() methods.
<%! Data members and method definition%>
An example demonstrating the use of Declaration tag
<form method="post" action="hello.jsp">
Name <input type="text" name="name"><br/>
<input type="submit" value="submit">
</form>
<%!
private String userName;
Public String sayHello(){
Return “Hello”+userName;
}
Public void jspInit()
{
System.out.println(“hello.jsp is initialized”);
}
%>
25.
3. Expression tag in jsp has two uses:
1. It is used to write the value of an expression to the output
stream.
2. It is used to assign the value of variables and expression to the
attributes of HTML elements.
syntax: <%=variable or expression%>
4. Action Tags in Jsp facilitate automation of common operations
such as creation of objects, setting object’s properties, writing
object’s property values to the output stream, including the
contents of a component to the reponse of current request and
forwarding request to another component etc. An action tag has
following syntax:
<jsp:actionName attribute=value…/>
26.
Action Tags in Jsp facilitates automation of common operations such as
creation of objects, setting object’s properties, writing object properties, writing
objects’s property value to the output stream, including contents of a
component to the response of current request and forwarding request to
another component etc.
Commonly used action tags of Jsp:
<jsp:include />: used to include the contents of specified page to the response
of current request.
<jsp:forward />: used to forward request to the specified page.
<jsp:useBean/>: used to create a Bean Object and save it in a scope.
<jsp:setProperty/>: used to set property of java Bean object.
<jsp:getProperty/>:used to write the value of java Bean object to the output
stream.
<jsp:param/>: it is used to define parameters to be provided to the included or
forwarded page.
27. A directive in JSP, represents an instruction to the translator to modify the
structure of the auto generated servlet at the time of translation on behalf of the
programmer. As we know for each JSP, a servlet class is autogenerate.
Sometimes modifications are required in this servlet e,g. Some extra packages
need to be imported in it or it need to be inherited from a use defined class, or
exception need to be managed in some specific way.Syntax:
%@directiveName attribute=“value..”%
Information to the translator is provided with the help of attributes.
JSP suports following three directives:
Page
Include and
taglib
Directives
28.
Include directive in JSP, is used by application programmers to
get the contents of a page included to the current JSP at the
time of translation.
<%@include file=“url of the component to be included ”%>
JSP page directive is used by the programmer to get the
structure of the auto generated servlet modified according to
their requirements.
<%@page attributeName=“value”%>
commonly used attributes:
import <%@page import=“packageName”%>
Extends <%page extends=“className”%>
ContentType<%@page contentType=“MIMEType”%>
Session <%@page session=“false”%>
isErrorPage<%@page isErrorPage=“true”%>
errorPage<%page errorPage=“URL of errorHandlerPage”%>