This is a discussion on How can we use beans in JSP? within the Java Server Pages (JSP) forums, part of the Web Development category; How can we use beans in JSP?...
| |||||||
| Register | FAQ | Members List | Calendar | Mark Forums Read |
| |||
| JSP provides three tags to work with beans:- <jsp:useBean id=“bean name” class=“bean class” scope = “page | request | session|application ”/> Bean name = the name that refers to the bean. Bean class = name of the java class that defines the bean. <jsp:setProperty name = “id” property = “someProperty” value = “someValue” /> id = the name of the bean as specified in the useBean tag. property = name of the property to be passed to the bean. value = value of that particular property . <jsp:getProperty name = “id” property = “someProperty” /> Here the property is the name of the property whose value is to be obtained from thebean.Below is a code snippet which shows how MyUserClass is used and the valuesaccessed. <jsp:useBean id="user" class="MyUserClass" scope="session"/> <HTML> <BODY>You entered <BR>Name: <%= user.getUsername() %><BR>Email: <%= user.getEmail() %><BR></BODY></HTML> |
| |||
| Jsp provides as approach to utilizing javabeans that is based on the concept of tags. These are really no more complicated than the standard html tags; they have a name and they take attributes. There are three tags provided by the specification to support the use of javabeans in your jsp pages: 1. <jsp:usebean> 2.<jsp:setproperty> 3.<jsp:getproperty>. Example : Javabean class. import java.io.Serializable; public class CarBean implements Serializable { private String make = "Ford"; public CarBean() {} public String getMake() { return make; } public void setMake(String make) { this.make = make; } } Jsp page: <html> <head> <title> Using a bean</title> </head> <body> <jsp:usebean id = "MyCar: Class = "CarBean"/> I have a <jsp:getproperty name = "MyCar" property = "make" /> <jsp:setproperty name = "MyCar" property = "make" value = "Ferrari" /> Now I have a <jsp:getproperty name = "MyCar" property = "make" /> </body> </html> output: -------------- Using a bean i have a Ford Now i have a Ferrari |
| |||
| In jsp eacy to use java beans. now i just explained a JSP exaple that uses a Java Beans for login steps. 1. Create a login.html page 2. Create a loginaction.jsp page 3. create a login.java page login.html --------- <html> <head> <title>Login Page</title> </head> <body> <form action="loginaction.jsp" method="post"> <table width="38%" border="1"> <tr> <td width="38%">User Name</td> <td width="62%"><input name="uname" type="text" id="uname"></td> </tr> <tr> <td>Password</td> <td><input name="pass" type="text" id="pass"></td> </tr> <tr> <td><input type="submit" name="Submit" value="Submit"></td> <td> </td> </tr> </table> </form> </body> </html> login.java Rule ---- ompile this file and copy the class file to WEB-INF/classes/your_package_name/your_class_file in our example ------------ WEB-INF/classes/login/Login.class package login; // Java Document public class Login { /* BEAN PROPERTIES These values must match with login.html form text fields names */ private String uname; private String pass; public Login() { } /* Now write setter and getter method just add set or get before the BEAN PROPERTIES */ public void setUname(String uname) { this.uname=uname; } public String getUname() { return uname; } public void setPass(String pass) { this.pass=pass; } public String getPass() { return pass; } public boolean validate() { if((uname.equals("kamal") && (pass.equals("kannan")) { return true; } else { return false; } } } loginaction.jsp -------------- <%@ page contentType="text/html; charset=" language="java" import="java.sql.*" errorPage="" %> <%@page import="login.Login"%> <jsp:useBean id="login" scope="request" type="login.Login"/> <%-- This is a comment --%> <%-- Sets all form values to the BEAN PROPERTY --%> <%-- This only works if your BEAN PROPERTY names and FORm NAMES are mactch --%> <jsp:setProperty property="*"/> <html> <head> <title>Untitled Document</title> <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1"> </head> <body> <% if(login.validate()) { Welcome ! %> <jsp:getProperty property="uname"/> <% } else { out.print("Invalid User"); } %> </body> </html> |
![]() |
| Thread Tools | |
| Display Modes | |
| |
Similar Threads | ||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| integerate Java Beans in java application | saravanan | Java Programming | 1 | 03-26-2008 01:22 AM |
| is there any visual tool or netbeans plugin available to build JAVA BEANS? | saravanan | Java Server Pages (JSP) | 0 | 03-19-2008 09:08 PM |
| Can anybody explain in brief Life cycle for Stateless and Stateful beans? | oxygen | Java Programming | 1 | 07-26-2007 03:49 AM |