Advertisment

HTML Forms with JSP

author-image
PCQ Bureau
New Update

Continuing our series on using JavaServer Pages, (JSP) this month we’ll write a JSP that can take information from a form written in HTML and store it in a MySQL database. We’ll use the same data as we used last month: a MySQL database named ‘Products’ that contains product information of various computers. We’ll run our JSP, named FormJSP, using the /jsp context that we had set up in last month’s article, Customizing Tomcat.

Advertisment

Mount this month’s CD, change to the directory cdrom/sorc_cod/jsp and copy the file product.html and FormJSP.jsp to /home/httpd/html/jsp directory. Open the file FormJSP.jsp in a text editor and change the value of the variable password from pcq to the password for your MySQL database. Now enter the following URL in yourWeb browser.

http://127.0.0.1:8080/jsp/product.html

If you’ve configured Tomcat module for Apache, you don’t need to use the port number 8080 in the URL. Enter the product information in the Web page presented. While entering the price, make sure that you enter an integer or a decimal value only. Then click the ‘Insert into database’ button. You will be shown a message ‘The database has been updated’. If you run the jspprogram JSP that we’d coded last month, you’ll be able to see the product information you have entered, along with the other existing products. 

Advertisment

To separate the JSP scripts from HTML files, you can place the product.html file in html subdirectory and FormJSP.jsp file in the jsp subdirectory, much like having a cgi-bin directory where all the CGI scripts are kept. If you do this, you must change the URL to the JSP script (specified with the action attribute of the Form) in product.html from FormJSP.jsp to

jsp/FormJSP.jsp. 

Let’s go through the JSP code now. Open the file FormJSP.jsp in a text editor to follow us better. If you remember, in the jspprogram JSP that we wrote last month, we used the full package name to specify the classes, for example java.sql.Connection, java.sql. Statement. To eliminate this, we introduce a JSP tag called a directive tag: <%@ page …….%>. The import attribute of this tag does the same work as the import statement in a Java program. This lets us refer to the classes by just their class names, without prefixing the package name. 

Now we come to the statements which extract the information given in the HTML forms. In the source of product.html, you’ll see that we’ve specified the name of the JSP file, FormJSP.jsp, with the action attribute of the form. This JSP program is called when we click on the submit button, labeled ‘Insert into database’. All the information fed in the form is encapsulated in an object named Request. The request object implements the methods (or functions) defined in the interface HttpServletRequest. This interface defines methods for extracting information about an HTTP transaction. Thus, a request object contains information about the URL that invoked the JSP, the name or IP address of the client and server machine, and the HTTP method used to invoke the JSP, namely GET, POST, PUT, etc. There are corresponding methods available to extract all this information from the request object. One of these methods, getParameter( ) accepts the name of a form element–text field, text area, radio button, etc,–and returns its value–the text entered in a text field or area, or the selected option of a radio button. With the following statements, 

Advertisment

String product_id = request.getParameter(“id”);



String product_name = request.getParameter(“name”);


String product_price = request.getParameter(“price”);

we extract the text typed in the three text fields named–id, name, and price–and store them in String objects product_id, product_name and product_price respectively. In the next statement, we issue an SQL query to update the database with the new product information. Once this is done and provided there have been no errors in updating the database, the message ‘Database has been updated’ (in bold font) is shown using the statement:

out.println(“The database has been updated”);

Advertisment

The out object, an object of the class JspWriter, is available to a JSP like the request object. The println( ) method of this object can be used to output some text on the Web page. In the jspprogram JSP, we used the JSP expression tag for this. Similarly, to display an error message, you can use the expression tag or the printIn() method of the out object. 

Next month, we’ll write a JavaBean that collaborates with a JSP to do the same work as the FormJSP, but more efficiently.

Shekhar Govindarajan

Advertisment