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

Leave a Comment

%d bloggers like this: