Saturday 28 January 2012

STRUTS DEMO WITH AJAX USING jQUERY

STRUTS DEMO WITH AJAX USING jQUERY

When you submit the form the page itself is sent to the server. So it took a little bit time. To avoid this we can send the data rather than the whole page. We can provide such functionality to a page with the help of jQuery AJAX. For that we need to do some modifications to our demoInput.jsp page and we don't need demoOutput.jsp since we are going to show the output on the demoInput.jsp page itself. So, let us see how to provide ajax functionality to a jsp page with the help of JQuery. To avoid confussion I rename demoInput.jsp as ajaxdemoInput.jsp and calcapp to ajaxdemoapp.

-----------------------------------------------------------------------

and so we have to do a little bit change in demoAction.java too.

package demopack;


public class demoAction extends Action

{

public ActionForward execute(ActionMapping mapping,ActionForm form, HttpServletRequest request, HttpServletResponse response)throws IOException

{

demoForm demoform=(demoForm)form;

response.setContentType("text/text;charset=utf-8");

response.setHeader("cache-control", "no-cache");

PrintWriter out = response.getWriter();

String a=demoform.getA();

String b=demoform.getB();

String operation=demoform.getOperation();

calcbean bean1=new calcbean();

String r =bean1.calculate(a,b,operation);

String r1 = "Result is ......"+r;

out.println(r1);

out.flush();

return null;

}

//------------------------------------------

}

and calcbean.java and demoForm.java are same as in last example code.

*********************************************************************************