Practical example of external objects in jsp

In this post, you will learn:

  • How to create external objects in JSP?
  • How to set properties of external objects in JSP?
  • How to obtain properties of external objects in JSP?

Practical use of external objects in JSP:

To demonstrate the use of external objects with the help of useBean, setProperty & getProperty actions, I have created a simple web application. Following diagram describes the flow of application:

jsp_useBean_page_flow

Description of the diagram:

1.0 – A request is sent to the rect.jsp from the index.html.

1.1 – A Rectangle object is created and its properties are set using the request parameters.

1.2 – Rectangle object is set as attribute in the session scope.

1.3 – Response page containing hyper links to send requests to the view.jsp and exit.jsp is generated.

2.0 – A request is sent to the view.jsp using the hyper link in the response page of rect.jsp.

2.1 – Rectangle object is obtained from the session scope.

2.2 – Response page containing the dimensions & area of the rectangle and hyper link to send request to the exit.jsp is generated.

3.0 – A request is sent to the exit.jsp.

3.1 – HttpSession object is invalidated by exit.jsp.

3.2 – Response page is generated.

Source code of the application:

First, the home page index.html.

<b>Rectangle Home</b>
<form action="rect.jsp">
Length: <input type="text" name="length"> <br/>
Breadth: <input type="text" name="breadth"> <br/>
<input type="submit" value="create">
</form>

Second, the Rectangle class.

package com.techmentro.learningpad;

public class Rectangle {

	private int length,breadth;

	public int area()
	{
		return length*breadth;
	}
	
	public int getLength() {
		return length;
	}

	public void setLength(int length) {
		this.length = length;
	}

	public int getBreadth() {
		return breadth;
	}

	public void setBreadth(int breadth) {
		this.breadth = breadth;
	}
	
	
}

Third, the rect.jsp. Note that Rectangle object is create and set in session scope using the useBean action and its properties are set using the setProperty action.

<jsp:useBean id="rect" class="com.techmentro.learningpad.Rectangle" 
scope="session"/>
<jsp:setProperty property="*" name="rect"/>

<b>Rectangle created.</b> <br/>
view dimension & area <br/>
exit <br/>

Fourth, the view.jsp. Note that Rectangle object is obtained from the session scope using the useBean action and its properties values are written to the output stream using getProperty action.

<jsp:useBean id="rect" class="com.techmentro.learningpad.Rectangle" 
scope="session"/>

<b>Dimension & area of the Rectangle</b> <br/>
Length: <jsp:getProperty property="length" name="rect"/>
<br/>
Breadth: <jsp:getProperty property="breadth" name="rect"/>
<br/> 
Area: <%=rect.area() %><br/>

exit <br/>

Fifth, the exit.jsp.

<%
session.invalidate();
%>
<b>Thank you for using rectangle app.</b><hr/>
try again

If you like the post, then share it...Share on FacebookShare on Google+Tweet about this on TwitterShare on LinkedIn

Leave a Reply

Your email address will not be published. Required fields are marked *