0% found this document useful (0 votes)
32 views63 pages

Servlets

The document discusses backend development using Java servlets. It begins by explaining the difference between GET and POST HTTP methods. It then compares CGI and servlets, noting advantages of servlets like being thread-based, written in Java, and able to access implicit and explicit data. The document defines servlets as Java programs that handle requests and send responses. It explains the servlet processing lifecycle including initialization, service, and destruction phases. Finally, it provides instructions on installing Apache Tomcat server and Eclipse IDE and creating a simple servlet program.

Uploaded by

relacin216
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
32 views63 pages

Servlets

The document discusses backend development using Java servlets. It begins by explaining the difference between GET and POST HTTP methods. It then compares CGI and servlets, noting advantages of servlets like being thread-based, written in Java, and able to access implicit and explicit data. The document defines servlets as Java programs that handle requests and send responses. It explains the servlet processing lifecycle including initialization, service, and destruction phases. Finally, it provides instructions on installing Apache Tomcat server and Eclipse IDE and creating a simple servlet program.

Uploaded by

relacin216
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 63

Subject: internet programming (IP) (CSDLO5012) SH-2022

Lecture 6

Module 3: Backend development

Mrs. Priyanka Y Abhyankar


Assistant Professor
Computer Engineering Department,
SAKEC
BACKEND
DEVELOPMENT
JAVA
SERVLETS

3
Form GET & POST methods
GET POST
Parameters In URL In body
Usage Fetching documents Updating data
BACK button/Reload Harmless Data will be re-submitted (the browser should alert the
user that the data are about to be re-submitted)
Bookmarked Can be bookmarked Cannot be bookmarked
Cached Can be cached Not cached
History Parameters remain in browser history Parameters are not saved in browser history
Restrictions on data Yes, when sending data, the GET method adds the No restrictions
length data to the URL; and the length of a URL is limited
(maximum URL length is 2048 characters)
Restrictions on data type Only ASCII characters allowed No restrictions. Binary data is also allowed
Security GET is less secure compared to POST because POST is a little safer than GET because the parameters
data sent is part of the URL are not stored in browser history or in web server logs
Never use GET when sending passwords or other
sensitive information!
Visibility Data is visible to everyone in the URL Data is not displayed in the URL

4
CGI v/s Servlets

5
CGI v/s Servlets
Basis Servlet CGI
It is thread based i.e. for every new request new It is process-based i.e. for every new request new
Approach
thread is created. process is created.
Language Used JAVA any programming language
The user will not get the benefits of OOPs, since it is
Object-Oriented Yes, and the user will get the benefits of OOPs
written in any language
Portability Yes No
Removed after the completion of the process-based
Persistence Needs to be destroyed explicitly.
request.
Server Independent Can use any of the web-server. Can use the web-server that supports it.
Construction and destruction of new threads are Construction and destruction of the new processes
Cost
not costly. are costly.
Speed Faster Slower
Platform
Platform independent Platform dependent.
dependency

6
CGI v/s Servlets
CGIs create 1 process for each request sent by the client as
opposed to a single process handling multiple requests in
Servlets.
Servlets are written in Java so are platform independent,
secure, portable and has access to various APIs created on
Java such as JDBC
Servlets can read implicit and explicit data.
Explicit (HTML data)
Implicit (Cookies, media files, etc.)

7
What are Servlets?
Servlets are the Java programs that run on the Java-enabled
web server or application server.
Used to handle the request obtained from the webserver,
process the request, produce the response, then send a
response back to the webserver.

8
What are Servlets?
A Servlet runs inside a Servlet container.
A Servlet container is the hosting and execution
environment for Java Servlets.
We can consider it to be a compiler plus run-time hosting
environment for Servlets.
E.g. Tomcat
Servlet container runs inside a Web server, such as Apache.

9
Servlet Processing
Browser sends an HTTP request to the Web server. (request
was for executing a Servlet)
Web server notices the request for the execution of a Servlet &
hands it over to the Servlet container, after providing apt
execution environment to the Servlet container.
Servlet container loads and executes the Servlet (i.e., the .class
file of the Servlet) by interpreting its contents via the JVM. The
result of the Servlet processing is usually some HTML output.
This HTML output is sent back to the Web browser via the Web
server, as a part of the HTTP response.
10
Servlet Processing

Servlet Processing Concept


(Servlet Architecture)
Servlet Processing

Servlet Processing Concept (Servlet Architecture)


Servlet Life-Cycle

Servlet Life-cycle
Servlet Life-Cycle
Servlet initialization
Done by the Servlet container. Whenever a Servlet is deployed in a
Servlet container, the container decides when to load a Servlet. The
programmer cannot explicitly initialize a Servlet. As a result of
initializing the Servlet, an instance of the Servlet is created in the
memory. From this instance, as many Servlet threads as needed
would get created to service the actual client requests.

14
Servlet Life-Cycle
Servlet Service
Once initialized, the Servlet can service client requests. This
process is repeated for every client request. In other words,
whenever an HTTP request arrives for a Servlet, the Servlet services
it, as appropriate with the help of the particular thread of the Servlet
instance.

15
Servlet Life-Cycle
Servlet destruction
Also happens only once. The container chooses an appropriate
moment to destroy the Servlet. Usually, when the container
resources are getting exhausted because of memory shortage, etc.,
the container decides to destroy one of the Servlets. On what basis
it decides it, and how it actually puts it into action, is unpredictable.
The programmer should not expect that the container would do the
Servlet destruction at a particular point, or based on some
condition.

16
THANKS!
Any questions?

19
Resources
Achyut S Godbole and Atul Kahate, ―Web Technologies, Second
Edition, Tata McGraw Hill, 2012.
https://docs.oracle.com/javaee/6/tutorial/doc/bnafd.html
https://www.edureka.co/blog/java-servlets
https://www.ibm.com/docs/en/i/7.2?topic=java-jsp-servlet-
programming
Lecture Outcomes
Learnt:
1. Difference between GET and POST methods
2. Why are Servlets preferred over CGI?
3. What are servlets?
4. Phases of Servlet life-cycle
Subject: internet programming (IP) (CSDLO5012) SH-2022
Lecture 7

Module 3: Backend development

Mrs. Priyanka Y Abhyankar


Assistant Professor
Computer Engineering Department,
SAKEC
Learning Objectives
In this lecture you will learn to:
▸ Install and configure Apache Tomcat
server
▸ Write simple servlet program
▸ Define Session
▸ Describe Session handling

2
Apache Tomcat &
Eclipse IDE
Installing Apache Tomcat
Go to: https://tomcat.apache.org/download-90.cgi

4
Installing Apache Tomcat
Go to: https://www.eclipse.org/downloads/

5
Installing Apache Tomcat
First extract the eclipse folder to your desired location

6
Installing Apache Tomcat
Create a folder for workspace and the run the eclipse
application

7
Installing Apache Tomcat
Extract the Apache Tomcat folder in the eclipse folder
created

8
Installing Apache Tomcat
Launch the eclipse IDE and close the welcome page to see
the following page

9
Installing Apache Tomcat
Click on the link to see the following dialog box. Then select
Apache > Tomcat v9.0 Server and click Next.

10
Installing Apache Tomcat
Following dialog box opens. Click on browse and select the
extracted Apache folder.

11
Simple Servlet Program
Go to Eclipse IDE
Click on New > Dynamic Web Project
Name your project as per your liking.
Click next and make sure to have “Generate web.xml
deployment descriptor” checked.
Click on finish.

12
Simple Servlet Program
Go to src > webapp > and create a new HTML file.
Write the desired HTML code.
Go to Java Resources > src/main/java > and create a new
Servlet file
The name of the servlet file should be the same as the form
action attribute

13
Points to note
PrintWriter class prints formatted representations of objects
to a text-output stream (Similar to System.out)

14
THANKS!
Any questions?

15
Resources
Achyut S Godbole and Atul Kahate, ―Web Technologies, Second
Edition, Tata McGraw Hill, 2012.
Harvey & Paul Deitel& Associates, Harvey Deitel and Abbey Deitel,
Internet and World Wide Web - How To Program, Fifth Edition,
Pearson Education, 2011.
https://tomcat.apache.org/download-90.cgi
https://www.eclipse.org/downloads/
Lecture Outcomes
Learnt to:
1. Install and configure Eclipse and Apache Tomcat Server
2. Write simple servlet program
Subject: internet programming (IP) (CSDLO5012) SH-2022
Lecture 8

Module 3: Backend development

Mrs. Priyanka Y Abhyankar


Assistant Professor
Computer Engineering Department,
SAKEC
Learning Objectives
In this lecture you will learn to:
▸ Define Session
▸ Describe and Apply Session handling
▸ Describe and Apply Cookies
▸ Describe and Apply Hidden form fields

2
Session Handling
Session Handling
E-commerce scenario
Needed because HTTP is stateless
Solution: Session handling

4
Session Handling
Session is a time interval.
Way to maintain state (data) of an user.
Helps recognize a user.
Session Tracking API allows you to
▹ look up session object associated with current request
▹ create a new session object when necessary
▹ look up information associated with a session
▹ store information in a session
▹ discard completed or abandoned sessions
5
Session Handling
Session Tracking Techniques
▹ Cookies
▹ Hidden Form Field
▹ URL Rewriting
▹ HttpSession

6
Cookies
Textual information that is stored in key-value pair format to the
client’s browser.
State management techniques. Server treats every client request as
a new one.
When the client generates a request, the server gives the response
with cookies having an id which are then stored in the client’s
browser.
The server will fetch the cookie id, if found it will treat it as an old
request otherwise the request is considered new.

7
Cookies
3. Request 2 + Old Cookie

1. Request 1

Client Server
2. Response + Cookie

Cookie stored after request 1

Client
Browser

8
Using cookies in Java
Use “Cookie” class that is present in javax.servlet.http package.
To make a cookie, create an object of Cookie class and pass a name
and its value.
To add cookie in response, use addCookie(Cookie) method of
HttpServletResponse interface.
To fetch the cookie, use getCookies() method of Request Interface.

9
Disadvantges of Cookies
It will not work if cookie is disabled from the browser.
Only textual information can be set in Cookie object.

10
Hidden Form Fields
Invisible text field is used for maintaining the state of an user.
Store the information in the hidden field and get it from another
servlet.
This approach is better if we have to submit form in all the pages
and we don't want to depend on the browser.
Widely used in comment form of a website. Page id or page name
stored in the hidden field so that each page can be uniquely
identified.

11
Hidden Form Fields
Works even if cookies are disabled.
Maintained at server side.
Extra form submission is required on each pages.
Only textual information can be used.

12
Session Handling API
Looking up a session object
HttpSession session = request.getSession(true);
Pass true to create a new session if one does not exist
Associating information with session
session.setAttribute(“user”,request.getParameter(“name”))
Session attributes can be of any type
Looking up session information
String name = (String) session.getAttribute(“user”)

13
Session Handling API
getId: the unique identifier generated for the session
isNew: true if the client (browser) has never seen the session
getCreationTime: time in milliseconds since session was made
getLastAccessedTime
time in milliseconds since the session was last sent from client
getMaxInactiveInterval
No. of seconds session should go without access before being
invalidated
negative value indicates that session should never timeout
14
THANKS!
Any questions?

15
Resources
Achyut S Godbole and Atul Kahate, ―Web Technologies, Second
Edition, Tata McGraw Hill, 2012.
Harvey & Paul Deitel& Associates, Harvey Deitel and Abbey Deitel,
Internet and World Wide Web - How To Program, Fifth Edition,
Pearson Education, 2011.
https://www.javatpoint.com/session-tracking-in-servlets
Lecture Outcomes
Learnt to:
1. Install and configure Eclipse and Apache Tomcat Server
2. Write simple servlet program
3. Define Session
4. Describe Session handling
Subject: internet programming (IP) (CSDLO5012) SH-2022
Lecture 9

Module 3: Backend development

Mrs. Priyanka Y Abhyankar


Assistant Professor
Computer Engineering Department,
SAKEC
Learning Objectives
In this lecture you will learn to:
▸ Describe and Apply URL Rewriting
▸ Describe and HTTP Session

2
Session Handling
URL Rewriting
Appending or modifying any URL structure while loading a page.
URL is modified to include a session ID.
Alternative if browser does not support cookies.
It doesn’t depend upon cookies and will work whether cookies are
enabled or disabled
Extra form submission is not required on all pages
It will work only with links ad can send only text

4
HTTP Session
Container creates a session id for each user to identify the user.
Object of HttpSession can be used to perform two tasks:
bind objects
view and manipulate information about a session, such as the
session identifier, creation time, and last accessed time.
Session is destroyed when the browser instance closes.

5
HTTP Session
Looking up a session object
HttpSession session = request.getSession(true);
Pass true to create a new session if one does not exist
Associating information with session
session.setAttribute(“user”,request.getParameter(“name”))
Session attributes can be of any type
Looking up session information
String name = (String) session.getAttribute(“user”)
session.removeAttribute(“user”) or session.invalidate();
6
HTTP Session
getId: the unique identifier generated for the session
isNew: true if the client (browser) has never seen the session
getCreationTime: time in milliseconds since session was made
getLastAccessedTime
time in milliseconds since the session was last sent from client
getMaxInactiveInterval
No. of seconds session should go without access before being
invalidated
negative value indicates that session should never timeout
7
THANKS!
Any questions?

8
Resources
Achyut S Godbole and Atul Kahate, ―Web Technologies, Second
Edition, Tata McGraw Hill, 2012.
Harvey & Paul Deitel& Associates, Harvey Deitel and Abbey Deitel,
Internet and World Wide Web - How To Program, Fifth Edition,
Pearson Education, 2011.
https://www.javatpoint.com/session-tracking-in-servlets
Lecture Outcomes
Learnt to:
1. Install and configure Eclipse and Apache Tomcat Server
2. Write simple servlet program
3. Define Session
4. Describe Session handling

You might also like