Dears,

This blog is moved to a new blog at:

The new update will be found at that new blog, please follow it!
This blog is no more updated.
Thanks for your visit!

Monday, September 23, 2013

JSP form treatement using parameters

Objective
We will learn how to use JSP (Java Server Pages) to create HTML from and treat it with parameter. This is illustrated with a Web application based on Tomcat server.

Config Web server Tomcat
- Step 1: Download Tomcat server from Apache site: http://tomcat.apache.org/download-70.cgi (version 6.0 and higher)
- Step 2: setup your environment variables:
JAVA_HOME = absolute path to your java root directory
CATALINA_HOME = absolute path to your Tomcat folder

Create a Web application in Tomcat server
- Go to the Tomcat/webapps folder
- Create a sub folder testAdd with a sub folder WEB-INF and two .jsp as following structure:



- Edit the file add.jsp as follow:

<%@page language="java" import = " java.util.*, java.awt.*" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML><HEAD><TITLE>JSP demo test</TITLE>
<META http-equiv=Content-Type content="text/html; charset=iso-8859-1">
</HEAD>
<BODY leftMargin=0 topMargin=0>

      <table border="0" cellpadding="0" cellspacing="0" >
        <tr>
          <td width="100%" height="133">&nbsp;
          <form method="POST" name = "addForm" action="doAdd.jsp" >
            <p align="left">First number:
            <input type="text" name="firstnumber" size="12"></p>
            <p align="left">Second number:
           <input type="text" name="secondnumber" size="12"></p>           
            <p align="left"><input type="submit" value="Submit" name="B1">
            <input type="reset" value="Reset" name="B2"></p>
          </form>
          <p>&nbsp;</td>
        </tr>
      </table>  
</BODY>
  </HTML>

Note that this file will create a Web form with two textfields to enter the two numbers to calculate. Once the submit button is clicked, the server will call the page doAdd.jsp to treat this form (this is defined in the blue line code).
Run the server Tomcat (file startup.bat in the Tomcat/bin) and then start your Web browser and type URL: http://localhost:8080:testAdd/add.jsp. The results will appear as follow:



- Edit the file doAdd.jsp as follow:

<%@page language="java" import = "java.util.*, java.awt.*"%>
<%
Double sum;
Double firstNumber = Double.parseDouble((String)request.getParameter("firstnumber"));
Double secondNumber = Double.parseDouble((String)request.getParameter("secondnumber"));
sum = firstNumber + secondNumber;
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML><HEAD><TITLE>JSP demo test</TITLE>
<META http-equiv=Content-Type content="text/html; charset=iso-8859-1">

</HEAD>
<BODY leftMargin=0 topMargin=0>
The sum  is:  <%= sum %>
</BODY></HTML>

Note that the two lines of code in blue enable this page to get the value of form variables from the submitted form (in the page add.jsp)



The line The sum  is:  <%= sum %> will display the value of the variable sum on the Web form.

No comments:

Post a Comment