Skip to content

Instantly share code, notes, and snippets.

@UtkarshYadav01
Last active September 19, 2024 01:15
Show Gist options
  • Save UtkarshYadav01/2d14c4d8180d1a8f4766b1b8fe3bfab4 to your computer and use it in GitHub Desktop.
Save UtkarshYadav01/2d14c4d8180d1a8f4766b1b8fe3bfab4 to your computer and use it in GitHub Desktop.
Java Refresher Notes

Certainly! Let's quickly recap the Java 8 features related to interfaces:

Java 8 Features: Interfaces

Default Methods

  • Why Default Methods?

    • Prior to Java 8, adding a new method to an existing interface would break all classes implementing that interface.
    • Default methods allow us to add new methods to interfaces without affecting existing code.
  • Syntax for Default Methods:

    interface MyInterface {
        void regularMethod(); // Regular abstract method
        
        default void defaultMethod() {
            // Default implementation
            System.out.println("This is a default method.");
        }
    }
  • Usage:

    • Classes implementing the interface can choose to override the default method or use the provided implementation.
    • If a class overrides the default method, its implementation takes precedence.

Static Methods

  • Static Methods in Interfaces:

    • Introduced in Java 8.
    • Associated with the interface itself (not tied to any specific instance).
  • Syntax for Static Methods:

    interface MyInterface {
        static void staticMethod() {
            System.out.println("This is a static method.");
        }
    }
  • Usage:

    • Call static methods directly on the interface (e.g., MyInterface.staticMethod()).
    • Cannot be overridden by implementing classes.

Constant Fields

  • Declaring Constants in Interfaces:

    • Now possible in Java 8.
    • Constants are implicitly public, static, and final.
    • Example:
      interface MyConstants {
          int MAX_VALUE = 100;
          String DEFAULT_NAME = "John";
      }
  • Usage:

    • Access constants using the interface name (e.g., MyConstants.MAX_VALUE).

Remember these features—they enhance code reusability and flexibility. Feel free to explore more Java topics as needed! 😊

JSP Tag Cheat Sheet

1. Scriptlet Tag (<% %>)

  • Used for writing Java code directly within a JSP.

    <% int count = 42; %>

2. Declaration Tag (<%! %>)

  • Declares variables/methods accessible throughout the JSP page.

    <%!
    int calculateSquare(int num) {
        return num * num;
    }
    %>

3. Expression Tag (<%= %>)

  • Evaluates and outputs the result directly to the client.

    <p>The result is: <%= calculateSquare(5) %></p>

4. Directive Tag (<%@ %>)

  • Provides global page information.
    • Page Directive:
      <%@ page contentType="text/html; charset=UTF-8" pageEncoding="UTF-8" %>
    • Import Directive:
      <%@ page import="com.ucode.UserDAO, com.ucode.model.User, java.util.*" %>
    • Taglib Directive:
      <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>

5. Action Tag

  • Performs specific tasks within the JSP.
    • Include Action (includes content from another resource):
      <jsp:include page="header.jsp" />
    • Forward Action (forwards the request to another resource):
      <jsp:forward page="result.jsp" />
    • UseBean Action (instantiates a JavaBean):
      <jsp:useBean id="u" class="com.ucode.model.User"></jsp:useBean>
    • SetProperty Action (populates JavaBean properties):
      <jsp:setProperty property="*" name="u"/>

6. Custom Tags

  • Encapsulate complex functionality into reusable tags.
    • Create Tag Handler Class:
      public class HelloTag extends TagSupport {
          public int doStartTag() throws JspException {
              pageContext.getOut().print("Hello, Custom Tag!");
              return SKIP_BODY;
          }
      }
    • Taglib Declaration (hello.tld):
      <taglib>
          <tag>
              <name>hello</name>
              <tag-class>com.ucode.tags.HelloTag</tag-class>
          </tag>
      </taglib>
    • Use Custom Tag:
      <%@ taglib uri="http://example.com/tags" prefix="ex" %>
      <ex:hello />

7. Expression Language (${})

  • Simplifies access to data stored in JavaBeans or request/session scope.

    <p>Welcome, ${u.username}</p>

8. Accessing Client Request Object

  • To get user agent:
    <%= request.getHeader("User-Agent") %>
  • To get client's locale:
    <%= request.getLocale() %>

9. Handling Errors in JSP

  • Set a custom error page:
    <%@ errorPage="error.jsp" %>
  • Indicate error page:
    <%@ page isErrorPage="true" %>

10. Common Error Page Configuration (web.xml)

<error-page>
    <exception-type>java.lang.Exception</exception-type>
    <location>/jsp/error.jsp</location>
</error-page>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment