Javabeans

private String firstName;

public String getFirstName()
{
  return firstName;
}

public void setFirstName(String f)
{
 firstName = f;
}

<jsp:useBean id="objectname" class = "classname" scope = "session"/>
       <jsp:useBean id="course" class="registrarbeans.Course" scope ="session" />

     replaces the following seqence.

registrarbeans.Course course = (registrarbeans.Course)session.getAttribute("course");   
if(course == null)  // new sesssion, just create a course
{
  course = new registrarbeans.Course();
  session.setAttribute("course", course);
 }

Connecting HTML parameters/forms directly to javabeans objects

     <jsp:useBean id="course" class="registrarbeans.Course" scope ="page" />
     <jsp:setProperty name="course" property="*" />
     <jsp:setProperty name="course" param="crn" property="crn" />


Course.java with JavaBean requirements:

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package registrarbeans;
import java.sql.*;

/**
 *
 * @author Mark Pendergast
 */
public class Course {
    int crn = 0;
    String course = "";
    String title = "";
    int credits = 0;
    String instructor = "";
    int maxseats = 0;
    int seatsleft = 0;
    String college = "";
   
    public Course()
    {
       
    }
    //
    // accessor methods, be careful of naming, must be get followed by variable name with first letter in caps
    //
    public int getCrn()
    {
      return crn;
    }
    public String getCourse()
    {
      return course;
    }
     public String getTitle()
    {
      return title;
    }
   
    public int getCredits()
    {
      return credits;
    }
    public String getInstructor()
    {
      return instructor;
    }
    public int getMaxseats()
    {
      return maxseats;
    }
     public int getSeatsleft()
    {
      return seatsleft;
    }
     public String getCollege()
    {
      return college;
    }
    //
    // mutator methods, be careful of naming, must be set followed by variable name with first letter in caps
    //
    public void setCrn(int n)
    {
      crn = n;
    }
   
    public void setCourse(String str)
    {
      course = str;
    }
   
    public void setTitle(String str)
    {
      title = str;
    }
   
    public void setCredits(int n)
    {
      credits = n;
    }
   
     public void setInstructor(String str)
    {
      instructor = str;
    }
    
    public void setMaxseats(int n)
    {
      maxseats = n;
    }
    
    public void setSeatsleft(int n)
    {
      seatsleft = n;
    }
    
    public void setCollege(String str)
    {
      college = str;
    }
   
  @Override 
    public String toString()
    {
      return crn+" "+course+" "+title;
    }
 
 //
 // Database methods
 //
 
  public boolean insertInto(String url)
  {
    //   
   // database work
   //              
        try{
         // open a connection
          Connection con = null;
          Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");  // load the driver
          con = DriverManager.getConnection(url);
        // create the sql command
          PreparedStatement prep = con.prepareStatement("insert into courseschedule (crn, course, title, credits, instructor, maxseats, seatsleft, college) values (?,?,?,?,?,?,?,?)"); 
          prep.setInt(1,crn);
          prep.setString(2,course);
          prep.setString(3,title);
          prep.setInt(4,credits);
          prep.setString(5,instructor);
          prep.setInt(6,maxseats);
          prep.setInt(7,seatsleft);
          prep.setString(8,college);
          int result = prep.executeUpdate();
         
          prep.close();
          con.close();
          if(result == 1)
             return true;
          else
             return false;
         }
        catch(Exception sqlex)
           {
            
           return false;
        }    
  }
 
}


addCourseWitBeans.jsp


<%--
    Document   : addCourseWithBeans
    Created on : Oct 24, 2012, 10:34:12 AM
    Author     : Mark Pendergast
--%>

<%@page contentType="text/html" pageEncoding="UTF-8"%>
<%@page errorPage = "errorPage.jsp" %>
<jsp:useBean id="course" class="registrarbeans.Course" scope="page" />
 <jsp:setProperty name="course" property="*" /> 
<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>Add Course</title>
    </head>
    <body>
      
  <h2>Add Course</h2>
 <%
    boolean ok = course.insertInto("jdbc:sqlserver://rubble.student.ad.fgcu.edu:1433;databaseName=sp12ism3232x;user=s12ISM3232;password=s12ISM3232;");
    if(ok)
     out.println("course added successfully :)<br/>");
    else
     out.println("error  - course not added :( <br/>");
 %>
    </body>
</html>



errorPage.jsp


<%--
    Document   : errorPage
    Created on : Oct 24, 2012, 10:56:31 AM
    Author     : Mark Pendergast
--%>

<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<%@page isErrorPage = "true" %>

<html>
<head><title>System Error</title></head>
<body>
<h2>An error occurred while processing your request</h2>
<input type="button" value="click here to try again" onclick="javascript:history.back()">
<p>
<%= "Input Error : "+exception.getMessage()+"\n"%>
<p>
</body>
</html>