Watch “JSP Part-10 Implicit Object, Scope of JSP Variables” on YouTube

#Java #JSP #ImplicitObjectsinJSP #ScopeofJSPVariables #jsp:useBean #RaviROza

There are 9 jsp implicit objects.

These objects are created by the web container  that are available to all the jsp pages.

some commonly used objects for servlets that JSP page developers might need to use are:

  • request
  • response
  • out
  • session
  • application
  • config
  • pageContext
  • page
  • exception

Scope of jsp varibles

  • application
  • session
  • request
  • page

How to deal with exception in JSP by:

  • using page directive
  • using deployment descriptor (web.xml) file

OS : Windows 10
Jdk : Version 8
IDE : Eclipse Mars
Server : Apache Tomcat 7

Follow me @
https://raviroza.wordpress.com/
https://raviroza.blogspot.com/
https://www.facebook.com/ravi.oza.it
https://twitter.com/raviozaIT

Subscribe my channel to get latest video notification https://www.youtube.com/user/ravioza101

Watch “JSP Part-9 | Use Bean Example-3 (Gujarati)” on YouTube

#Java #JSP #jsp:useBean #RaviROza

jsp:use bean tag is used to declare and used object using tag format in jsp page.

Bean object is declared using use:bean tag in jsp page. set:property tag is used to set the values and get:property tag used to get the values from bean object.

Scope for jsp:bean is set to request so that when a request is transfer to another page its also accessible there.

following is the list of important attributes of jsp:usebean tag
➡ id (name of the object)
➡ name (name of the fully qualified class name of bean)
➡ scope (application,request,session and page), default is page.

In this example bean values are set in process.jsp and then its transferred to welcome.jsp using request dispatcher. Welcome.jsp access the values get the values which were set in process.jsp page.

Summary of files used in this example
➡️ index.jsp (to accept client input)
➡️ process.jsp (to declare and set the values in bean)
➡️ UserBean.java (user bean POJO file)
➡️ welcome.jsp (welcome page to display)

Index.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 Static Login using Bean</title>
</head>
<body>

<form action="process.jsp">

Username : <input type="text" name="txtUser">
Password :  <input type="password" name="txtPass">
<input type="submit">
</form>

</body>
</html>

process.jsp

<%@page import="com.raviroza.beans.UserBean"%>
<%@ 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 : Using Beans</title>
</head>
<body>

<jsp:useBean id="hjd" class="com.raviroza.beans.UserBean" scope="request">
</jsp:useBean>

<jsp:setProperty 
property="username" name="hjd" 	value='<%=request.getParameter("txtUser") %>'/>
	
<jsp:setProperty 	
property="password" name="hjd"	value='<%=request.getParameter("txtPass") %>' />
	
<%
	RequestDispatcher rd = request.getRequestDispatcher("welcome.jsp");
 	rd.forward(request, response);
%>
</body>
</html>

UserBean.java

package com.raviroza.beans;

public class UserBean implements java.io.Serializable 
{
	private String username,password;

	public String getUsername() {
		return username;
	}

	public void setUsername(String username) {
		this.username = username;
	}

	public String getPassword() {
		return password;
	}

	public void setPassword(String password) {
		this.password = password;
	}	
	public boolean validate()
	{
		if(username.equals("admin") && password.equals("admin"))
		{
			return true;
		}
		else 
		{
			return false;
		}
	}
}

welcome.jsp

<%@page import="com.raviroza.beans.UserBean"%>
<%@ 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>Welcome</title>
</head>
<body>
<h1>Welcome Page</h1>

<jsp:useBean id="hjd" class="com.raviroza.beans.UserBean" scope="request">
</jsp:useBean>

Hello,
<p>User name : <jsp:getProperty property="username" name="hjd"/>

<p>Password : <jsp:getProperty property="password" name="hjd"/>

</body>
</html>

Output

OS : Windows 10
Jdk : Version 8
IDE : Eclipse Mars
Server : Apache Tomcat 7

Follow me @
https://raviroza.wordpress.com/
https://raviroza.blogspot.com/
https://www.facebook.com/ravi.oza.it
https://twitter.com/raviozaIT

Subscribe my channel to get latest video notification https://www.youtube.com/user/ravioza101

Watch “JSP Part-8 | Use Bean Example-2 (Gujarati)” on YouTube

#Java #JSP #jsp:useBean #RaviROza

It contains the following files
👉 Index.jsp
👉 load.jsp
👉 StudentBean.java
👉 welcome.jsp

Index.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>use Bean Example</title>
</head>
<body>

<form action="load.jsp">
	<p>Enter Student No
	<input type="text" name="t1">
	<p>Enter Student Name
	<input type="text" name="t2">	
	<p>
	<input type="submit">
</form>
</body>
</html>

load.jsp

<%@page import="raviroza.StudentBean"%>
<%@ 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">
</head>
<body>
	<%
		int 	 id  = Integer.parseInt(request.getParameter("t1"));
		String name = request.getParameter("t2");
		
 		StudentBean st1 = new StudentBean();
 		st1.setSid(id);
 		st1.setSname(name);
		
// 		request.setAttribute("p1", id);
// 		request.setAttribute("p2", name);
		
// 		out.println("<p>"+st1.getSid());
// 		out.println("<p>"+st1.getSname());
		
		request.setAttribute("st1", st1);
		
		RequestDispatcher rd = request.getRequestDispatcher("welcome.jsp");
		rd.forward(request, response);
		//response.sendRedirect("welcome.jsp");		
	%>	
</body>
</html>

StudentBean.java

package raviroza;

public class StudentBean implements java.io.Serializable 
{
	private int Sid;
	private String Sname;
	
	public int getSid() {
		return Sid;
	}
	public void setSid(int sid) {
		Sid = sid;
	}
	
	public String getSname() {
		return Sname;
	}
	public void setSname(String sname) {
		Sname = sname;
	}			
}

welcome.jsp

<%@page import="raviroza.StudentBean"%>
<%@ 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>welcome</title>
</head>
<body>
<h1> Welcome to the home page</h1>
<%
	StudentBean st = new StudentBean();
	st = (StudentBean) request.getAttribute("st1");
	
	out.println(st.getSid());
	out.println(st.getSname());
%>

<%-- <%=request.getAttribute("p1").toString() %> --%>
<%-- <%=request.getAttribute("p2").toString() %> --%>
</body>
</html>

OS : Windows 10
Jdk : Version 8
IDE : Eclipse Mars
Server : Apache Tomcat 7

Follow me @
https://raviroza.wordpress.com/
https://raviroza.blogspot.com/
https://www.facebook.com/ravi.oza.it
https://twitter.com/raviozaIT

Subscribe my channel to get latest video notification https://www.youtube.com/user/ravioza101

Watch “JSP Part-7 | Use Bean Example-1 (Gujarati)” on YouTube

#Java #JSP #jsp:useBean #RaviROza

✔️ jsp:useBean standard action tag is use to establish a connection between a jsp page and a java bean.
✔️ In web applications of java, jsp and java bean communication is required in the following two cases: 1️⃣In a real-time MVC project, a model class (business class) will set the data to a java bean and a jsp (view) will read the data from a bean and finally displays it on the browser. 2️⃣If multiple jsp pages need common java logic then it separates that java code into a bean and then we call the bean from jsp.

Video features the following:
👉 How to define Bean class in java
👉 How to use bean class in JSP scriplet
👉 How to use bean using jsp:useBean

It contains the following files
👉 Index.jsp 👉 process.jsp 👉 UserBean.java

Index.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 Static Login using Bean</title>
</head>
<body>

<form action="process.jsp">

Username : <input type="text" name="txtUser">
Password :  <input type="password" name="txtPass">
<input type="submit">
</form>

</body>
</html>

process.jsp

<%@page import="com.raviroza.beans.UserBean"%>
<%@ 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 : Using Beans</title>
</head>
<body>
<p>Using JSP:useBean tag
<%
// 	String u = request.getParameter("txtUser");
// 	String p = request.getParameter("txtPass");
	
// 	UserBean user = new UserBean();
// 	user.setUsername(u);
// 	user.setPassword(p);
	%>
<jsp:useBean id="userobj" class="com.raviroza.beans.UserBean"/>
	
<jsp:setProperty property="username" name="userobj" value="ravi r oza"/>
<jsp:setProperty property="password" name="userobj" value="my password"/>

<h2>using get property</h2>
<jsp:getProperty property="username" name="userobj"/>

</body>
</html>

UserBean.java

package com.raviroza.beans;

public class UserBean implements java.io.Serializable 
{
	private String username,password;

	public String getUsername() {
		return username;
	}

	public void setUsername(String username) {
		this.username = username;
	}

	public String getPassword() {
		return password;
	}

	public void setPassword(String password) {
		this.password = password;
	}	
	public boolean validate()
	{
		if(username.equals("admin") && password.equals("admin"))
		{
			return true;
		}
		else 
		{
			return false;
		}
	}
}

OS : Windows 10
Jdk : Version 8
IDE : Eclipse Mars
Server : Apache Tomcat 7

Follow me @
https://raviroza.wordpress.com/
https://raviroza.blogspot.com/
https://www.facebook.com/ravi.oza.it
https://twitter.com/raviozaIT

Subscribe my channel to get latest video notification https://www.youtube.com/user/ravioza101

Watch “JSP Part-6 | Action Tag with example (Gujarati)” on YouTube

Jdk #Java #JSP #JavaServerPages #Scriplet #JSPActionTags #Gujarati #RaviROza #JspInclude #JSPForward

Video features the following:

  • usage of Action tag
  • jsp:include
  • jsp-forward
  • jsp-param
  • Actions tags are used to do things without using Java inside the scriptlets.
  • They are used to control the flow between pages and to use Java Bean.
  • They can be used for specific task such as including other resource, forward the request to other resource (html/jsp/servlet), working with java bean objects

Following are the list of files used in example

  • header.jsp
  • index.jsp
  • page1.jsp
  • page2.jsp

header.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>Header</title>
</head>
<body>
<center>
<h1> Ravi R. Oza </h1>
<h4> Home Page </h4>
<hr>
</center>
</body>
</html>
index.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 Action Tag Demo</title>
</head>
<body>
<jsp:include page="header.jsp"></jsp:include>
<form action="page1.jsp">
<h1>JSP Action Tag Demo</h1>
<hr>
	<p> Enter your name 
		<input type="text" name="txtName">
	</p>
	<p>
		<input type="submit" value="Go to Page1">
	</p>

</form>
</body>
</html>
page1.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>Page1</title>
</head>
<body>

<jsp:forward page="page2.jsp">
	<jsp:param value="Advance Java" name="txtSub"/>
</jsp:forward>

</body>
</html>
page2.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>Testing Action tags with JSP Param</title>
</head>
<body>
<jsp:include page="header.jsp"></jsp:include>
<h1>Testing Action tags with JSP Param</h1>
<hr>
<p> Name : <%=request.getParameter("txtName") %>
<p> Subject : <%= request.getParameter("txtSub") %>
</body>
</html>

Summary:

  • Actions tags are used to do things without using Java inside the scriptlets.
  • They are used to control the flow between pages and to use Java Bean.
  • They can be used for specific task such as including other resource, forward the request to other resource (html/jsp/servlet), working with java bean objects

OS : Windows 10
Jdk : Version 8
IDE : Eclipse Mars
Server : Apache Tomcat 7

Follow me @
https://raviroza.wordpress.com/
https://raviroza.blogspot.com/
https://www.facebook.com/ravi.oza.it
https://twitter.com/raviozaIT

Subscribe my channel to get latest video notification https://www.youtube.com/user/ravioza101

Watch “JSP Part-5 | Page Directive attributes example (Gujarati)” on YouTube

Jdk #Java #JSP #JavaServerPages #Scriplet #PageDirectiveAttributes #Gujarati #RaviROza

Video features the following:

  • usage of page attributes in JSP

Following are the list of files used in example

  • index.jsp
  • myerror.jsp
index.jsp
<%@ page 	
	language="java" 
	contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"  
    errorPage="/myerror.jsp"         
    %>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>JSP Page Directives</title>
</head>
<body>
<h1>JSP Page Directives Demo</h1>
<h3>Ravi R. Oza</h3>
<hr/>

<%
	String name = null;
	int n = name.length();
        //   or try below
	//int n=100/0;
%>
</body>
</html>
myerror.jsp
<%@ page isErrorPage="true" %>

<h1> OOPS, There is an Error</h1>

<h2> Please check the input or wait for server to response.</h2>
<font color="red">
 
<%= exception %>
</font>

Summary

  • It is used to provide instructions to a container that connect to current JSP page.
  • It defines page dependent properties/attributes such as scripting language, error page, etc., which communicates with the Web Container at the time of page translation.

OS : Windows 10
Jdk : Version 8
IDE : Eclipse Mars
Server : Apache Tomcat 7

Follow me @
https://raviroza.wordpress.com/
https://raviroza.blogspot.com/
https://www.facebook.com/ravi.oza.it
https://twitter.com/raviozaIT

Subscribe my channel to get latest video notification https://www.youtube.com/user/ravioza101

Watch “JSP-4 | Include Directive example (Gujarati)” on YouTube

Jdk #Java #JSP #JavaServerPages #Scriplet #IncludeDirectiveTag #IncludeDirective #Gujarati #RaviROza

Video features the following:

  • Usage of Include Directive tag
  • Inclusion of any java/html/jsp resource in jsp file

Following are the list of files used in example

  • index.jsp
  • header.html
  • footer.html
  • search.jsp
  • about.jsp
  • contact.jsp
index.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 Tag Example</title>
</head>
<body>
<%@ include file="header.html" %>

<h2>Content of index page is this </h2>
<p> <p> <p> <p> <p> <p> <p> <p> 



<%@ include file="footer.html" %>
</body>
</html>
header.html
<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
<title>header</title>
</head>
<body>

<h1 align="center">Ravi R. Oza (header)</h1>
<p align="right">
<a href="index.jsp"> Home </a> |
<a href="search.jsp"> Search </a> | 
<a href="contact.jsp"> Contact </a>|
<a href="about.jsp"> About </a>
<hr/>
</p> 
</body>
</html>
footer.html
<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
<title>footer</title>
</head>
<body>

<hr/>
<p align="center">

<h3>2020 Copyrights reserved to RaviROza (footer)</h3>

</p>

</body>
</html>
search.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 charset="ISO-8859-1">
<title>search</title>
</head>
<body>
<%@ include file="header.html" %>

<h2> SEARCH  </h2>
Search : <input type="text">
<p> <p> <p> <p> <p> <p> <p> <p> 

<%@ include file="footer.html" %>
</body>
</html>
about.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 charset="ISO-8859-1">
<title>contact</title>
</head>
<body>
<%@ include file="header.html" %>

<h3>About me  </h3>
<h2> Myself Ravi R. Oza </h2>
<h4> Myself Educator, Blogger, YouTuber, Learner </h4> 

<p> <p> <p> <p> <p> <p> <p> <p> 

<%@ include file="footer.html" %>
</body>
</html>
contact.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 charset="ISO-8859-1">
<title>contact</title>
</head>
<body>
<%@ include file="header.html" %>

<h2>Contact me  </h2>
<p> Mail me : Ravi.oza.it@gmail.com
<p> Follow me on Youtube @RaviROza
<p> <p> <p> <p> <p> <p> <p> <p> 

<%@ include file="footer.html" %>
</body>
</html>

In this example index page used as home page.
Header.jsp contains the header information such as website name, logo & menus.
Footer.jsp contains the footer information such as quick links, company’s policies etc.,
Search.jsp is used to find the information from website.
About.jsp is the page which holds the info related to company history & its objective past and future work
Contact.jsp is the contact page which is useful when client want to communicate with the company.

Summary:
JSP Include Directive is the best way to have reuseability, where a the content of one page or resources can be merged to existing Jsp

OS : Windows 10
Jdk : Version 8
IDE : Eclipse Mars
Server : Apache Tomcat 7

Follow me @
https://raviroza.wordpress.com/
https://twitter.com/raviozaIT
https://www.facebook.com/ravi.oza.it

Subscribe my channel to get latest video notification https://www.youtube.com/user/ravioza101

Watch “JSP Part-3 | Declaration tag example (Gujarati)” on YouTube

Jdk #Java #JSP #JavaServerPages #Scriplet #DeclarationTag #JSPDeclarationTag #Delimiters #Gujarati #RaviROza

Video features the combination of following

  • Scriplet tag example in JSP
  • Expression tag example in JSP
  • Comment tag example in JSP

If we declare them in declaration tag, both variables, objects & methods, they will be accessible throughout that page. It is used declare public objects or methods for the current page, every scriplet in current page can access it.

OS : Windows 10
Jdk : Version 8
IDE : Eclipse Mars
Server : Apache Tomcat 7

follow me @
https://raviroza.wordpress.com/
https://twitter.com/raviozaIT
https://www.facebook.com/ravi.oza.it

Subscribe my channel to get latest video notification https://www.youtube.com/user/ravioza101

Watch “JSP Part-1 | Java Server Pages – Introduction (Gujarati)” on YouTube

Jdk #Java #JSP #JavaServerPages #Scriplet #Delimiters #Gujarati #RaviROza

JSP is a server side scripting language
It is based on java language and enable the development of dynamic web page.

JSP files are HTML files with special tag containing java source code that provide dynamic content.

It is used to create web app, focuses more on presentation logic.

JSP is easier to maintain than a Servlet.
Servlet adds HTML code inside Java code, while JSP adds Java code inside HTML.

OS : Windows 10
Jdk : Version 8

follow me @
https://raviroza.wordpress.com/
https://twitter.com/raviozaIT
https://www.facebook.com/ravi.oza.it

Subscribe my channel to get latest video notification https://www.youtube.com/user/ravioza101