Discuss various scopes of JSP objects briefly. Create HTML File with principal, time and rate. Then crate a JSP file that reads values from the HTML form, calculates simple interest and displays it.
SCOPE OF JSP OBJECTS
The availability of a JSP object for use from a particular place of the application is defined as the scope of that JSP object. Every object created on a JSP page will have a scope. Object scope in JSP is segregated into four parts and they are page, request, session, and application.
a) Page Scope-page scope means, the JSP object can be accessed only from within the same page where it was created. JSP implicit objects out, exception, response, pageContext, config, and page have page scope.
//Example of JSP Page Scope
<jsp:useBean id="employee" class="EmployeeBean" scope="page" />
b) Request Scope- A JSP object created using the request scope can be accessed from any pages that serves that request. More than one page can serve a single request. Implicit object request has the request scope.
//Example of JSP Request Scope
<jsp:useBean id="employee" class="EmployeeBean" scope="request" />
c) Session Scope-session scope means, the JSP object is accessible from pages that belong to the same session from where it was created. The implicit object session has the session scope.
//Example of JSP Session Scope
<jsp:useBean id="employee" class="EmployeeBean" scope="session" />
d)Application Scope- A JSP object created using the application scope can be accessed from any page across the application. The. Implicit object application has the application scope.
//Example of JSP application Scope
<jsp: useBean id="employee" class="EmployeeBean" scope="application" />
2nd part
index.html
<html>
<head>
<meta charset="UTF-8">
<title>Simple Interest</title>
</head>
<body>
<h1>Simple InterestF</h1>
<form action="index.jsp">
Enter the principal : <input type="text" name="principal"><br />
<br /> Enter the time : <input type="text" name="time"><br />
<br /> Enter the rate (%) : <input type="text" name="rate"><br />
<br /> <input type="submit" value="Calculate">
</form>
</body>
</html>
index.jsp
<html>
<head>
<meta charset="UTF-8">
<title>JSP Page</title>
</head>
<body>
<%
double p = Double.parseDouble(request.getParameter("principal"));
double t = Double.parseDouble(request.getParameter("time"));
double r = Double.parseDouble(request.getParameter("rate"));
double interest = (p * t * r) / 100;
%>
<h1>
Simple Interest : <%=interest %>
</h1>
</body>
</html>
Comments
Post a Comment