Steps to Connect AJAX Database using Struts 
 As i have explained clearly connecting AJAX with Servlets in previous chapter. 
Now i going to explain how to connect Database using Struts and AJAX.

Steps to Connect Database using Struts and Ajax
Step 1: Configuring  web.xml and struts-config.xml

Web.xml :
<web-app>
<display-name>AjaxTutorialonline</display-name> 
    <servlet>
        <servlet-name>action</servlet-name>
        <servlet-class>org.apache.struts.action.ActionServlet</servlet-class>
        <init-param>
            <param-name>config</param-name>
            <param-value>/WEB-INF/struts-config.xml</param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>action</servlet-name>
        <url-pattern>*.do</url-pattern>
    </servlet-mapping>
</web-app>

Step 2: Configuring Struts for Ajax Database Connection
Struts-config.xml

   <form-beans>
      <form-bean name="Ajaxtutorialonline" type="com.ajax.ExampleForm" />
   </form-beans>
   <action-mappings>
   <action path="/ajax" name="Ajaxtutorialonline" scope="request" type="ajax.tutorial.AjaxAction"/>
   </action-mappings>

Step 3:  
ExampleForm.java

package com.ajax;
import org.apache.struts.action.ActionForm;
public class ProfileForm extends ActionForm{
    private String empid=null;
    private String employee=null;
//Generate getter setter method
public String getEmpid() {
        return empid;
    }
  public void setEmpid (String empid) {
        this.empid= empid;
    }
public String getEmployee() {
        return employee;
    }
    public void setEmployee(String employee) {
        this.employee = employee;
    }

Step 4: Writing Action 
AjaxAction.java

package ajax.tutorial;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
public class AjaxAction extends Action
{
  public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest req, HttpServletResponse res)
{
 String val = req.getParameter("id");
String stor=null;
     Connection con = null;
     PreparedStatement pst =null;
     ResultSet rs=null;
     response.setContentType("text/xml");
     Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
     pst = con.prepareStatement("select Empid, EmpName where Empid=?");
        pst.setString(1,val);
     if(rs.next())
     {
      stor =rs.getString(1);
      response.getWriter().write(stor);
     }
  con.close();
  }catch(SQLException e)
   {
    
   }
}


Step 5: JavaScript AJAX
<script type="text/javascript">

function ajaxDB()
{
if(window.XMLHttpRequest)
{
xmlhttp = new XMLHttpRequest();

}
else
{
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.onreadystatechange=function()
{
var x= document.getElementById("hello");
x.innerHTML=xmlhttp.responseText;
}
xmlhttp.open("GET","/test.do",true);
xmlhttp.send();
}
</script>

<div id="hello"></div>


0 comments