DIPLOMA CH 3 MOST IMP QUESTION (AJP)

 

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:

  1. String getServletName(): Returns the name of the servlet.
  2. String getInitParameter(String name): Retrieves initialization parameters.
  3. ServletContext getServletContext(): Provides access to the ServletContext.

Example:


public void init(ServletConfig config) { String paramValue = config.getInitParameter("myParam"); System.out.println("Initialization parameter value: " + paramValue); }

ServletContext:

  • Represents the entire web application.
  • Used for application-wide settings or resources shared by all servlets.

Key Methods:

  1. String getInitParameter(String name): Retrieves context initialization parameters.
  2. Object getAttribute(String name): Accesses attributes shared across servlets.
  3. void setAttribute(String name, Object value): Sets shared attributes.

Example:


ServletContext context = getServletConfig().getServletContext(); context.setAttribute("sharedData", "This is shared");



Click here for more IMP AND EXAMPLES 👇



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:


Cookie cookie = new Cookie("sessionID", "12345"); response.addCookie(cookie);

2. URL Rewriting:

  • Embeds session data as a query string in the URL.
  • Useful when cookies are disabled.

Example:


String url = response.encodeURL("welcomePage.jsp");

3. Hidden Form Fields:

  • Passes session data using hidden fields in HTML forms.
  • Requires forms for every interaction.

Example:


<input type="hidden" name="sessionID" value="12345">

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:


HttpSession session = request.getSession(); session.setAttribute("username", "JohnDoe");



Click here for more IMP AND EXAMPLES 👇



4. Advantages and Disadvantages of Servlets

Advantages:

  1. Platform Independence:

    • Written in Java, servlets are platform-independent.
  2. Efficiency:

    • Servlets are faster than CGI as they don’t create a new process for each request.
  3. Rich API:

    • Provides extensive APIs for handling requests, sessions, cookies, and more.
  4. Scalability:

    • Handles multiple requests using a single servlet instance with multithreading.
  5. Integration:

    • Easily integrates with other Java-based technologies like JSP, EJB, and frameworks.
  6. Robustness:

    • Strong exception handling and type safety due to Java.

Disadvantages:

  1. Complexity:

    • Managing presentation and business logic in servlets can become cumbersome.
  2. Thread Safety:

    • Must handle multithreading carefully, as multiple threads access the same servlet instance.
  3. Limited to Java:

    • Requires knowledge of Java, restricting non-Java developers.
  4. Verbose Code:

    • Writing HTML in servlets is cumbersome compared to JSP or modern frameworks.

Click here for more IMP FOR SUBJECTS 👇


CH 4 IMP

Post a Comment

Previous Post Next Post