Pin It

How to use Jsp action elements useBean, setProperty and getProperty to add, set and get property of a bean in a JSP page ?.

A simple Web-Application showing how to use action jsp action elements useBean, setProperty and getProperty to add, set and get property of a bean in a JSP page


Create a simple JavaBean by name User.java

package com.hubberspot.bean;


public class User {

 private String firstName;
 private String lastName;
 private String email;
 private String password;

 public User() {

 }

 public String getFirstName() {
  return firstName;
 }

 public void setFirstName(String firstName) {
  this.firstName = firstName;
 }

 public String getLastName() {
  return lastName;
 }

 public void setLastName(String lastName) {
  this.lastName = lastName;
 }

 public String getEmail() {
  return email;
 }

 public void setEmail(String email) {
  this.email = email;
 }

 public String getPassword() {
  return password;
 }

 public void setPassword(String password) {
  this.password = password;
 }

}



Create style.css file

 body {
  font-family: Verdana, Geneva, Arial, helvetica, sans-serif;
  background-color: #FDF4DF;
  font-size: 14px;
  color: #333333;
 }
 
 td {
  font-family: Verdana, Geneva, Arial, helvetica, sans-serif;
  font-size: 14px;
 }
 
 .small {
  font-family: Verdana, Arial, Helvetica, sans-serif;
  font-size: 12px;
  font-weight: bold;
 }



Create user.jsp file



1.
2.
3.
4.
5.
6.
7.
8.
9.
10.
11.
12.
13.
14.
15.
16.
17.
18.
19.
20.
21.
22.
23.
24.
25.
26.
27.
28.
29.
30.
31.
32.
33.
34.
35.
36.
37.
38.
39.
40.
41.

<%@ 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>User Details !!!</title>
<link href="style.css" rel="stylesheet" type="text/css">
</head>
<body>
    <form method="post" action="done.jsp">
        <p>&nbsp;</p>
        <p class="small" align="left">Enter Your Details !!!</p>
        <table align="left" bgcolor="#728DCF">
            <tr>
                <td class="small">First Name</td>
                <td><input name="firstName" size=25 class="small"></td>
            </tr>
            <tr>
                <td class="small">Last Name</td>
                <td><input name="lastName" size=25 class="small"></td>
            </tr>
            <tr>
                <td class="small">Email</td>
                <td><input name="email" size=25 class="small"></td>
            </tr>
            <tr>
                <td class="small">Password</td>
                <td><input name=password type="password" size=25 class="small"></td>
            </tr>

            <tr>
                <td colspan="2" align="center"><input type="submit"
                    value="Update" /></td>
            </tr>
        </table>
    </form>
</body>
</html>


Create done.jsp file



1.
2.
3.
4.
5.
6.
7.
8.
9.
10.
11.
12.
13.
14.
15.
16.
17.
18.
19.
20.
21.
22.
23.
24.
25.
26.
27.
28.
29.
30.
31.
32.
33.
34.
35.
36.
37.
38.
39.
40.
41.
42.

<%@ 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>User Confirm !!!</title>
<link href="style.css" rel="stylesheet" type="text/css">
</head>
<body>
    <p>&nbsp;</p>
    <p class="small" align="left">Details You Entered !!!</p>

    <jsp:useBean id="user" scope="request" class="com.hubberspot.bean.User">
        <jsp:setProperty property="firstName" name="user" />
        <jsp:setProperty property="lastName" name="user" />
        <jsp:setProperty property="email" name="user" />
        <jsp:setProperty property="password" name="user" />
    </jsp:useBean>
    <table align="left" bgcolor="#728DCF">
        <tr>
            <td class="small">First Name -</td>
            <td><jsp:getProperty name="user" property="firstName" /></td>
        </tr>
        <tr>
            <td class="small">Last Name -</td>
            <td><jsp:getProperty name="user" property="lastName" /></td>
        </tr>
        <tr>
            <td class="small">Email -</td>
            <td><jsp:getProperty name="user" property="email" /></td>
        </tr>
        <tr>
            <td class="small">Password -</td>
            <td><jsp:getProperty name="user" property="password" /></td>
        </tr>
    </table>

</body>
</html>

After running JSP's on browser :






















 On Updating the User details :




Please Give Us Your 1 Minute In Sharing This Post!
SOCIALIZE IT →
FOLLOW US →
SHARE IT →
Related Posts Plugin for WordPress, Blogger...

Post your related queries below ...

comments powered by Disqus
Powered by Blogger.
 
© 2012 Learn Java by Examples Template by Hubber Spot