Ch 4 link is at the bottom of the page
Click here for more IMP AND EXAMPLES 👇
UNIT - 3 SERVLETS
1. LIFE CYCLE OF SERVLETS (Do it yourself)
2. SERVLETCONTEXT AND SERVLETCONFING INTERFACE
3. EXPLORING SESSION TRACKING MECHANISMS
4.ADVANTAGES AND DISADVANTAGES OF SERVLET
2. ServletContext and ServletConfig Interfaces
ServletConfig:
- Provides configuration information specific to a single servlet.
- Set up in the servlet deployment descriptor (web.xml).
Key Methods:
- String getServletName(): Returns the name of the servlet.
- String getInitParameter(String name): Retrieves initialization parameters.
- ServletContext getServletContext(): Provides access to the- ServletContext.
Example:
ServletContext:
- Represents the entire web application.
- Used for application-wide settings or resources shared by all servlets.
Key Methods:
- String getInitParameter(String name): Retrieves context initialization parameters.
- Object getAttribute(String name): Accesses attributes shared across servlets.
- void setAttribute(String name, Object value): Sets shared attributes.
Example:
3. Exploring Session Tracking Mechanisms
Session tracking is the process of maintaining a user’s state across multiple requests. Servlet API provides several mechanisms for this:
1. Cookies:
- Stores session information on the client side.
- Sent with each HTTP request.
- Simple to use but can be disabled by the user.
Example:
2. URL Rewriting:
- Embeds session data as a query string in the URL.
- Useful when cookies are disabled.
Example:
3. Hidden Form Fields:
- Passes session data using hidden fields in HTML forms.
- Requires forms for every interaction.
Example:
4. HTTP Session (Recommended):
- Managed by the servlet container.
- Stores session data on the server side.
Key Methods:
- HttpSession session = request.getSession();
- session.setAttribute("user", "John");
- String user = (String) session.getAttribute("user");
Example:
4. Advantages and Disadvantages of Servlets
Advantages:
- Platform Independence: - Written in Java, servlets are platform-independent.
 
- Efficiency: - Servlets are faster than CGI as they don’t create a new process for each request.
 
- Rich API: - Provides extensive APIs for handling requests, sessions, cookies, and more.
 
- Scalability: - Handles multiple requests using a single servlet instance with multithreading.
 
- Integration: - Easily integrates with other Java-based technologies like JSP, EJB, and frameworks.
 
- Robustness: - Strong exception handling and type safety due to Java.
 
Disadvantages:
- Complexity: - Managing presentation and business logic in servlets can become cumbersome.
 
- Thread Safety: - Must handle multithreading carefully, as multiple threads access the same servlet instance.
 
- Limited to Java: - Requires knowledge of Java, restricting non-Java developers.
 
- Verbose Code: 
- Writing HTML in servlets is cumbersome compared to JSP or modern frameworks.
Click here for more IMP FOR SUBJECTS 👇
 
إرسال تعليق