Thursday, May 27, 2010

JSP Example:Scriptless

Going straight from the request to the JSP without going through a servlet...

Case1: Using param Attribute

STEP1: Create a “dynamic web” project named “FirstBean”.

STEP 2: Create a new package named” com.examp” under Src or Source Folder.

STEP 3: Create a new html file named “SampleHtml.html” under WebContent folder.

< html >
< head >
< title >Entry Page< /title >
< /head >

< body >
< form action="TestBean.jsp" >
name: < input type="text" name="Username" >
ID#: < input type="text" name="UserID" >
< input type="submit" >
< /form >
< /body >
< /html >

STEP 4: Create a new java class named “Person.java” under Src folder.

package com.examp;

public abstract class Person {
private String name;
public void setName(String name) {
this.name=name;
}
public String getName() {
return name;
}}

STEP 5: Create a new java class named “Employee.java” under Src folder.

package com.examp;

public class Employee extends Person {
private int empID;
public void setEmpID(int empID) {
this.empID = empID;
}
public int getEmpID() {
return empID;
} }

STEP 6: Create a new JSP file named “TestBean.jsp” under WebContent folder.

< body  >
< jsp:useBean id="person" type="com.examp.Person" class="com.examp.Employee" >
< jsp:setProperty name="person" property="name" param="Username" / >
< jsp:setProperty name="person" property="empID" param="UserID" / >
< /jsp:useBean >

Person is:
< jsp:getProperty name="person" property="name" / >
ID is :
< jsp:getProperty name="person" property="empID" / >
< /body >

STEP7: Include below code in a Xml file “web.xml” under WEB-INF folder.

< welcome-file-list >
< welcome-file >SampleHtml.html< /welcome-file >
< welcome-file >TestBean.jsp< /welcome-file >
< /welcome-file-list >

STEP 8 Export the project” FirstBean” into a war file named ” FirstBean.war” and place it in the deploy folder of JBOSS server.

STEP 9:To see the output use this url ” http://localhost:8080/ FirstBean / SampleHtml.html ” where FirstBean is the .war file name and SampleHtml.html is specified in the of web.xmlfile.

Step 10: Sample output is

Person is: Ammu ID is : 1

Bean tags convert primitive properties automatically

 
Case2 :With out using param attribute

We need to do following modifications
1: SampleHtml.html:-change the entire request parameter names match the bean property names.

< body >
< form action="TestBean.jsp" >
name: < input type="text" name="name" >
ID#: < input type="text" name="empID" >
< input type="submit" >
< /form >
< /body >

2: TestBean.jsp:-Modify the type as Employee instead of Person

< body >
< jsp:useBean id="person" type="com.examp.Employee" class="com.examp.Employee" >
< jsp:setProperty name="person" property="*" / >
< /jsp:useBean >
Person is:
< jsp:getProperty name="person" property="name" / >
ID is :
< jsp:getProperty name="person" property="empID" / >
< /body >

1 comment:

  1. It's really a nice and helpful piece of information.
    I'm satisfied that you simply shared this helpful info
    with us. Please stay us up to date like this.
    Thank you for sharing.

    Here is my blog :: hostgator 1 cent coupon code

    ReplyDelete