TP : XMLHTTP & JSP (MVC 1) ===> TELECHARGER
1 - Les objets Bean :
1.1 - PaysBean :
----------------------------- PaysBean.java --------------------------------
package com.bean;
public class PaysBean {
private String strCode;
private String strLibelle;
public String getStrCode()
{
return strCode;
}
public void setStrCode(String strCode)
{
this.strCode = strCode;
}
public String getStrLibelle()
{
return strLibelle;
}
public void setStrLibelle(String strLibelle)
{
this.strLibelle = strLibelle;
}
}
---------------------------------------------------------------
1.2 - VilleBean :
----------------------------- VilleBean.java --------------------------------
package com.bean;
public class VilleBean {
private String strCode;
private String strLibelle;
private String strPays;
public String getStrCode()
{
return strCode;
}
public void setStrCode(String strCode)
{
this.strCode = strCode;
}
public String getStrLibelle()
{
return strLibelle;
}
public void setStrLibelle(String strLibelle)
{
this.strLibelle = strLibelle;
}
public String getStrPays()
{
return strPays;
}
public void setStrPays(String strPays)
{
this.strPays = strPays;
}
}
---------------------------------------------------------------
2 - Lobjet Connect (Conexion a la base de donnee ): ----------------------------- Connect.java --------------------------------
package com.data;
import java.sql.*;
public class Connect {
private Connection con;
public ResultSet rs;
private Statement st;
public Connect() {
try
{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
}
catch (ClassNotFoundException c) {
}
}
public void getConexion()throws SQLException
{
con = DriverManager.getConnection("jdbc:odbc:DB");
}
public ResultSet getDatos(String req) throws SQLException {
st = con.createStatement();
rs = st.executeQuery(req);
return rs;
}
public void closeConexion()throws SQLException
{
con.close();
if (rs != null)
{
rs.close();
}
}
}
---------------------------------------------------------------
3 - L’objet Controleur (Traitement ) :
----------------------------- Controleur.java --------------------------------
package com.controleur;
import java.sql.ResultSet;
import java.util.ArrayList;
import com.bean.PaysBean;
import com.bean.VilleBean;
import com.data.Connect;
public class Controleur {
public ArrayList getPays()
{
ResultSet rs = null;
Connect objConnect = null;
PaysBean objPaysBean = null;
ArrayList arrListPays = new ArrayList();
try
{
objConnect = new Connect();
objConnect.getConexion();
rs = objConnect.getDatos("select * from pays " );
while (rs.next())
{
objPaysBean = new PaysBean();
objPaysBean.setStrCode(rs.getString("Code"));
objPaysBean.setStrLibelle(rs.getString("Libelle"));
arrListPays.add(objPaysBean);
}
}
catch (Exception e)
{
}
return arrListPays;
}
public ArrayList getVille(String strPays)
{
ResultSet rs = null;
Connect objConnect = null;
VilleBean objVilleBean = null;
ArrayList arrListVille = new ArrayList();
try
{
objConnect = new Connect();
objConnect.getConexion();
rs = objConnect.getDatos("select * from ville where pays=" + strPays);
while (rs.next())
{
objVilleBean = new VilleBean();
objVilleBean.setStrCode(rs.getString("Code"));
objVilleBean.setStrLibelle(rs.getString("Libelle"));
arrListVille.add(objVilleBean);
}
}
catch (Exception e)
{
}
return arrListVille;
}
}
---------------------------------------------------------------
4 - Servlet :
----------------------------- Servlet.java --------------------------------
package com.servlet;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.bean.VilleBean;
import com.controleur.Controleur;
public class XmlServlet extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doPost(request, response);
}
public void doPost(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException
{
int i = 0;
String strPays = "";
String pageSortie = "";
String strAction = "";
StringBuffer strXml = null;
Controleur objControleur = new Controleur();
VilleBean objVilleBean = null;
ArrayList arrListVille = new ArrayList();
ArrayList arrListPays = new ArrayList();
strAction = request.getParameter("hidAction");
if (strAction.equals("getPays"))
{
arrListPays = objControleur.getPays();
request.setAttribute("ListPays", arrListPays);
pageSortie ="/MyJsp.jsp";
getServletConfig().getServletContext().getRequestDispatcher(pageSortie).forward(request, response);
}
else if (strAction.equals("getVille"))
{
strPays = request.getParameter("hidPays");
arrListVille = objControleur.getVille(strPays);
strXml = new StringBuffer("");
for (i=0 ; i");
strXml.append("");
strXml.append(objVilleBean.getStrCode());
strXml.append("
");
strXml.append("");
strXml.append(objVilleBean.getStrLibelle());
strXml.append("");
strXml.append("");
}
strXml.append("");
response.setContentType("text/xml; charset=utf-8");
PrintWriter out = response.getWriter();
out.println(strXml.toString());
out.flush();
out.close();
}
}
}
---------------------------------------------------------------
5 – Les page JSP :
5.1 - Index.jsp :
5.2 - MyJsp.jsp :
by AideSoft