SlideShare a Scribd company logo
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>
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:useBean id="name" class="beans.NameBean" />
<jsp:setProperty name="name" property="*" />
<html>
<head>
<title>JSP Form results using a bean</title>
</head>
<body>
<h1>JSP Form results using a bean</h1>
Hello
<jsp:getProperty name="name" property="firstName" />
<jsp:getProperty name="name" property="lastName" />
</body>
</html>

Note the use of property="*" to set all the bean properties
using the form data values (i.e., GET/POST parameters)
MVC approach
 MVC stands for Model-View-Controller
 Controller:
   controls the application by getting the input
   and forwarding the request to the appropriate
   page
 View:
   visual appearance of the output data
 Model:
   logic, calculations necessary to produce the
   data. Sometimes called business logic




MVC Example (Temperature)
Getting the input (Fahrenheit temperature)
  form.jsp (user input of temperature)
  controller.jsp (dispatch (forward) request)
Displaying the output (Celsius temperature)
  response.jsp
Model (logic and calculations done by beans)
  TemperatureBean.java (form data)
  TemperatureCalculatorBean.java
  (does the temperature conversion)
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
      <jsp:forward page="file.jsp" />
    The important idea here is that this new
    page receives all the request parameters
    of the forwarding page.
controller.jsp:

<jsp:useBean id=" temperature" class="beans.TemperatureBean">
<jsp:setProperty name="temperature" property="*" />
</jsp:useBean>
<% if (request.getParameter("temperature") == null)
   {
%>
   <jsp:forward page="form.jsp" />
<%
   } else {
%>
   <jsp:forward page="response.jsp" />
<% }
%>




 form.jsp
<html>
<head>
<title>Fahrenheit to Celsius Conversion</title>
</head>
<body>
<h1>Fahrenheit to Celsius Conversion</h1>
<form action="controller.jsp" method="GET">
Fahrenheit temperature:
<input type="text" name="temperature"></ br>
<input type="submit" name="button"
   value="Submit Temperature"></p>
</form>
</body>
</html>
response.jsp
<jsp:useBean id="temp" class="beans.Temperature"
   property=“*” />
<jsp:useBean id="convert"
   class="beans.TempertureCalculatorBean" />
<jsp:setProperty name="convert" property="temperature"
   value="<%= temp.getTemperature() %>" />
<html>
<head><title>Fahrenheit to Celsius Conversion</title>
</head><body><h1>Fahrenheit to Celsius Conversion</h1>
<jsp:getProperty name="temp" property="temperature" />
Fahrenheit is
<jsp:getProperty name="convert"
property="temperature" /> Celsius.
<p><a href="controller.jsp">Convert another .. </a>
</p></body></html>




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

More Related Content

PPT
CRUD with Dojo
PDF
Basic Crud In Django
PPTX
Mule esb first http connector
DOC
Library Project Marcelo Salvador
PDF
React 101
DOCX
Week 12 code
DOCX
Html To JSP
PDF
Cake fest 2012 create a restful api
CRUD with Dojo
Basic Crud In Django
Mule esb first http connector
Library Project Marcelo Salvador
React 101
Week 12 code
Html To JSP
Cake fest 2012 create a restful api

What's hot (12)

DOCX
VPN Access Runbook
PPTX
PDF
REST API with CakePHP
PDF
Desenvolvendo APIs usando Rails - Guru SC 2012
PPTX
F# in the enterprise
ODP
Box connector Mule ESB Integration
DOCX
Build Your First Java Jersey JAX-RS REST Web Service in less than 15 Minutes
DOC
2190 pertemuan24(polling)
ODP
Running ms sql stored procedures in mule
TXT
Conexcion java mysql
PDF
Full Stack Toronto - the 3R Stack
PDF
XamarinとAWSをつないでみた話
VPN Access Runbook
REST API with CakePHP
Desenvolvendo APIs usando Rails - Guru SC 2012
F# in the enterprise
Box connector Mule ESB Integration
Build Your First Java Jersey JAX-RS REST Web Service in less than 15 Minutes
2190 pertemuan24(polling)
Running ms sql stored procedures in mule
Conexcion java mysql
Full Stack Toronto - the 3R Stack
XamarinとAWSをつないでみた話
Ad

Viewers also liked (14)

PDF
Lecture6
PPT
Environmental strategies workshop bouligny
ODP
Did you know every BODY needs Fiber?
PDF
Katalog jaro leto-2013-web
PDF
The Millennial Shift: Financial Services and the Digial Generation Study Preview
PDF
2ª semana
PDF
Ejb examples
PPTX
December 2014 Mobile Finance Trends and Innovations
PPTX
PPP HOW TO EXPORT A PRODUCT
PDF
Innovative work surumi
ODP
#weightloss 2014 vs Old School #Dieting
PPT
Alimenti ricchi in grassi
DOCX
Digital osc
PPTX
5 string bluegrass banjo photo album slideshare
Lecture6
Environmental strategies workshop bouligny
Did you know every BODY needs Fiber?
Katalog jaro leto-2013-web
The Millennial Shift: Financial Services and the Digial Generation Study Preview
2ª semana
Ejb examples
December 2014 Mobile Finance Trends and Innovations
PPP HOW TO EXPORT A PRODUCT
Innovative work surumi
#weightloss 2014 vs Old School #Dieting
Alimenti ricchi in grassi
Digital osc
5 string bluegrass banjo photo album slideshare
Ad

Similar to Jsp examples (20)

PDF
PDF
AnkaraJUG Kasım 2012 - PrimeFaces
PPT
Struts Overview
KEY
Integrating Wicket with Java EE 6
PDF
Need help on creating code using cart. The output has to show multip.pdf
PDF
JS Fest 2019. Glenn Reyes. With great power comes great React hooks!
PDF
Step By Step Guide For Buidling Simple Struts App
PDF
Java(Java_Servlet_programs_examples).pdf
PDF
Introduction to backbone presentation
KEY
Django quickstart
PPT
JavaScript
PDF
Primefaces Confess 2012
PDF
#30.스프링프레임워크 & 마이바티스 (Spring Framework, MyBatis)_스프링프레임워크 강좌, 재직자환급교육,실업자국비지원...
PDF
Experience Manager 6 Developer Features - Highlights
PDF
4 introduction-php-mvc-cakephp-m4-controllers-slides
PDF
JavaServer Pages
PDF
Core Java tutorial at Unit Nexus
PDF
FLOW3, Extbase & Fluid cook book
AnkaraJUG Kasım 2012 - PrimeFaces
Struts Overview
Integrating Wicket with Java EE 6
Need help on creating code using cart. The output has to show multip.pdf
JS Fest 2019. Glenn Reyes. With great power comes great React hooks!
Step By Step Guide For Buidling Simple Struts App
Java(Java_Servlet_programs_examples).pdf
Introduction to backbone presentation
Django quickstart
JavaScript
Primefaces Confess 2012
#30.스프링프레임워크 & 마이바티스 (Spring Framework, MyBatis)_스프링프레임워크 강좌, 재직자환급교육,실업자국비지원...
Experience Manager 6 Developer Features - Highlights
4 introduction-php-mvc-cakephp-m4-controllers-slides
JavaServer Pages
Core Java tutorial at Unit Nexus
FLOW3, Extbase & Fluid cook book

More from vantinhkhuc (20)

PDF
Url programming
PDF
Servlets intro
PDF
Servlet sessions
PDF
Security overview
PDF
PDF
PDF
Lecture17
PDF
Lecture11 b
PDF
Lecture10
PDF
Lecture9
PDF
PDF
Jsf intro
PDF
PDF
PDF
PDF
Ejb intro
PPT
Chc6b0c6a1ng 12
PPT
PDF
PDF
Ajas11 alok
Url programming
Servlets intro
Servlet sessions
Security overview
Lecture17
Lecture11 b
Lecture10
Lecture9
Jsf intro
Ejb intro
Chc6b0c6a1ng 12
Ajas11 alok

Jsp examples

  • 1. 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>
  • 2. 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:useBean id="name" class="beans.NameBean" /> <jsp:setProperty name="name" property="*" /> <html> <head> <title>JSP Form results using a bean</title> </head> <body> <h1>JSP Form results using a bean</h1> Hello <jsp:getProperty name="name" property="firstName" /> <jsp:getProperty name="name" property="lastName" /> </body> </html> Note the use of property="*" to set all the bean properties using the form data values (i.e., GET/POST parameters)
  • 3. MVC approach MVC stands for Model-View-Controller Controller: controls the application by getting the input and forwarding the request to the appropriate page View: visual appearance of the output data Model: logic, calculations necessary to produce the data. Sometimes called business logic MVC Example (Temperature) Getting the input (Fahrenheit temperature) form.jsp (user input of temperature) controller.jsp (dispatch (forward) request) Displaying the output (Celsius temperature) response.jsp Model (logic and calculations done by beans) TemperatureBean.java (form data) TemperatureCalculatorBean.java (does the temperature conversion)
  • 4. 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
  • 5. 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 <jsp:forward page="file.jsp" /> The important idea here is that this new page receives all the request parameters of the forwarding page.
  • 6. controller.jsp: <jsp:useBean id=" temperature" class="beans.TemperatureBean"> <jsp:setProperty name="temperature" property="*" /> </jsp:useBean> <% if (request.getParameter("temperature") == null) { %> <jsp:forward page="form.jsp" /> <% } else { %> <jsp:forward page="response.jsp" /> <% } %> form.jsp <html> <head> <title>Fahrenheit to Celsius Conversion</title> </head> <body> <h1>Fahrenheit to Celsius Conversion</h1> <form action="controller.jsp" method="GET"> Fahrenheit temperature: <input type="text" name="temperature"></ br> <input type="submit" name="button" value="Submit Temperature"></p> </form> </body> </html>
  • 7. response.jsp <jsp:useBean id="temp" class="beans.Temperature" property=“*” /> <jsp:useBean id="convert" class="beans.TempertureCalculatorBean" /> <jsp:setProperty name="convert" property="temperature" value="<%= temp.getTemperature() %>" /> <html> <head><title>Fahrenheit to Celsius Conversion</title> </head><body><h1>Fahrenheit to Celsius Conversion</h1> <jsp:getProperty name="temp" property="temperature" /> Fahrenheit is <jsp:getProperty name="convert" property="temperature" /> Celsius. <p><a href="controller.jsp">Convert another .. </a> </p></body></html> Reference COS 2206 JSP, Barry G. Adams, Department of Mathematics and Computer Science, Laurentain University.