Page Import Directive

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
     
    <!-- Page Import attribute -->
    <%@ page import="java.util.Date" %>
<!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>Page import Directive  </title>
</head>
<body>
    Hi User! Today date is : <%= new Date() %>
</body>
</html>

PageContext in JSP

The pageContext object can be used to set,get or remove attribute from one of the following scopes:
JSP Page – Scope: PAGE_CONTEXT
HTTP Request – Scope: REQUEST_CONTEXT
HTTP Session – Scope: SESSION_CONTEXT
Application Level – Scope: APPLICATION_CONTEXT

Default scope is page.

Methods of pageContext Implicit Object:

  • Object findAttribute (String AttributeName) – It looks for an attribute in all scope
pageContext.findAttribute("username”);
  • Object getAttribute (String AttributeName, int Scope) – It looks for an attribute in the specified scope
pageContext.getAttribute("userName", PageContext.SESSION_CONTEXT);
  • void removeAttribute(String AttributeName, int Scope) – to remove an attribute from a given scope
pageContext.removeAttribute(“MyAttr”, PageContext. PAGE_CONTEXT);
  • void setAttribute(String AttributeName, Object AttributeValue, int Scope) – It writes an attribute in a given scope
pageContext.setAttribute(“mydata”, “This is my data”, PageContext. APPLICATION_CONTEXT)

WelcomeFile.html


<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
<title>Voter Eligibility Checker</title>
</head>
<body>
<form name="RegistrationForm" action="ValidateAge.jsp" >
Name: <input type="text" name="userName">
Age: <input type="text" name="age" >
<input type="submit" value="Submit">
</form>
</body>
</html>

ValidateAge.jsp


<%@ 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></title>
</head>
<body>
<%
String userName=request.getParameter("userName");
String age=(String)request.getParameter("age");
pageContext.setAttribute("name", userName,PageContext.SESSION_SCOPE);
pageContext.setAttribute("age", age,PageContext.SESSION_SCOPE);
%>
Hi <%=userName %>, Click below to check your voter eligibility
<a href="eligibilityChecker.jsp">Check Your Eligibility</a>
</body>
</html>

eligibilityChecker.jsp


<%@ 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></title>
</head>
<body>
<%
    String userName = (String)pageContext.getAttribute("name",PageContext.SESSION_SCOPE);
    String age = (String)pageContext.getAttribute("age",PageContext.SESSION_SCOPE);
    out.print(pageContext.findAttribute("age"));
    int userAge= Integer.parseInt(age);
         
     if(userAge>=18)
    {
%>
    <h2>Hi <%= userName %>, Your Eligible to vote </h2>
     
    <%
    }
    else
    {
    %>
    <h2>Hi <%= userName %>, Your Not Eligible to vote </h2>
<%} %>
</body>
</html>

JSTL

<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/sql" prefix="sql"%>
<sql:query dataSource="jdbc/btxdb" var="result">
select * from forum_posts where status='proceed' AND discussion_id='1';
</sql:query>
<c:forEach var="row" items="${result.rows}">
     <c:out value="${row.posts}"/>
</c:forEach>

Example #1

<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE HTML>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
  
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>Home</title>
    </head>
    <body>
        <div align="center">
            <h1>Contact List</h1>
            <table border="1">
                <th>No</th>
                <th>Username</th>
                <th>Email</th>
                  
                <c:forEach var="user" items="${userList}" varStatus="status">
                <tr>
                    <td>${status.index + 1}</td>
                    <td>${user.username}</td>
                    <td>${user.email}</td>
                              
                </tr>
                </c:forEach>            
            </table>
        </div>
    </body>
</html>

jsp:useBean – Action Tag:

EmployeeBean.java


package com.spin.sourse;
  
public class Employee {
  
       private int empID=101;
       private String empName = "Sridhar";
       
       public Employee(){
       }
              
       public int getEmpID() {
              return empID;
       }
       
       public void setEmpID(int empID) {
              this.empID = empID;
       }
       
       public String getEmpName() {
              return empName;
       }
       
       public void setEmpName(String empName) {
              this.empName = empName;
       }
}

TestEmployeeBean.java


 <%@ 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>Testing Use Bean</title>
</head>
<body>
  
<jsp:useBean id="Employee" class="com.spin.sourse.Employee" />
<jsp:setProperty property="*" name="Employee"/>
  
Employee id: <jsp:getProperty property="empID" name="Employee"/>
<br>
Employee Name: <jsp:getProperty property="empName" name="Employee"/>
  
</body>
</html>

jsp: forword – action tag:

First.jsp


<%@ 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>Jsp Include Action Tag </title>
</head>
<body>
  
<jsp:forward page="second.jsp">
  
<jsp:param value="Spinsoft Learning Soultions Pvt Ltd" name="companyName" />
  
</jsp:forward>
  
  
</body>
</html>

Second.jsp


 <%@ 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>This is Second jsp page</title>
</head>
<body>
  
<%= "Company Name received from the first jsp page is:"+ request.getParameter("companyName") %>
  
</body>
</html>

jsp:include – Action Tag:

<%@ 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>Jsp Include Action Tag </title>
</head>
<body>
 
<jsp:include page="Header.jsp"></jsp:include>
 
<br>
This is the body portion in First.Jsp
 
</body>
</html>

JSP Action Tags

<jsp:include> Action Tag:

To include the file during the request processing (dynamic)

Syntax of <jsp:include> :

 

<jsp:include page="page URL"  flush="Boolean Value" />

 

<jsp: forword> action tag:

It is used for redirecting the request to the page, mentioned in this action.

Syntax of <jsp:forward> :

 

<jsp:forward page="URL of the another page, JSP OR Servlet page" />

 

<jsp:useBean> Action Tag:

It is used to invoke and use Beans in jsp page. Once Bean class is instantiated, we have to use jsp:setProperty and jsp:getProperty actions to use the bean’s parameters.

Syntax of <jsp:useBean>:

<jsp: useBean id="unique_name_to_identify_bean"  class="package_name.class_name" />

	

Declaration Tag – Method

<%@ 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>Jsp Declaration Tag </title>
</head>
<body>
<%!
String changeUpperCase(String name)
{
 return name.toUpperCase();
}
%>
<%= "The returned value of the changeUpperCase method is :"+ changeUpperCase("Spinsoft Learning Solutions Pvt Ltd") %>
</body>
</html>

Declaration Tag – Variable

<%@ 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>First Jsp Page</title>
</head>
<body>
  
    <!-- Jsp Declaration tag -->
    <%! String name = "Spinsoft Learning Solutions"; %>
  
    <!-- Jsp Expression tag -->
    Declared string is: <%=name %>
  
</body>
</html>

Expression Tag

<%@ 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>First Jsp Page</title>
</head>
<body>
 Addition of 2+3 is <%=2+3 %>
        </body>
</html>