Things of
Things of
### 1. **Performance**
- **CGI**: Each request to a CGI script spawns a new
process. This is resource-intensive and can cause
significant overhead, especially when handling multiple
requests.
- **Servlet**: Servlets run within the same JVM (Java
Virtual Machine) and are multithreaded. A new thread is
created for each request, which is much more efficient
than spawning a new process.
### 3. **Scalability**
- **CGI**: Due to the overhead of creating new
processes for each request, CGI does not scale well under
heavy load.
- **Servlet**: The lightweight threading model of
servlets makes them highly scalable, suitable for high-
traffic applications.
### 4. **Persistence**
- **CGI**: CGI scripts do not maintain state between
requests, which makes managing user sessions or
persistent data difficult.
- **Servlet**: Servlets can maintain state through
mechanisms like session tracking and cookies, making
them more suitable for web applications that require state
management.
### 5. **Security**
- **CGI**: Each CGI script execution can pose a security
risk, especially when dealing with sensitive data or
multiple concurrent users.
- **Servlet**: Servlets, running within the JVM, benefit
from Java’s built-in security model, including sandboxing
and access control, making them more secure in
comparison.
2. **Initializing (init())**:
- The container calls the `init()` method once when the
servlet is loaded.
- This method is used to perform initialization tasks like
reading configuration files, establishing database
connections, etc.
5. **Destroying (destroy())**:
- The `destroy()` method is called when the servlet is
being unloaded or the server is shutting down.
- It’s used to release resources like database
connections and clean up before the servlet is removed
from memory.
6. **Servlet Destroyed**:
- After the `destroy()` method, the servlet is removed
from memory and is eligible for garbage collection.
Q what is cookies
A **cookie** is a small text file that is stored on a user's
device by a web server when they visit a website. Cookies
allow websites to store and retrieve information about a
user’s browsing session, creating a stateful interaction
between the client (user’s browser) and the server. This
helps websites remember user-specific data, such as
preferences, authentication details, and session
information, between visits or page reloads. Cookies are
commonly used for a variety of purposes, including user
authentication, tracking browsing behavior, storing
personalized settings (like language or theme
preferences), and maintaining a user’s shopping cart in e-
commerce sites.
There are different types of cookies based on their
purpose and lifespan. **Session cookies** are temporary
and are deleted when the user closes the browser. They
are often used for tasks like remembering user actions
during a single browsing session (e.g., maintaining a login
state). **Persistent cookies**, on the other hand, remain
on the user’s device for a specified period, even after the
browser is closed. These are used to remember user
preferences across multiple visits, such as saving login
credentials or language preferences.
Q How Many types of Cookies are there tu Seruled Where
used?
Here's a more concise version of the types of cookies:
### 1. **Based on Lifetime:**
- **Session Cookies**: Temporary cookies that last only
during the browser session and are deleted when the
browser is closed.
- **Persistent Cookies**: Stored on the device for a set
period and remain even after the browser is closed (e.g.,
for remembering login details).
### 2. **Based on Purpose:**
- **Necessary Cookies**: Essential for the website to
function (e.g., authentication).
- **Performance Cookies**: Track website usage and
improve performance (e.g., analytics).
- **Functionality Cookies**: Remember user preferences
for a personalized experience (e.g., language settings).
- **Advertising Cookies**: Used for targeted ads based
on user behavior (e.g., ad networks).
### 3. **Based on Scope:**
- **First-party Cookies**: Set by the website you're
visiting.
- **Third-party Cookies**: Set by external domains (e.g.,
ad networks for tracking across sites).
### Usage:
- **Authentication**, **session management**,
**personalization**, and **tracking** (e.g., ads and
analytics).
```java
public Cookie(String name, String value);
```
- **`name`**: The name of the cookie (e.g.,
`"userSessionId"`).
- **`value`**: The value of the cookie (e.g., `"12345"`).
1. **`setMaxAge(int expiry)`**
Sets the maximum age of the cookie in seconds.
- **Example**: `cookie.setMaxAge(3600);` // Cookie will
expire in 1 hour.
2. **`getName()`**
Returns the name of the cookie.
- **Example**: `String name = cookie.getName();`
3. **`getValue()`**
Returns the value of the cookie.
- **Example**: `String value = cookie.getValue();`
4. **`setDomain(String domain)`**
Sets the domain for which the cookie is valid.
- **Example**: `cookie.setDomain("example.com");` //
Valid for `example.com` and subdomains.
5. **`setPath(String path)`**
Sets the path for which the cookie is valid.
- **Example**: `cookie.setPath("/user");` // Valid for
`/user` and subpaths.