<%@ page import="javax.servlet.http.HttpSession" %> <% HttpSession session = request.getSession(true); %> <% session.setAttribute("username", "John"); %> <% String username = (String) pageContext.getAttribute("username", PageContext.SESSION_SCOPE); %>
<% request.setAttribute("message", "Hello World"); %> <% String message = (String) pageContext.getAttribute("message", PageContext.REQUEST_SCOPE); %>In this example, a request attribute named "message" is set to "Hello World". Then, the PageContext.getAttribute() method is used to retrieve this attribute value from the request scope. In both examples, we are using the PageContext class from the javax.servlet.jsp package library to retrieve attribute values from different scopes.