Wednesday, 5 May 2010

Develop a web application to add or sub of two numbers using jsp:forward and jsp:param tags

Example program to illustrate about jsp:forward and jsp:param tags:
Develop a web application to add or sub of two numbers using jsp:forward and jsp:param tags

Steps:
Maintain the folder structure:


Home.html

<html>
 <head>
  <title>ActionTags
  </title>
 </head>
 <body>
  <pre>
   <form action="FronJSP.jsp" method="get">
    <b>Enter value of A </b><input type="text" name="field1"/>
    <b>Enter value of B</b><input type="text" name="field2"/>
    <input type="submit" name="submit" value="Add"/>
    <input type="submit" name="submit" value="Sub"/>
   </form>
  </pre>
 </body>
</html>


FronJSP.jsp

<%@page errorPage="/Home.html"%>

<%
  String s1=request.getParameter("field1");
  String s2=request.getParameter("field2");
  Integer.parseInt(s1);
  Integer.parseInt(s2);
  String s3=request.getParameter("submit");
  if(s3.equals("Add"))
  {  
 %>
 <jsp:forward page="/AddJsp.jsp"/>
 <%
  } 
  else if(s3.equals("Sub"))
  {  
 %>
<jsp:forward page="/SubJsp.jsp"/>
<%
 } 
  else
  {%>
<jsp:forward page="/Home.html"/>  
 
  <% }%>

//AddJsp.jsp

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<%
 int a=Integer.parseInt(request.getParameter("field1"));
 int b=Integer.parseInt(request.getParameter("field2"));
 int result=a+b;
 %>
 <jsp:forward page="/Result.jsp"> 
 <jsp:param value="<%=result%>" name="output"/>
 </jsp:forward>

SubJsp.jsp

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<%
 int a=Integer.parseInt(request.getParameter("field1"));
 int b=Integer.parseInt(request.getParameter("field2"));
 int result=a-b;
 %>
 <jsp:forward page="/Result.jsp">
 <jsp:param name="output" value="<%=result%>"></jsp:param>
 </jsp:forward>

Result.jsp

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<% 
 int a=Integer.parseInt(request.getParameter("field1"));
 int b=Integer.parseInt(request.getParameter("field2"));
 int result=a-b;
 %>
 <jsp:forward page="/Result.jsp">
 <jsp:param name="output" value="<%=result%>"></jsp:param>
 </jsp:forward>
Go to WEB-INF folder, create web.xml file (this is optional)
web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app id="WebApp_ID" version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
<display-name>ActionTag</display-name>
<welcome-file-list>
<welcome-file>Home.html</welcome-file>

</welcome-file-list>
</web-app>





Ho
Jsp Action Tags:



No comments:

Post a Comment