- 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
).
- 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
, andexception
.
- Directives:
- JSP Actions: Tags that perform built-in functions, such as
jsp:include
,jsp:forward
, andjsp:useBean
. - Implicit Objects: Objects available in JSP pages without explicit declaration, such as
request
,response
,session
,application
,out
,config
,pageContext
,page
, andexception
.
- 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.
- 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).
- 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.
- Creating Cookies: Use
new Cookie(name, value)
andsetMaxAge(seconds)
to set the expiration. - Retrieving Cookies: Use
request.getCookies()
and iterate to find the desired cookie.
- EL Implicit Objects: Objects like
param
,paramValues
,header
,headerValues
,cookie
, andinitParam
.
- JSP Lifecycle Methods:
jspInit()
,_jspService()
, andjspDestroy()
. - 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.
- 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}"/>
- ActionForm Bean: Used in Struts to encapsulate form data.
- Struts Configuration: Registering ActionForm classes in
struts-config.xml
.
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.
- XMLHttpRequest Object: Used to exchange data with a server.
- JavaScript: Manages the AJAX requests and responses.
- Server-side Script: Processes the requests and sends responses (e.g., servlets in Java).
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 thedoLookup()
function each time the user types in the Gender text box. XMLHttpRequest
is used to send a GET request to theValidation
servlet with the gender value as a parameter.- The servlet processes the request and returns a response, which is handled in the
onreadystatechange
function.
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 thevalidateEmail()
function when the text box loses focus. XMLHttpRequest
is used to send a POST request to theValidation
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.
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.
- AsyncContext: An object that holds the context for asynchronous processing.
- AsyncListener: An interface for receiving notifications about the lifecycle of an asynchronous operation.
- Completing the Async Operation: Methods like
complete()
anddispatch()
to signal the end of the async processing.
You need to create an asynchronous servlet that uses a thread pool to handle a time-consuming task.
Code Snippet for Asynchronous Servlet
- Servlet Configuration and Initialization:
- Define the servlet and configure async support in the
web.xml
or using annotations.
- Define the servlet and configure async support in the
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();
}
}
- 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:
-
Servlet Configuration:
- The
@WebServlet
annotation withasyncSupported = true
enables asynchronous processing for the servlet. - The
init
method initializes anExecutorService
with a thread pool of 20 threads.
- The
-
Handling Requests Asynchronously:
- In the
doGet
method,request.startAsync()
starts asynchronous processing and returns anAsyncContext
. - The
executorService.submit(new AsyncRequestProcessor(asyncContext))
submits the task to the thread pool.
- In the
-
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.
- Implements