Wednesday 28 April 2010

Example program on Request Attributes


<!--Home.html-->
<html><body>
<form action="entry"><pre>
<b>

Operand 1 : <input type="text" name="op1"/>

Operand 2 : <input type="text" name="op2"/>

<input type="submit" name="action" value="Add"/>  <input type="submit" name="action" value="Substract"/>
</b></pre></form>
</body></html>

//servlet programs
package com.rajendra.servlets;
import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.GenericServlet;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;

public class ValidationServlet extends GenericServlet {

public void service (ServletRequest req, ServletResponse resp) throws ServletException, IOException {

try {
req.setAttribute("operand1",new Integer(req.getParameter("op1")));
req.setAttribute("operand2",new Integer(req.getParameter("op2")));
}//try
catch(Exception e){
resp.setContentType("text/html");
PrintWriter out=resp.getWriter();
out.println("Operand1 and Operand2 value should be numbers<br/>");
/*Defines an object that receives requests from the client and sends them to any 
* resource (such as a servlet, HTML file, or JSP file) on the server.
* The servlet container creates the RequestDispatcher object, which is used as a wrapper
* around a server resource located at a particular path or given by a particular name.
This interface is intended to wrap servlets, but a servlet container can create
RequestDispatcher objects to wrap any type of resource.
  */
RequestDispatcher rd=req.getRequestDispatcher("/Home.html");
rd.include(req,resp);
return;
}//catch

String action=req.getParameter("action");
RequestDispatcher rd=null;

if (action.equals("Add"))
rd=req.getRequestDispatcher("/add");
else
rd=req.getRequestDispatcher("/sub");
/*rd.forward(req,res);
 *  Forwards a request from a servlet to another resource
 *  (servlet, JSP file, or HTML file) on the server.
 */
rd.forward(req,resp);
}//service
}//class
////package com.rajendra.servlets;
import javax.servlet.GenericServlet;
import javax.servlet.ServletResponse;
import javax.servlet.ServletRequest;
import javax.servlet.ServletException;
import javax.servlet.RequestDispatcher;
import java.io.IOException;
import java.io.PrintWriter;
public class ResponseServlet extends GenericServlet {

public void service (ServletRequest req, ServletResponse res) throws ServletException, IOException {

Integer result=(Integer) req.getAttribute("result");

RequestDispatcher rd=req.getRequestDispatcher("/Home.html");

if (result==null){
rd.forward(req,res);
return;
/*
return is required here since we know that after forward the control comes back
to the source servlet, and here we dont want to execute the remaning part
of this service if our request is forwarded
*/
}//if

res.setContentType("text/html");
PrintWriter out=res.getWriter();

String op=(String) req.getAttribute("operation");
out.println(op+" Result : <b>"+result.intValue()+"</b><br/>");

rd.include(req,res);
}//service
}//class
//AddServlet.java
package com.rajendra.servlets;
import javax.servlet.GenericServlet;
import javax.servlet.ServletResponse;
import javax.servlet.ServletRequest;
import javax.servlet.ServletException;
import javax.servlet.RequestDispatcher;
import java.io.IOException;
public class AddServlet extends GenericServlet {

public void service (ServletRequest req, ServletResponse res) throws ServletException, IOException {

int op1=((Integer)req.getAttribute("operand1")).intValue();
int op2=((Integer)req.getAttribute("operand2")).intValue();

int result=op1+op2;

req.setAttribute("operation", "Addition");
req.setAttribute("result", new Integer(result));

RequestDispatcher rd=req.getRequestDispatcher("/resp");
rd.forward(req,res);
}//service
}//class
//SubServlet.java
package com.rajendra.servlets;
import javax.servlet.GenericServlet;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.ServletException;
import javax.servlet.RequestDispatcher;
import java.io.IOException;
public class SubServlet extends GenericServlet {

public void service (ServletRequest req, ServletResponse res) throws ServletException, IOException {

int op1=((Integer)req.getAttribute("operand1")).intValue();
int op2=((Integer)req.getAttribute("operand2")).intValue();

int result=op1-op2;

req.setAttribute("operation", "Subtraction");
req.setAttribute("result", new Integer(result));

RequestDispatcher rd=req.getRequestDispatcher("/resp");
rd.forward(req,res);
}//service
}//class
//maintain the folder structure
////web.xml
<!--web.xml-->
<web-app>
<servlet>
<servlet-name>es</servlet-name>
<servlet-class>com.rajendra.servlets.ValidationServlet</servlet-class>
</servlet>
<servlet>
<servlet-name>as</servlet-name>
<servlet-class>com.rajendra.servlets.AddServlet</servlet-class>
</servlet>
<servlet>
<servlet-name>ss</servlet-name>
<servlet-class>com.rajendra.servlets.SubServlet</servlet-class>
</servlet>
<servlet>
<servlet-name>rs</servlet-name>
<servlet-class>com.rajendra.servlets.ResponseServlet</servlet-class>
</servlet>

<servlet-mapping>
<servlet-name>es</servlet-name>
<url-pattern>/entry</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>Home.html</welcome-file>
</welcome-file-list>
<servlet-mapping>
<servlet-name>as</servlet-name>
<url-pattern>/add</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>ss</servlet-name>
<url-pattern>/sub</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>rs</servlet-name>
<url-pattern>/resp</url-pattern>
</servlet-mapping>
</web-app>









No comments:

Post a Comment