You are on page 1of 3

Basic Servlet Communication Ok, getParameter() first: Code

:
<html> <body> <form name="testForm" method="post" action="testJSP.jsp"> <input type="text" name="testParam" value="Hello Vaibhav"> <input type="submit"> </form> </body> </html>

Ok so when you click the submit button on this HTML page the form will be posted to testJSP.jsp Code:
<!-- Code for testJSP.jsp --> <% String sValue = request.getParameter("testParam"); %> <html> <body> <%= sValue %> </body> </html>

request.getParameter("testParam") will get the value from the posted form (named testForm) of the input box named testParam which is "Hello Vaibhav". It will then print it out, so you should see "Hello Vaibhav" on the screen. So request.getParameter() will retrieve a value that the client has submitted. You will get the value on the server side. Now for request.getAttribute(), this is all done server side. YOU add the attribute to the request and YOU submit the request to another resource, the client does not know about this. So all the code handling this would typically be in servlets. Code:
// Your servlet code public class SomeServlet extends HttpServlet { protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { processRequest(request, response); } /** Handles the HTTP <code>POST</code> method. * @param request servlet request

com/yourResource" Now in this case yourResource is a Servlet.* @param response servlet response */ protected void doPost(HttpServletRequest request. IOException { processRequest(request.forward() the request will be forwarded to the resource specified ie: in this case "http://www. Strings. IOException { processRequest(request. * @param request servlet request * @param response servlet response */ protected void processRequest(HttpServletRequest request. } /** Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods. HttpServletResponse response) throws ServletException. Object object = new Object(). HttpServletResponse response) throws ServletException. * @param request servlet request * @param response servlet response */ protected void doPost(HttpServletRequest request. IOException { processRequest(request. I'm just using absolute for a clear example. / /You could give a relative URL. } } Now when you call rd. response). IOException { // Retrieve the object stored in the request. response). response). in fact any object.forward(request. response). . rd. HttpServletResponse response) throws ServletException. HttpServletResponse response) throws ServletException. request. object). here is the code Code: public class YourResourceServlet extends HttpServlet { protected void doGet(HttpServletRequest request.setAttribute("ObjectName".getAttribute("ObjectName"). * @param request servlet request * @param response servlet response */ protected void processRequest(HttpServletRequest request. IOException { RequestDispatcher rd = request. } /** Handles the HTTP <code>POST</code> method.website. Object myDispatchedObject = request. Custom objects.com/yourResource"). } /** Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods. // You can use any type of object you like here. HttpServletResponse response) throws ServletException.website.getRequestDispatcher("http://www.

} } .