Docsity

Using Beans with HTML Forms and JSP: An Example of Model-View-Controller Architecture - Pr, Study notes of Computer Science

Toggle sidebar
bg1
Beans and HTML Forms
Usually Beans are used to
represent the data of HTML
forms
Example: Name Form
<html>
<head>
<title>JSP Processong form using a bean</title>
</head>
<body>
<h1>JSP Processing form using a bean</h1>
<form action="doFormBean.jsp" method="GET">
First name: <input type="text" name="firstName"></ br>
Last name: <input type="text" name="lastName">
<p><input type="submit" name="button"
value="Submit Name"></p>
</form>
</body>
</html>
pf3
pf4
pf5

Partial preview of the text

Download Using Beans with HTML Forms and JSP: An Example of Model-View-Controller Architecture - Pr and more Study notes Computer Science in PDF only on Docsity!

Beans and HTML Forms

Usually Beans are used to

represent the data of HTML

forms

Example: Name Form

JSP Processong form using a bean

JSP Processing form using a bean

First name:
Last name:

Example: Name Bean

package beans; public class NameBean { private String firstName; // property private String lastName; // property public String getFirstName() { return firstName; } public String getLastName() { return lastName; }

public void setFirstName(String first) { firstName = first; } public void setLastName(String last) { lastName = last; } }

Example: doFormBean.jsp

JSP Form results using a bean

JSP Form results using a bean Hello

Note the use of property="*" to set all the bean properties

using the form data values (i.e., GET/POST parameters)

TemperatureBean:

package beans; public class TemperatureBean { private String temperature;

public String getTemperature() { return temperature ; }

public void setTemperature(String t) { temperature = t; } }

TemperatureCalculatorBean:

package beans; import java.text.DecimalFormat; public class TemperatureCalculatorBean { private double celsius;

public String getTemperature() { return new DecimalFormat("0.00").format(celsius); }

// continued on next slide

TemperatureCalculatorBean (Contd.):

public void setTemperature(String fahr) { double f; try { f = Double.parseDouble(fahr); } catch (NumberFormatException e) { f = 0.0; } celsius = (f - 32.0) * (5.0 / 9.0); } }

Forwarding A Page

„ A JSP can forward the request to another

page using

„ The important idea here is that this new

page receives all the request parameters

of the forwarding page.

response.jsp

Fahrenheit to Celsius Conversion Fahrenheit to Celsius Conversion

Fahrenheit is Celsius.

Convert another ..

Reference

„ COS 2206 JSP, Barry G. Adams, Department of Mathematics and Computer Science, Laurentain University.