Giter Club home page Giter Club logo

materialsummary-webapp's Introduction

1. Servlet API and JSP Overview

  • Servlet: A Java program that runs on a server, handling client requests and generating dynamic web content.
  • Servlet API: Provides the necessary interfaces and classes for writing servlets. Key components include HttpServlet, HttpServletRequest, HttpServletResponse, and the servlet lifecycle methods (init, service, destroy).

2. JSP Elements and Components

  • JSP Page Elements:
    • Directives: <%@ %> used for setting up the page environment, such as <%@ page %>, <%@ include %>, and <%@ taglib %>.
    • Declarations: <%! %> for declaring variables and methods.
    • Scriptlets: <% %> for embedding Java code.
    • Expressions: <%= %> for outputting values.
    • Actions: <jsp:action> tags for performing built-in functionalities like <jsp:include>, <jsp:forward>, and <jsp:useBean>.
    • Implicit Objects: Predefined variables like request, response, session, application, out, config, pageContext, page, and exception.

3. JSP Actions and Implicit Objects

  • JSP Actions: Tags that perform built-in functions, such as jsp:include, jsp:forward, and jsp:useBean.
  • Implicit Objects: Objects available in JSP pages without explicit declaration, such as request, response, session, application, out, config, pageContext, page, and exception.

4. JSP Standard Tag Library (JSTL)

  • JSTL: A collection of tags that provide core functionalities, such as loops and conditionals. Categories include:
    • Core Tags: General-purpose tags.
    • Formatting Tags: For formatting text, dates, numbers.
    • SQL Tags: For interacting with databases.
    • XML Tags: For manipulating XML data.

5. HTTP Protocol

  • HTTP Requests: Consist of methods like GET, POST, PUT, DELETE, headers, and body content.
  • HTTP Responses: Include status codes, headers, and body content. Notable status codes include 200 (OK), 404 (Not Found), 500 (Internal Server Error).

6. Web Architecture and Components

  • Web Server: Delivers web pages, images, and other resources over the web.
  • Tiers in Web Applications: Logical chunks such as presentation tier, logic tier, and data tier.

7. Cookies in Servlets

  • Creating Cookies: Use new Cookie(name, value) and setMaxAge(seconds) to set the expiration.
  • Retrieving Cookies: Use request.getCookies() and iterate to find the desired cookie.

8. Expression Language (EL)

  • EL Implicit Objects: Objects like param, paramValues, header, headerValues, cookie, and initParam.

9. JSP and Servlet Lifecycle

  • JSP Lifecycle Methods: jspInit(), _jspService(), and jspDestroy().
  • Servlet Lifecycle:
    • Class Loaded: Servlet class is loaded.
    • Instantiation: Servlet instance is created.
    • Initialization: init() method is called.
    • Service: service() method handles requests.
    • Destruction: destroy() method is called before servlet is taken out of service.

10. Code Snippets and Examples

  • JSP c:set Tag: <c:set var="FirstNum" value="10"/>, <c:set var="SecondNum" value="40"/>
  • Output with c:out: <c:out value="${FirstNum + SecondNum}"/>

11. Struts Framework

  • ActionForm Bean: Used in Struts to encapsulate form data.
  • Struts Configuration: Registering ActionForm classes in struts-config.xml.

12. AJAX Overview and Implementation in JSP

AJAX (Asynchronous JavaScript and XML) is a technique for creating fast and dynamic web pages. It allows web pages to be updated asynchronously by exchanging small amounts of data with the server behind the scenes. This means that parts of a web page can be updated without reloading the entire page.

Key Components of AJAX

  1. XMLHttpRequest Object: Used to exchange data with a server.
  2. JavaScript: Manages the AJAX requests and responses.
  3. Server-side Script: Processes the requests and sends responses (e.g., servlets in Java).

Example 1: Gender Field Validation

HTML and JavaScript Code:

Gender: <input id="gender" type="text" onkeyup="doLookup()">
<script>
    function doLookup() {
        var xhr = new XMLHttpRequest();
        var gender = document.getElementById('gender').value;
        xhr.open('GET', 'Validation?gender=' + gender, true);
        xhr.onreadystatechange = function() {
            if (xhr.readyState === 4 && xhr.status === 200) {
                var response = xhr.responseText;
                // Handle the response from the server
                console.log(response);
            }
        };
        xhr.send();
    }
</script>

Explanation:

  • The onkeyup event triggers the doLookup() function each time the user types in the Gender text box.
  • XMLHttpRequest is used to send a GET request to the Validation servlet with the gender value as a parameter.
  • The servlet processes the request and returns a response, which is handled in the onreadystatechange function.

Example 2: E-mail ID Validation

HTML and JavaScript Code:

E-mail ID: <input id="emailId" type="text" onblur="validateEmail()">
<script>
    function validateEmail() {
        var xhr = new XMLHttpRequest();
        var email = document.getElementById('emailId').value;
        xhr.open('POST', 'Validation', true);
        xhr.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
        xhr.onreadystatechange = function() {
            if (xhr.readyState === 4 && xhr.status === 200) {
                var response = xhr.responseText;
                // Handle the response from the server
                console.log(response);
            }
        };
        xhr.send('email=' + encodeURIComponent(email));
    }
</script>

Explanation:

  • The onblur event triggers the validateEmail() function when the text box loses focus.
  • XMLHttpRequest is used to send a POST request to the Validation servlet with the email value as a parameter.
  • The request header is set to application/x-www-form-urlencoded to ensure proper encoding of the data.
  • The servlet processes the request and returns a response, which is handled in the onreadystatechange function.

13. Asynchronous Processing in Servlets

Asynchronous Processing in servlets allows long-running tasks to be executed in separate threads, freeing up the servlet container to handle other requests. This is particularly useful for improving the scalability and performance of web applications that need to handle time-consuming tasks.

Key Concepts

  1. AsyncContext: An object that holds the context for asynchronous processing.
  2. AsyncListener: An interface for receiving notifications about the lifecycle of an asynchronous operation.
  3. Completing the Async Operation: Methods like complete() and dispatch() to signal the end of the async processing.

Example Scenario

You need to create an asynchronous servlet that uses a thread pool to handle a time-consuming task.

Code Snippet for Asynchronous Servlet

  1. Servlet Configuration and Initialization:
    • Define the servlet and configure async support in the web.xml or using annotations.
import javax.servlet.AsyncContext;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

@WebServlet(urlPatterns = "/asyncTask", asyncSupported = true)
public class AsyncServlet extends HttpServlet {
    private ExecutorService executorService;

    @Override
    public void init() throws ServletException {
        super.init();
        executorService = Executors.newFixedThreadPool(20); // Thread pool with 20 threads
    }

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        AsyncContext asyncContext = request.startAsync();
        executorService.submit(new AsyncRequestProcessor(asyncContext));
    }

    @Override
    public void destroy() {
        executorService.shutdown();
        super.destroy();
    }
}
  1. AsyncRequestProcessor Class:
import javax.servlet.AsyncContext;
import java.io.IOException;

public class AsyncRequestProcessor implements Runnable {
    private AsyncContext asyncContext;

    public AsyncRequestProcessor(AsyncContext asyncContext) {
        this.asyncContext = asyncContext;
    }

    @Override
    public void run() {
        // Simulate a time-consuming task
        try {
            Thread.sleep(5000); // Simulate delay
            asyncContext.getResponse().getWriter().write("Task completed!");
        } catch (InterruptedException | IOException e) {
            e.printStackTrace();
        } finally {
            asyncContext.complete(); // Complete the asynchronous processing
        }
    }
}

Explanation:

  1. Servlet Configuration:

    • The @WebServlet annotation with asyncSupported = true enables asynchronous processing for the servlet.
    • The init method initializes an ExecutorService with a thread pool of 20 threads.
  2. Handling Requests Asynchronously:

    • In the doGet method, request.startAsync() starts asynchronous processing and returns an AsyncContext.
    • The executorService.submit(new AsyncRequestProcessor(asyncContext)) submits the task to the thread pool.
  3. AsyncRequestProcessor Class:

    • Implements Runnable to define the task to be executed asynchronously.
    • Simulates a time-consuming task with Thread.sleep(5000).
    • Writes a response and calls asyncContext.complete() to complete the asynchronous processing.

materialsummary-webapp's People

Contributors

harryrosianto avatar

Stargazers

Arajdian Altaf avatar M Kemal SR avatar Dharmawan Surya Kusuma avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.