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