Wednesday 5 May 2010

Develop a web application for registration form using Beans (java beans) in a JSP

                                                                Declaring a Bean in a JSP:

follow these steps to declare a bean in a JSP:
1. create a bean (java program)
2. Declare the bean in a JSP by using the <jsp:useBean> tag
3. Access the Bean properties
4. Generate Dynamic Content
5. Deploy and run the application

1. create a bean (java program)
Before declaring a Bean in a JSP , you must create a Bean:(java program)
/* creating Bean (java beans) called RegForm.java
 Before declaring a Bean in a JSP , we must create a Bean
 * */
package com.rajendra.jsp.beans;

import java.io.Serializable;

public class RegBean implements Serializable 
{
 private String uname,pass,repass,email,fn,ln,address;
 public void setUserName(String s)
 {
uname=s;
 }
 public void setPassword(String s)
 {
pass=s;
 }
 public void setRePassword(String s)
 {
repass=s;
 }
 public void setEmail(String s)
 {
email=s;
 }
 public void setFirstName(String s)
 {
fn=s;
 }
 public void setLastName(String s)
 {
ln=s;
 }
 public void setAddress(String s)
 {
address=s;
 }
 public String getUserName()
 {
return(uname);
 }
 public String getPassword()
 {
return(pass);
 }
 public String getRePassword()
 {
return(repass);
 }
 public String getEmail()
 {
return(fn); 
 }
 public String getFirstName()
 {
return(fn);
 }
 public String getLastName()
 {
return(ln);
 }
 public String getAddress()
 {
return(address);
 }
}//end of the class RegBean

2. Declare the bean in a JSP by using the <jsp:useBean> tag



  • After creating the Bean, you can declare it in a JSP page by using <jsp:useBean> tag, to declare this Bean in a JSP, You have to create a JSP

<%@ page errorPage="Registration.html"%>
<html>
 <body>
  <jsp:useBean id="regform" class="com.rajendra.jsp.beans.RegBean" scope="session"/>
  /*this is the jsp action tag used to declare bean*/
  <jsp:setProperty name="regform" property="*"/>
  <form action="RegProcessFinal.jsp" method="get">
  <pre>
  <b>
  First Name  :<input type="text" name="first_name"/>
  Last Name   :<input type="text" name="last_name"/>
  Address     :<input type="text" name="address"/>
  
  <input type="submit" value="Register" />
  </b>
  </pre>
  </form>b
 </body>
</html>


  • The <jsp:useBean> action tag creates an instance (object) of a bean class (RegBean.java) according to the attributes specified in the JSP page.
  • id: specifies the name of the Bean class, which helps the jsp container in searching the Bean class and creating its instance.
  • class: specifies name of the Bean class, which helps the jsp container in searching the Bean class and creating its instance
  • scope: specifies the scope of the bean
All these attributes help JSP Container to create an instance of the Bean class. The <jsp:useBean> action tag has some more attributes along with the attributes used in. You can also specify these attributes to make the Bean more specific.

3. Access the Bean properties



  • you can verify the bean properties by using the <jsp:getProperty> action tag. The <jsp:getProperty> tag  uses the name and property tags to read the Bean properties.

  • we have created the ViewRegistrationDetails.jsp page in this application to tells how the <jsp:getProperty> action tag is used to read the Bean property.
<%@ 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">
<jsp:useBean id="regform" type="com.rajendra.jsp.beans.RegBean" scope="session"/>
<%@page errorPage="Registration.html"%>
<html>
 <body>
 <pre>
 <b> User Name  : </b> <jsp:getProperty name="regform" property="userName"/>
 <b> Password   : </b> <jsp:getProperty name="regform" property="password"/>
 <b>Email ID    : </b> <jsp:getProperty name="regform" property="email"/>
 <b>First Name  : </b> <jsp:getProperty name="regform" property="firstName"/>
 <b>Last Name   : </b> <jsp:getProperty name="regform" property="lastName"/>
 <b>Address     : </b> <jsp:getProperty name="regform" property="address"/>
 
 </pre>
 <form action="javascript:alert('the remaining process is under construction');">
  <input type="submit" value="Register"/>
 </form>
 </body>
</html>
  • before reading the Bean properties, the JSP container needs to locate the instance of the Bean class (RegBean.java) created
  • After JSP Container locates the object of the Bean class, it starts reading the Bean properties by using the <jsp:getProperty> tag
<jsp:getProperty> tag attributes are: 
name: attribute specifies the name of the Bean class to the JSP container and the property attribute specific the Bean properties that have to be read

4. Generate Dynamic Content

  • now we need to create the html page Registration.html, to retrieve the required data from users.
  • this html page contains a button called Register, when a user clicks the Register button after providing all user details, that user is directed to the RegProcess.jsp page to set the properties of "RegBean" java Bean, based on the details entered by the user
  • in other words, we use the Registration.html page to generate dynamic content in the RegProcess.jsp page

<html>
 <body>
  <pre>
  <form action="RegProcess.jsp" method="post"/>
  
  <b>
   UserName  : <input type="text" name="userName"/>
   Password  : <input type="password" name="password"/>
   RePassword: <input type="password" name="rePassword"/>
   Email ID  : <input type="text" name="email"/>
   
   <input type="submit" value="Register"/>
   
  </b></pre></form>
 </body>
</html>



  •  In the Registration.html page, the param names such as userName, password, and rePassword are the same as the property names in the RegForm bean
  • the other param names, such as first_name and last_name, are different from the property names such as firstName and lastName. Therefore, the RegProcess.jsp page is created to set these properties in the RegBean.
RegProcessFinal.jsp page also collects some more information about the user in the RegBean java bean in addition to what we have collected by using the Registration.html page. Here Register button is provided, when you click on that button after entering all user information, you are redirected to the RegProcessFinal.jsp page:


<%@page errorPage="Registration.html"%>
<jsp:useBean id="regform" type="com.rajendra.jsp.beans.RegBean" scope="session"/>
<jsp:setProperty name="regform" property="firstName" param="fist_name"/>
<jsp:setProperty name="regform" property="lastName" param="last_name"/>
<jsp:setProperty name="regform" property="address" param="address"/>

<html>
 <body>
  <pre>
   Your registration details are correct <a href="ViewRegistrationDetails.jsp">Click</a>
   to view Registration Details and Confirm
  </pre>
 </body>
</html>



                                                                   web.xml file (this is optional)
<web-app>
<display-name>DeclareBean</display-name>
<welcome-file-list>
<welcome-file>Registration.html</welcome-file>
</welcome-file-list>
</web-app>

                                                   5. Deploy and run the application



  • maintain the folder structure
  • Here my folder name is DeclareBean(project name), copy this folder and place into the <tomcat-home>/webapps folder and start the Tomcat Server:













What is java beans? and its advantages

Jsp Action Tags :

       <jsp:setProperty>

Develop a web application to add or sub of two numbers using jsp:forward and jsp:param tags

Example program to illustrate about jsp:forward and jsp:param tags:
Develop a web application to add or sub of two numbers using jsp:forward and jsp:param tags

Steps:
Maintain the folder structure:


Home.html

<html>
 <head>
  <title>ActionTags
  </title>
 </head>
 <body>
  <pre>
   <form action="FronJSP.jsp" method="get">
    <b>Enter value of A </b><input type="text" name="field1"/>
    <b>Enter value of B</b><input type="text" name="field2"/>
    <input type="submit" name="submit" value="Add"/>
    <input type="submit" name="submit" value="Sub"/>
   </form>
  </pre>
 </body>
</html>


FronJSP.jsp

<%@page errorPage="/Home.html"%>

<%
  String s1=request.getParameter("field1");
  String s2=request.getParameter("field2");
  Integer.parseInt(s1);
  Integer.parseInt(s2);
  String s3=request.getParameter("submit");
  if(s3.equals("Add"))
  {  
 %>
 <jsp:forward page="/AddJsp.jsp"/>
 <%
  } 
  else if(s3.equals("Sub"))
  {  
 %>
<jsp:forward page="/SubJsp.jsp"/>
<%
 } 
  else
  {%>
<jsp:forward page="/Home.html"/>  
 
  <% }%>

//AddJsp.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">
<%
 int a=Integer.parseInt(request.getParameter("field1"));
 int b=Integer.parseInt(request.getParameter("field2"));
 int result=a+b;
 %>
 <jsp:forward page="/Result.jsp"> 
 <jsp:param value="<%=result%>" name="output"/>
 </jsp:forward>

SubJsp.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">
<%
 int a=Integer.parseInt(request.getParameter("field1"));
 int b=Integer.parseInt(request.getParameter("field2"));
 int result=a-b;
 %>
 <jsp:forward page="/Result.jsp">
 <jsp:param name="output" value="<%=result%>"></jsp:param>
 </jsp:forward>

Result.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">
<% 
 int a=Integer.parseInt(request.getParameter("field1"));
 int b=Integer.parseInt(request.getParameter("field2"));
 int result=a-b;
 %>
 <jsp:forward page="/Result.jsp">
 <jsp:param name="output" value="<%=result%>"></jsp:param>
 </jsp:forward>
Go to WEB-INF folder, create web.xml file (this is optional)
web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app id="WebApp_ID" version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
<display-name>ActionTag</display-name>
<welcome-file-list>
<welcome-file>Home.html</welcome-file>

</welcome-file-list>
</web-app>





Ho
Jsp Action Tags:



Tuesday 4 May 2010

What is java beans? and its advantages

Java Beans are reusable software components that separate the business logic from the presentation logic. In general, JavaBeans are simple java classes that follow certain specifications to develop dynamic content. Java Beans are required to create effective and dynamic Web pages by providing the separate Java classes instead of embedding large amount of java code directly in a JSP page. These separate java Bean classes are easier to write, compile, test, debug, and reuse. Java Bean uses getter and setter methods to invoke various methods explicitly  to use their functionality with jsp pages.
·         Java Beans components are also known as Beans.
·         Java beans can be created or manipulated by using Java-Beans-compliant application builder tools such as NetBeans IDE.

·         The Java Beans-compliant application builder tools automatically discover information about the classes, based on Java Beans specifications, and can create and manipulate these classes.


1.       Advantages of java beans in jsp
·         Developers can manage presentation code and business logic separately. By using this features big companies have separate Web and Java development teams. Both the presentation logic and business logic are developed separately, and Java Beans ensure proper communication between them.
·         Object sharing can be done between web pages simple.
We can simplify the process of request and response handling