Server Side Programming
•Server side programming means any code or software that can be
executed on web server.
•Server side techniques provides simplicity as well as multiple
advancements.
•Most popular techniques are Common Gateway Interface (CGI),
Personnel Homepage Processor (PHP), Active Server Pages (ASP),
Servlets and Java Server Pages (JSP).
•Every application on web uses some kind of server-side
programming.
•Eg. Airline reservation, On line banking, e-learning etc.
Advantages of Server Side Programming
•Session Management
•Monitor Activities
•Implement Accessibility
•Programming Flexibility
•Record Data
•Feasibility
•Security
Disadvantages of Server Side Programming
•Can be drastically slow
•Reduced Web Page Appearance
Servlets
•Servlets are server side components written in JAVA. They are
platform independent as they are written in JAVA.
•It is used for developing dynamic web applications.
•The main difference between static and dynamic web page is that
static page as name suggests remains same for all users however a
dynamic web page changes based on the request from client (user’s
browser). For example, consider a web application that shows you
two input fields & an add button and when you enter two numbers
and click add, it shows you another web page that has the result of
addition of two numbers, this web application is dynamic in nature
as the second web page that shows you the result changes based on
the user input, it is not static for all users.
Servlets
•Servlets handle multiple requests coming from the clients
simultaneously.
•Servlets acts as a middle layer between a request coming from a
Web browser and applications running on an Application server.
•For eg. a Servlet might take data entered by the user in HTML order
entry form and apply the business logic to update a company’s order
database.
Functionality of Servlets
•It can manage and store the data submitted by an HTML form.
•It can provide dynamic response to HTTP request, for e.g., it can
return the result of a database query to the client.
•It can read the data entered by the client in a form and look for any
additional information embedded in the HTTP request.
•It can access a database running on an Application server to
prepare a response.
•It can prepare a response to send it back to the client. The response
could be in text form, binary form or a zip file.
Java Servlet Architecture
Java Servlet Architecture
Execution of Servlets basically involves six basic steps:
•The clients send the request to the web server.
•The web server receives the request.
•The web server passes the request to the
corresponding Servlet.
•The Servlet processes the request and generates the
response in the form of output.
•The Servlet sends the response back to the web server.
•The web server sends the response back to the client
and the client browser displays it on the screen.
Classes of Java
Java Servlet Architecture
Life Cycle of Servlet
Servlet life cycle contains five steps:
1) Loading of Servlet
2) Creating instance of Servlet
3) Invoke init() once
4) Invoke service() repeatedly for each client request
5) Invoke destroy()
GET & POST Methods
GET is used to request data from a specified resource.
GET is one of the most common HTTP methods.
Query string (name/value pairs) is sent in the URL of a GET
request.
e. g /test/demo_form.php?name1=value1&name2=value2
GET requests remain in the browser history
This method must not be used if you have a password or
some sensitive information to be sent to the server
GET requests have length restrictions
GET requests are only used to request data (not modify)
It is better for data that is not secure. It cannot be used for
sending images or word documents.
GET & POST Methods
POST is used to send data to a server to create/update a
resource.
E.g: POST /test/demo_form.php HTTP/1.1
Host: facebook.com
name1=value1&name2=value2
POST requests do not remain in the browser history
POST requests have no restrictions on data length
It helps you to securely pass sensitive and confidential
information like login details to server.
POST method takes lots of time when uploading the large binary
file.
This method is a little safer than GET because the
parameters are not stored in browser history.
GET vs. POST Method
GET POST
Data is visible to everyone in the URL Data is not displayed in the URL
GET requests are often used for POST parameters are often used for
fetching documents. updating data for actually making
changes to the server.
GET method supports only string POST method supports different data
data types types, such as string, numeric, binary,
etc.
Get request is not secured because Post request is secured because data
data is exposed in URL bar. Never use is not exposed in URL bar.
GET when sending passwords or
other sensitive information!
Parameters remain in browser history Parameters are not saved in browser
history
Session Handling
Cookies and Sessions are used to store information. Cookies are
only stored on the client-side machine, while sessions get stored on
the client as well as a server.
A session creates a file in a temporary directory on the server
where registered session variables and their values are stored. This
data will be available to all pages on the site during that visit.
A session ends when the user closes the browser or after leaving
the site, the server will terminate the session after a
predetermined period of time, commonly 30 minutes duration.
Session Handling
It can also transfer the information in the form of value from
one web page to another.
The session values are automatically deleted when the
browser is closed.
How a Session Works?
An example of a session is a shopping cart on
online shopping websites. It stores the products
the user has added to their cart. So when the
user opens a new page, the products remain in
the cart. Without session, a user wouldn’t be
able to add multiple items to their cart. Also,
the new pages would not recognize a user’s past
activities and their cart would always be empty.
Cookies
A cookie is a small text file that is stored on the user's
computer. The maximum file size of a cookie is 4KB. It is also
known as web cookie, or internet Cookie.
Whenever a user visits a website for the first time, the site sends
packets of data in the form of a cookie to the user's computer.
The cookies help the websites to keep track of the user's
browsing history information when they visit their sites.
We can enable or disable the cookies as per the requirement.
Cookies are created and shared between the server and browser
with the help of an HTTP header.
Cookies
Cookies are text files stored on the client computer and they
are kept of use tracking purpose. Server sends a set of cookies
to the browser.
For example name, age, or identification number etc. The
browser stores this information on a local machine for future
use.
When next time browser sends any request to web server
then it sends those cookies information to the server and server
uses that information to identify the user.
How does Cookie work?
For e.g. if we visit any website on the internet and our
browser has enabled the cookies for that website then that
website will implant a cookie in the client’s machine which
preserves the data related to the user activity in the user’s
machine.
When we visit YouTube channel and search for some songs,
next time whenever we visit YouTube, cookies read our
browsing history and shows similar songs or last played songs.
Session vs. Cookies
Session Cookies
A session stores the variables and their Cookies are stored on the user's computer
values within a file in a temporary as a text file.
directory on the server.
The session ends when the user logout Cookies end on the lifetime set by the user.
from the application or closes his web
browser.
It can store an unlimited amount of data. It can store only limited data.
Sessions are more secured compared to Cookies are not secure, as data is stored in
cookies, as they save data in encrypted a text file, and if any unauthorized user
form. gets access to our system, he can tamper
the data.
Sessions can store any type of data. Cookies can only store strings.
Java Server Pages (JSP)
Java Server Pages (JSP) is a server side technology for developing
dynamic web pages.
JSP is an extension of servlets and every JSP page first gets converted
into servlet by JSP container before processing the client’s request.
A JSP page consists of HTML tags and JSP tags.
The JSP pages are easier to maintain than Servlet because we can
separate designing and development.
JSP provides some additional features such as Expression Language,
Custom Tags, etc.
The JSP tags which allow java code to be included into it are <% ----
java code----%>.
Java Server Pages (JSP)
This is mainly used for implementing presentation layer (GUI
Part) of an application.
A complete JSP code is more like a HTML with bits of java code
in it.
If JSP page is modified, we don't need to recompile and
redeploy the project. The Servlet code needs to be updated and
recompiled if we have to change the look and feel of the
application.
In JSP, we can use many tags such as action tags, JSTL, custom
tags, etc. that reduces the code.
Java Server Pages (JSP)
JSP takes care of exception handling. Servlet does not take care
of exception handling. It is the responsibility of programmers.
JSP increases readability of code by using tags. In Servlet,
readability of code is less.
JSP is easy to learn and apply. Servlet is comparatively complex
to learn and apply.
The major difference between them is that servlet adds HTML
code inside java while JSP adds java code inside HTML.
Frequently used for designing websites and used by web
developers.