A simple application demonstrating Request, Session, Context differences and usages in JSP and Servlets
Create a CSS file by name style.css :
body {
font-family: Verdana, Geneva, Arial, helvetica, sans-serif;
background-color: #FDF4DF;
font-size: 12px;
color: #000080;
}
td {
font-family: Verdana, Geneva, Arial, helvetica, sans-serif;
font-size: 14px;
font-weight: bold;
}
tr {
font-family: Verdana, Geneva, Arial, helvetica, sans-serif;
font-size: 14px;
}
.black {
color: blue;
font-weight: bold;
font-size: 16px;
font-family: Verdana, Geneva, Arial, helvetica, sans-serif;
}
Create a JSP file by name scope.jsp :
1. 2. 3. 4. 5. 6. 7. 8. 9. 10. 11. 12. 13. 14. 15. 16. 17. 18. 19. 20. 21. 22. 23. 24. 25. 26. 27. 28. 29. 30. 31. 32. 33. 34. 35. 36. 37. 38. 39. 40. 41. 42. 43. 44. 45. 46. 47. 48. 49. 50. 51. 52. 53. 54. 55. 56. 57. 58. |
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Scope testing in JSP</title>
<link href="style.css" rel="stylesheet" type="text/css">
</head>
<body>
<%
String name = request.getParameter("name");
session = request.getSession();
ServletContext context = request.getServletContext();
if (name != "" && name != null) {
session.setAttribute("name", name);
context.setAttribute("name", name);
}
String sessionName = (String) session.getAttribute("name");
String contextName = (String) context.getAttribute("name");
%>
<hr></hr>
<h2>Request , Session and Context Scope Usage :</h2>
<hr>
<br />
<table cellspacing="0" cellpadding="5" border="1">
<tr>
<td class="black" bgcolor="#FFFFFF">Scope</td>
<td class="black" bgcolor="#FFFFFF">Parameter</td>
<td class="black" bgcolor="#FFFFFF">Value</td>
</tr>
<tr>
<td>Request</td>
<td>name</td>
<td><%=name%></td>
</tr>
<tr>
<td>Session</td>
<td>name</td>
<td><%=sessionName%></td>
</tr>
<tr>
<td>Context</td>
<td>name</td>
<td><%=contextName%></td>
</tr>
</table>
<br />
<hr>
</body>
</html>
|
|
Running the steps below in order (mandatory) :
Step 1 : Opening scope.jsp in Mozilla Firefox with url as "http://localhost:8080/Scopes/scope.jsp?name=Jonty"
Output at the browser :
Step 2 : Opening scope.jsp in Mozilla Firefox's New Tab with url as "http://localhost:8080/Scopes/scope.jsp
Output at the browser :
Step 3 : Opening scope.jsp in Mozilla Firefox New Window with url as "http://localhost:8080/Scopes/scope.jsp
Output at the browser :
Step 4 : Opening scope.jsp in Internet Explorer with url as "http://localhost:8080/Scopes/scope.jsp
Output at the browser :