Id:
Name:
Address City:
● Deploy this JSP and ensure that you get the JSP displayed on the browser as per expectations. Well, there
is not JSPness in this JSP. All that you have written here could be written in Html and still make it work.
But the real work goes in next JSP.
● Note the action attribute in the Form tag. This means that when the user clicks on the submit button, the
request will be sent to AddCustomer.jsp on the server - needless to say that this request is sent by the browser.
● On the next slide we will see what goes in AddCustomer.jsp
Reading Data from request Object (Contd.) Add Customer
● Write a new jsp called AddCustomer.jsp as follows: id = request.getParameter("id");
name = request.getParameter("name");
address = request.getParameter("address");
cityId = request.getParameter("cityId");
out.println("The id is : " + id + " Name : " + name +
● Now once again run the Customer.jsp, enter data and click on submit button.
● Note that the data you have entered on Customer.jsp is made available to you on AddCustomer.jsp
● Also note the anchor that helps you to go back to Customer.jsp
Using Response Object The response Object
● The response object is also constructed by the server. All that is put in response object is rendered to
the client.
● You can write to the response object using the PrintWriter object of response object as follows:
PrintWriter myWriter = response.getWriter();
myWriter.println("Hi, This is my Writer");
● The above technique is of no use in JSP because we already have implicit object called out which is
nothing but an object of type PrintWriter pointing to response writer.
● Still, try the above code snippet in your AddCustomer.jsp and print the the data entered by the user
on Customer.jsp - What I mean is, use myWriter after initializing it to response.getWriter() instead of out.
Using Exception Object (Handling Errors) The page Directive
● Any number of exceptions can arise when a JSP page is executed.
● To specify that the Web container should forward control to an error page if an exception occurs,
include the following page directive at the beginning of your JSP page:
<%@ page errorPage="file_name" %>
● If there is any exception thrown while the jsp (which includes above oage directive) is executing, the
server will automatically forward the request to the page specified in errorPage="file_name"
● In AddCustomer.jsp, deliberately throw an SQLException as follows (do not forget to import the
java.sql.SQLException)
throw new SQLException("Duplicate Value");
Also include the page directive in AddCustomer.jsp as follows:
<%@ page errorPage="Error.jsp" %>
The Error Page
● Create a new JSP called Error.jsp. The following directive is important in Error.jsp :
<%@ page isErrorPage="true|false" %>
● The page directive indicates that it is serving as an error page
● This directive makes the exception object (of type javax.servlet.jsp.JspException) available to the error page, so that you can
retrieve, interpret, and possibly display information about the cause of the exception in the error page.
● In Error.jsp write the following:
<%
out.println("The Error Occurred is : <b>" + exception.getMessage() + "</>");
%>
● Execute Customer.jsp, put some data and hit submit. Since you deliberately throwing an exception from AddCustomer.jsp
the server forwards the request to Error.jsp
Assignment(s)
Implicit Objects Assignment(s)
● Create an index.jsp with anchors to Customer, Tax, Invoice and Product. When the user clicks on any one of these options, display him/her the form with all the fields that are there in the tables Customer, Tax, Invoice & Product respectively.
● Now implement the following: When the user clicks on Submit on the Customer.jsp (or any other jsp
mentioned above) your browser must submit the request to AddCustomer.jsp or AddTax.jsp or AddInvoice.jsp or AddProduct.jsp - depending on which form the user has clicked on submit. Using jdbc, add data entered by the user in the respective tables. i.e. AddCustomer.jsp must use jdbc to add to Customer table, AddTax to tax table and so on.
● Continue with the above application to provide search facility. Display Customer, Tax etc based on
id provided by the user.
● Continue with the above application to provide delete facility. Delete Customer, Tax etc based on id
provided by the user
Chapter: 3 - Beans & Form Processing jsp:UseBean
What is a Java Bean
● JavaBeans brings component technology to the Java platform.
● With the JavaBeans API you can create reuseable, platform-independent components.
● Using JavaBeans-compliant application builder tools, you can combine these components into
applets, applications, or composite components. JavaBean components are known as Beans.
● Any java class that has private attribute(s) and public get and set methods in it can be called a java
bean. For e.g a CustomerValueObject java bean can created as follows:
public class CustomerValueObject { public CustomerValueObject() { }
private int id;
private String name;
public int getId() { return id;
}
public String getName() { return name;
}
public void setId(int id) { this.id = id;
}
public void setName(String name) { this.name = name;
} }
● You will need to create such hundreds of java beans in real-life application, so that you can
instantiate it one component, put data in it and pass it over to the other component so that it can use it.
Creating a Java Bean
● Create a new package in your application called valueobjects and then create a class called
CustomerValueObject.java as follows:
package valueobjects;
public class CustomerValueObject { public CustomerValueObject() { }
private int id;
private String name;
private String address;
private int cityId;
public int getId() { return id;
}
public String getName() { return name;
}
public void setId(int id) { this.id = id;
}
public void setName(String name) { this.name = name;
}
public String getAddress() { return address;
}
public void setAddress(String address) { this.address = address;
}
public int getCityId() { return cityId;
}
this.cityId = cityId;
} }
● Now we can use jsp:UseBean method create the bean
● Go to AddCustomer.jsp and add the following jsp code after the closing of java scriplet you have already
written :
<jsp:useBean id="customerVO" class="valueobjects.CustomerValueObject"
scope="request"/>
Do not forget to import CustomerVO class using
<%@ page import="valueobjects.CustomerValueObject" %>
● Now we would like to forward our request to the other jsp called CustomerAddress.jsp. Write the
following line just before </body>
<jsp:forward page="CustomerAddress.jsp"/>
● Note how useBean method is used to initialize a java bean.
The jsp:useBean tag will look for the the object called "customerVO" - specified as id in request object (why in request ? because the scope="request" in jsp:useBean tag). If found, it will use it or else it will create one and put it on request object.
jsp:setProperty
Using the jsp:setProperty tag
● Add the following code snippet in the AddCustomer.jsp
<jsp:setProperty name="customerVO" property="id" value="<%=id%>" />
<jsp:setProperty name="customerVO" property="name" value="<%=name%>" />
<jsp:setProperty name="customerVO" property="address" value="<%=address%>" />
<jsp:setProperty name="customerVO" property="cityId" value="<%=cityId%>" />
● The above statements will add the values to the object created called customerVO of class valueobjects.
CustomerValueObject. Note that this object is created and put in request using jsp:useBean tag explained in earlier tag.
● Using the jsp:setProperty to write value to name using <jsp:setProperty name="customerVO" property="name"
value="yabadabadoo" /> is as good as writing <% customerVO.setName("yabadabadoo"); />
jsp:getProperty
Using jsp:getProperty Tag
● Now Write a CustomerAddress.jsp as follows: