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

Leave a Comment