Java Based
Web Development
Servlet architecture
• Servlet lifecycle
• Handling GET and POST requests
• Sessions and cookies
Java Based Web Technologies
Introduction
Java-based web technologies are technologies that use the programming language Java to
develop dynamic websites and web applications.
These technologies help developers create:
•Dynamic websites
•Enterprise applications
•Banking systems
•E-commerce websites
•Online management systems
Java web applications run on a web server or application server.
Introduction to Servlet
What is a Servlet?
Advantages of Servlet
A Servlet is a Java program that runs on the web
server and helps create dynamic web pages. Advantage Description
Servlets are mainly used for: Fast Better performance
•Processing user requests Secure Runs on server
•Handling forms
Portable Works on all platforms
•Database connectivity
Reusable Can be reused easily
•Session management
Servlets work on the server side.
Servlet Architecture Diagram of Servlet Architecture
What is Servlet Architecture? Client (Browser)
Servlet architecture describes how a servlet ↓
works between client and server. Web Server
Components of Servlet Architecture ↓
Servlet Container
[Link] (Browser) ↓
[Link] Server Servlet
[Link] Container ↓
Database
[Link]
↓
[Link] (optional) Response Back
Working of Servlet Architecture What is Servlet Container?
Step-by-Step Process Servlet container manages servlets.
[Link] sends request from browser. Responsibilities
[Link] goes to web server. •Loads servlet
•Creates objects
[Link] server passes request to servlet container.
•Manages lifecycle
[Link] container calls servlet. •Handles requests/responses
[Link] processes request. Examples
•Apache Tomcat
[Link] sent back to browser.
•GlassFish
Servlet Lifecycle [Link]() Method
What is Servlet Lifecycle? Purpose
Servlet lifecycle means different stages Used to initialize servlet.
through which a servlet passes during Called
execution. Only once when servlet is loaded.
There are mainly 3 lifecycle methods: public void init()
[Link]() {
[Link]() [Link]("Servlet Initialized");
[Link]() }
Lifecycle Diagram 2. service() Method
Purpose
Loading Servlet
Processes client requests.
↓
Called
init()
Every time when request comes
↓
service() public void service(ServletRequest req,
↓ ServletResponse res)
destroy() {
[Link]("Request Processed");
}
3. destroy() Method
Purpose
Used to destroy servlet and release resources.
Called
Only once before servlet removal. Complete Lifecycle Example
Example import [Link].*;
import [Link].*;
public void destroy()
{ public class DemoServlet extends GenericServlet
[Link]("Servlet Destroyed"); {
} public void init()
{
Handling GET and POST Requests [Link]("Init Method");
}
What are GET and POST Requests?
These are HTTP methods used to send data public void service(ServletRequest req,
between browser and server. ServletResponse res)
{
GET Request
[Link]("Service Method");
Features of GET }
•Data visible in URL
public void destroy()
•Less secure
{
•Limited data transfer
[Link]("Destroy Method");
Example URL: login?name=Pankaj }
}
doGet() Method doPost() Method
Used to handle GET requests Used to handle POST requests.
import [Link].*;
import [Link].*;
import [Link].*;
import [Link].*;
import [Link].*;
import [Link].*;
public class MyServlet extends HttpServlet
public class LoginServlet extends HttpServlet
{
{
public void doGet(HttpServletRequest req,
public void doPost(HttpServletRequest req,
HttpServletResponse res)
HttpServletResponse res)
throws IOException
throws IOException
{
{
PrintWriter out = [Link]();
PrintWriter out = [Link]();
String name = [Link]("name");
String user =
[Link]("username");
[Link]("Welcome " + name);
}
[Link]("Hello " + user);
}
}
POST Request }
Features of POST
•Data hidden
•More secure
•Large amount of data can be sent
Difference Between GET and POST Advantages of Session
GET POST •Secure
Data visible in URL Data hidden •Stores user information
•Maintains user state
Less secure More secure
What is Cookie?
Limited data Large data A cookie is a small file stored in the user's
Faster Slightly slower browser.
It stores user-related information.
Sessions and Cookies
Example:
What is Session?
•Remember username
A session is used to store user data temporarily
•Website preferences
on the server.
Example: Creating Cookie
•Online shopping cart Cookie ck =
•Login session new Cookie("user","Rahul");
Session Example [Link](ck);
HttpSession session = [Link](); Reading Cookie
[Link]("user","Rahul");
Cookie c[] = [Link]();
Getting Session Value for(Cookie x : c)
{
String name = [Link]([Link]());
(String)[Link]("user"); }
Difference Between Session and Cookie Top 10 Most Important (VVI) University Exam
Questions
Session Cookie [Link] Servlet Architecture with suitable diagram.
[Link] Servlet Lifecycle and discuss init(), service(),
Stored on server Stored in browser and destroy() methods with examples.
[Link] between GET and POST methods in
More secure Less secure Servlet/PHP.
[Link] Sessions and Cookies with suitable
Temporary Can be permanent examples. Also differentiate between them.
[Link] is Server Side Scripting? Explain its advantages
Real Life Examples and applications.
[Link] is PHP? Explain PHP syntax and features with
Technology Example suitable examples.
Session Login system [Link] Variables and Data Types in PHP with
Cookie Remember password suitable examples.
GET Method Search engine query [Link] Control Structures in PHP (if, if-else, else-if,
POST switch) with suitable examples.
Login form
Method [Link] Form Handling in PHP using GET and POST
methods with examples.
[Link] is Form Validation in PHP? Explain different
validation techniques with suitable examples.