IdentifiantMot de passe
Loading...
Mot de passe oublié ?Je m'inscris ! (gratuit)
Navigation

Inscrivez-vous gratuitement
pour pouvoir participer, suivre les réponses en temps réel, voter pour les messages, poser vos propres questions et recevoir la newsletter

Servlets/JSP Java Discussion :

Afficher deux servlets dans une meme page


Sujet :

Servlets/JSP Java

  1. #1
    Candidat au Club
    Homme Profil pro
    Stagiaire développeur Java
    Inscrit en
    Juin 2014
    Messages
    6
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 33
    Localisation : France, Val de Marne (Île de France)

    Informations professionnelles :
    Activité : Stagiaire développeur Java
    Secteur : Conseil

    Informations forums :
    Inscription : Juin 2014
    Messages : 6
    Points : 3
    Points
    3
    Par défaut Afficher deux servlets dans une meme page
    Bonjour a tous ,
    Je souhaiterai afficher le résultat de mon formulaire contenu dans une servlet dans une même page que le résultat (Formulaire , puis juste en bas le résultat de ma requête.
    J'ai essayé request.getDispatcher(..) mais je n'ai pas réussi.
    J'ai deux servlet , l'une affichant le formulaire a remplir . J'ai utilisé des select pour que l'utilisateur puisse choisir parmi une liste choisis dans la bdd (search).
    ResultSearch affiche le résultat de la requête .
    Je vous joins le code de mon projet pour y voir plus claire

    Search.java
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
    136
    137
    138
    139
    140
    141
    142
    143
    144
    145
    146
    147
    148
    149
    150
    151
    152
    153
    154
    155
    156
    157
    158
    159
    160
    161
    162
    163
    164
    165
    166
    167
    168
    169
    170
    171
    172
    173
    174
    175
    176
    177
    178
    179
    180
    181
    182
    183
    184
    package servlet.classes;
     
    import java.io.IOException;
    import java.io.PrintWriter;
    import java.sql.Connection;
    import java.sql.DriverManager;
    import java.sql.ResultSet;
    import java.sql.SQLException;
    import java.sql.Statement;
     
    import javax.servlet.RequestDispatcher;
    import javax.servlet.ServletContext;
    import javax.servlet.ServletException;
    import javax.servlet.annotation.WebServlet;
    import javax.servlet.http.HttpServlet;
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;
    import javax.servlet.http.HttpSession;
     
    import org.apache.log4j.BasicConfigurator;
     
     
    public class Search extends HttpServlet {
    	private static String driverName = "org.apache.hive.jdbc.HiveDriver";
    	private static Connection con;
    	private static final long serialVersionUID = 1L;
     
     
        public Search() {
            super();
            // TODO Auto-generated constructor stub
        }
     
     
    	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    		// TODO Auto-generated method stub
    		response.setContentType("text/html");
    		PrintWriter out= response.getWriter();
     
     
    		out.println("<link type=\"text/css\" rel=\"stylesheet\" href=\"inc/formSearch.css\" />");
    		out.println("<html>");
    		out.println(" <img src=\"inc/logo.jpg\" width=\"10%\" height=\"10%\" align=\"right\" margin-right:50px/>");
    		out.println("<head>");
    		out.println("<meta charset='utf-8'/>");
    		out.println("<title>Test</title>");
    		out.println("</head>");
    		out.println("<body>");
    		out.println("<p>Rechercher un professionnel près de chez vous</p>");
    		out.println("<form method=\"POST\" action=ResultSearch>");
    		out.println("<div align=\"center\"> <fieldset>");
    		out.println("<legend>Rechercher un professionnel</legend>");
    		out.println("<label for=\"activite\">Activité <span class=\"requis\">*</span></label>");
    		out.println("<select name=\"activite\">");
     
     
     
    		Statement stmt = null;
    		try {
    			stmt = con.createStatement();
    		} catch (SQLException e) {
    			// TODO Auto-generated catch block
    			e.printStackTrace();
    		}
    		String tableName = "new_search_db.activite";
     
     
    		String sql;
    		BasicConfigurator.configure();
     
     
    		sql = "select no_rub_an9,li_activite from " +tableName +" order by li_activite asc limit 3000";
    		//System.out.println("Requete: " + sql);
    		ResultSet res = null;
    		try {
    			res = stmt.executeQuery(sql);
    		} catch (SQLException e) {
    			// TODO Auto-generated catch block
    			e.printStackTrace();
    		}
    		try {
    			while (res.next()){
    				out.println("<option value=\""+res.getInt(1)+"\">"+res.getString(2)+"</option>");
    			}
    		} catch (SQLException e) {
    			// TODO Auto-generated catch block
    			e.printStackTrace();
    		}
    		try {
    			stmt.close();
    		} catch (SQLException e1) {
    			// TODO Auto-generated catch block
    			e1.printStackTrace();
    		}
    		out.println("</select> <br>");
     
    		out.println("<label for=\"localite\">Localité <span class=\"requis\">*</span></label>");
    		out.println("<select  name=\"localite\"> ");
    		Statement stmt2 = null;
    		try {
    			stmt2 = con.createStatement();
    		} catch (SQLException e) {
    			// TODO Auto-generated catch block
    			e.printStackTrace();
    		}
    		String tableName2 = "new_search_db.localite";
     
     
    		String sql2;
    		BasicConfigurator.configure();
     
     
    		sql2 = "select no_localite,li_localite from " +tableName2 +" order by li_localite asc limit 1000";
    		//System.out.println("Requete: " + sql2);
    		ResultSet res2 = null;
    		try {
    			res2 = stmt2.executeQuery(sql2);
    		} catch (SQLException e) {
    			// TODO Auto-generated catch block
    			e.printStackTrace();
    		}
    		try {
    			while (res2.next()){
    				out.println("<option value=\""+res2.getInt(1)+"\">"+res2.getString(2)+"</option>");
     
    			}
    		} catch (SQLException e) {
    			// TODO Auto-generated catch block
    			e.printStackTrace();
    		}
     
    		out.println("</select><br><br>");
    		out.println("<labelfor=\"Rayon\">Rayon Max <span class=\"requis\">");
    		out.println("</label>");
    		out.println("<input type=\"text\" id=\"rayonMax\" name=\"rayonMax\" size=\"2\" maxlength=\"4\" /> <br />");
    		out.println("<label for=\"nombreCible\">Nombre d'etablissements </label> <input type=\"text\" id=\"nbCible\" name=\"nombreCible\" size=\"1\" maxlength=\"6\" /><br /> <br>");
    		out.println("<input type=\"submit\" value=\"Search\" onclick=\"showGif();\"/>");
    		out.println("<div id=\"Gif\" align=\"center\" style=\"visibility: hidden; \"> <img src=\"inc/Gif.gif\"/> </div>");
    		out.println("<script language=\"javascript\"> function showGif(){ document.getElementById(\"Gif\").style.visibility=\"visible\"} </script>");
    		out.println("<footer align=\"center\">  Business Solution <br> Copyright © 2014 </footer>");
    		out.println("</fieldset></div>");
    		out.println("</form>");
     
     
    		out.println("</body>");
     
    		out.println("</html>");
    		try {
    			stmt2.close();
    		} catch (SQLException e) {
    			// TODO Auto-generated catch block
    			e.printStackTrace();
    		}
     
     
    	}
     
     
    	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    		// TODO Auto-generated method stub
    	}
     
    	@Override
    	public void init() throws ServletException {
    		// TODO Auto-generated method stub
    		super.init();
     
    		con = null;
     
    			try {
    				Class.forName(driverName);
    				con = DriverManager.getConnection("jdbc:hive2://192.168.236.203:21050/;auth=noSasl");
     
    			} catch (Exception e1) {
    				// TODO Auto-generated catch block
    				e1.printStackTrace();
    			}
     
     
     
    	}
     
     
    }
    ResultSearch.java
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
    136
    137
    138
    139
    140
    141
    142
    143
    144
    145
    146
    147
    148
    149
    150
    151
    152
    153
    154
    155
    156
    157
    158
    159
    160
    161
    162
    163
    164
    165
    166
    167
    168
    169
    170
    171
    172
    173
    174
    175
    176
    177
    178
    179
    180
    181
    182
    183
    184
    185
    186
    187
    188
    189
    190
    191
    192
    193
    194
    195
    196
    197
    198
    199
    200
    201
    202
    203
    204
    205
    206
    207
    208
    209
    210
    211
    212
    213
    214
    215
    216
    217
    218
    219
    220
    221
    222
    223
    224
    225
    226
    227
    228
    229
    230
    231
    232
    233
    234
    235
    236
    237
    238
    239
    240
    241
    242
    243
    244
    245
    246
    247
    248
    249
    250
    251
    252
    253
    254
    255
    256
    257
    258
    259
    260
    261
    package servlet.classes;
     
    import java.io.IOException;
    import java.io.PrintWriter;
    import java.sql.Connection;
    import java.sql.DriverManager;
    import java.sql.ResultSet;
    import java.sql.SQLException;
    import java.sql.Statement;
     
    import javax.servlet.FilterChain;
    import javax.servlet.RequestDispatcher;
    import javax.servlet.ServletConfig;
    import javax.servlet.ServletContext;
    import javax.servlet.ServletException;
    import javax.servlet.ServletRequest;
    import javax.servlet.ServletResponse;
    import javax.servlet.annotation.WebServlet;
    import javax.servlet.http.HttpServlet;
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;
    import javax.swing.JOptionPane;
     
    import org.apache.log4j.BasicConfigurator;
     
    public class ResultSearch extends HttpServlet {
    	private static final long serialVersionUID = 1L;
    	private static String driverName = "org.apache.hive.jdbc.HiveDriver";
    	private static Connection con;
     
    	/**
    	 * @see HttpServlet#HttpServlet()
    	 */
    	public ResultSearch() {
    		super();
    		// TODO Auto-generated constructor stub
    	}
     
    	/**
    	 * @see Servlet#init(ServletConfig)
    	 */
    	public void init(ServletConfig config) throws ServletException {
    		// TODO Auto-generated method stub
    		con = null;
     
    		try {
    			Class.forName(driverName);
    			con = DriverManager.getConnection("jdbc:hive2://192.168.236.203:21050/;auth=noSasl");
     
    		} catch (Exception e1) {
    			// TODO Auto-generated catch block
    			e1.printStackTrace();
    		}
     
    	}
     
    	/**
    	 * @see Servlet#destroy()
    	 */
    	public void destroy() {
    		// TODO Auto-generated method stub
    	}
     
    	/**
    	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
    	 */
    	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    		// TODO Auto-generated method stub
     
     
     
     
    	}
     
    	/**
    	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
    	 */
    	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    		// TODO Auto-generated method stub
     
     
    		String activiteSaisie=request.getParameter("activite");
    		String localiteSaisie=request.getParameter("localite");
    		String rayonMax=request.getParameter("rayonMax");
    		String nbCible=request.getParameter("nombreCible");
    		RequestDispatcher rd=null;
     
     
    		response.setContentType("text/html");
    		PrintWriter out= response.getWriter();
     
    		out.println("<link type=\"text/css\" rel=\"stylesheet\" href=\"inc/formResultSearch.css\" />");
     
    		out.println("<html>");
    		out.println(" <img src=\"inc/logo.jpg\" width=\"18%\" height=\"15%\" align=\"right\" margin-right:50px/>");
    		out.println("<head>");
    		out.println("<meta charset='utf-8'/>");
    		out.println("<title>Test</title>");
    		out.println("</head>");
     
    		out.println("<body>");
     
     
    		out.println("<fieldset>");
     
    		out.println("<legend>Résultat de votre recherche</legend>");
     
     
    		Statement stmt = null;
    		try {
    			stmt = con.createStatement();
    		} catch (SQLException e) {
    			// TODO Auto-generated catch block
    			e.printStackTrace();
    		}
     
    		String sql;
    		BasicConfigurator.configure();
     
    		long debut=System.currentTimeMillis();
    		//sql = "select distinct * from new_search_db.etablissement as etablissement join new_search_db.etablissement_activite as etablissement_activite on etablissement.no_etab=etablissement_activite.no_etab join new_search_db.activite as activite on activite.no_rub_an9=etablissement_activite.no_rub_an9 where etablissement_activite.no_rub_an9="+activiteSaisie+" and cast(co_loc_resid_pj as int)="+localiteSaisie;
    		//sql="select count(distinct etablissement.no_etab) from new_search_db.etablissement as etablissement join new_search_db.etablissement_activite_lien as etablissement_activite_lien on etablissement.no_etab=etablissement_activite_lien.no_etab join new_search_db.activite as activite on activite.no_rub_an9=etablissement_activite_lien.no_rub_an9 join new_search_db.localite as localite on localite.co_localite_pj=cast(etablissement.co_loc_resid_pj as int) and localite.no_departement=cast(etablissement.co_dep_resid as int) where activite.no_rub_an9="+activiteSaisie+" and no_localite="+localiteSaisie;
    		sql="select count(distinct etablissement.no_etab) from new_search_db.etabs as etablissement join new_search_db.etablissement_activite_lien as etablissement_activite_lien on etablissement.no_etab=etablissement_activite_lien.no_etab join new_search_db.activite as activite on activite.no_rub_an9=etablissement_activite_lien.no_rub_an9 where activite.no_rub_an9=" + activiteSaisie+" and no_localite="+localiteSaisie;
    		//System.out.println("Requete: " + sql);
     
     
    		ResultSet res = null;
    		try {
    			res = stmt.executeQuery(sql);
     
    		} catch (SQLException e) {
    			// TODO Auto-generated catch block
    			e.printStackTrace();
    		}
    		try {
    			//res.last();
     
    			int i=0;
    			while(res.next()) i=res.getInt(1);
    			//System.out.println("  valeur de i "+i);
    			stmt.close();	
     
     
    			//if(res.getRow() >= Integer.parseInt(nbCible))
    			if (i>=Integer.parseInt(nbCible)){
     
    				out.println("Nombre de professionnels dans la localite : "+i+"<br>");
     
    				out.println("  UV=localité ");
     
     
     
    			}
     
    			else{ 
    				try {
    					stmt = con.createStatement();
    				} catch (SQLException e) {
    					// TODO Auto-generated catch block
    					e.printStackTrace();
    				}
    				String sql2;
    				//sql2="select distinct lepj.no_localite,lepj.no_etab,distance from new_search_db.localite_epj as lepj join new_search_db.localite as loc on lepj.no_localite=loc.no_localite join new_search_db.etablissement as eta on eta.no_etab=lepj.no_etab join new_search_db.etablissement_activite ea on ea.no_etab=eta.no_etab join new_search_db.activite as act on act.no_rub_an9=ea.no_rub_an9 where distance <="+rayonMax+" AND cast(co_localite_pj as int)="+localiteSaisie+" and act.no_rub_an9="+activiteSaisie +" order by distance ASC limit "+nbCible;
    				sql2="select distinct lepj.no_etab,distance from new_search_db.localite_epj_partitioned as lepj join new_search_db.localite as loc on lepj.no_localite=loc.no_localite join new_search_db.etablissement_activite_lien ea on ea.no_etab=lepj.no_etab join new_search_db.activite as act on act.no_rub_an9=ea.no_rub_an9 where distance <= "+rayonMax +" AND lepj.no_localite="+localiteSaisie +" and act.no_rub_an9="+activiteSaisie +" order by distance asc limit "+nbCible ;
    				double distance = 0;
    				//System.out.println("Requete :"+sql2);
    				ResultSet res2=null;
    				try {
    					res2 = stmt.executeQuery(sql2);
    					//System.out.println("aaaa");
    				} catch (SQLException e) {
    					// TODO Auto-generated catch block
    					e.printStackTrace();
    				}
    
    				//res.last();
    
    				int j=0;
    				while(res2.next()){
    					j++;
    					distance=res2.getDouble("distance");
    					//	System.out.println("  distance "+distance);
    					
    
    
    
    				}
    				stmt.close();
    				
    				distance=Math.round(100d*distance)/100d; 
    				out.println("Nombre de professionnels dans l'UV : "+j+"<br>");
    				out.println(" Rayon réel de l'UV: "+distance +" km <br> ");
    				
    				
    				String sql3;
    				try {
    					stmt = con.createStatement();
    				} catch (SQLException e) {
    					// TODO Auto-generated catch block
    					e.printStackTrace();
    				}
    				//sql3="select count( distinct lepj.no_etab), max(distance)from new_search_db.localite_epj as lepj join new_search_db.localite as loc on lepj.no_localite=loc.no_localite join new_search_db.etablissement as eta  on eta.no_etab=lepj.no_etab  join new_search_db.etablissement_activite ea  on ea.no_etab=eta.no_etab join new_search_db.activite as act on act.no_rub_an9=ea.no_rub_an9 where distance >"+distance+" AND cast(co_loc_resid_pj as int)="+localiteSaisie+" and cast(co_localite_pj as int)="+localiteSaisie+" and act.no_rub_an9="+activiteSaisie ; 
    				//sql3="select count(distinct lepj.no_etab),max(distance) from new_search_db.localite_epj_partitioned as lepj join new_search_db.localite as loc on lepj.no_localite=loc.no_localite join new_search_db.etablissement as eta on eta.no_etab=lepj.no_etab AND cast(co_loc_resid_pj as int)=loc.co_localite_pj and cast(co_dep_resid as int)=loc.no_departement join new_search_db.etablissement_activite_lien ea on ea.no_etab=eta.no_etab join new_search_db.activite as act on act.no_rub_an9=ea.no_rub_an9 where distance > "+rayonMax +" and act.no_rub_an9="+activiteSaisie +" and lepj.no_localite="+localiteSaisie;
    				sql3="select count(distinct lepj.no_etab),max(distance) from new_search_db.localite_epj_partitioned as lepj join new_search_db.etabs as eta on eta.no_etab=lepj.no_etab AND eta.no_localite=lepj.no_localite join new_search_db.etablissement_activite_lien ea on ea.no_etab=eta.no_etab join new_search_db.activite as act on act.no_rub_an9=ea.no_rub_an9 where distance >"+rayonMax +" and act.no_rub_an9= "+activiteSaisie +" and lepj.no_localite=" +localiteSaisie;
    				//System.out.println("Requete :"+sql3);
    				ResultSet res3=null;
    				try {
    					res3 = stmt.executeQuery(sql3);
    					//System.out.println("aaaa");
    				} catch (SQLException e) {
    					// TODO Auto-generated catch block
    					e.printStackTrace();
    				}
     
    				while (res3.next()){
    					double distanceMax=res3.getDouble(2);
    					distanceMax=Math.round(100d*distanceMax)/100d;
    					int nbPro=res3.getInt(1);
    					System.out.println("  nb "+nbPro);
    					if (nbPro>0){
    						out.println("Commune non couverte : "+nbPro+" pros de la localité ne sont pas dans l'UV <br>"); 
    						out.println("La distance entre le centre de la localité et le pro le plus éloigné dans la localité est de : "+distanceMax+" km<br>");
    					}
     
     
    				}stmt.close();
    			}
     
     
     
     
     
     
     
     
    			long fin=System.currentTimeMillis();
    	          long diff=fin-debut;
    	          out.println("<br> <div align=\"right\"> <font size=1> Opération terminée en: "+(diff/1000f)+"sec</div></font>");
     
     
    			//out.println("<iframe src=\"http://www.ma-carte-geographique.com/my-google-maps.php?SID=1402919269-fofW5\" width=\"470\" height=\"300\" frameborder=\"0\" hspace=\"0\" vspace=\"0\" marginheight=\"0\"marginwidth=\"0\" scrolling=\"no\"></iframe>");			
    			//out.println("<footer align=\"center\">  Business Solution <br> Copyright © 2014 </footer>");
    			out.println("</fieldset");
     
     
    		} catch (SQLException e) {
    			// TODO Auto-generated catch block
    			e.printStackTrace();
    		}
     
    		response.sendRedirect("*.html");
     
    		out.println("</body>");
     
    		out.println("</html>");
    		//request.getRequestDispatcher("*.html").forward(request, response);
     
    	}
     
     
    }
    web.xml
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    <?xml version="1.0" encoding="UTF-8"?>
    <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" version="3.0">
       <servlet>
        <servlet-name>Search</servlet-name>
        <servlet-class>servlet.classes.Search</servlet-class>
     
      </servlet>
      <servlet-mapping>
        <servlet-name>Search</servlet-name>
        <url-pattern>*.html</url-pattern>
      </servlet-mapping>
     
      <servlet>
        <servlet-name>ResultSearch</servlet-name>
        <servlet-class>servlet.classes.ResultSearch</servlet-class>
      </servlet>
      <servlet-mapping>
        <servlet-name>ResultSearch</servlet-name>
        <url-pattern>*.html</url-pattern>
      </servlet-mapping>
    </web-app>
    Merci à tous

  2. #2
    Membre confirmé Avatar de freddou17
    Homme Profil pro
    Ingénieur développement logiciels
    Inscrit en
    Avril 2013
    Messages
    341
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 35
    Localisation : France, Sarthe (Pays de la Loire)

    Informations professionnelles :
    Activité : Ingénieur développement logiciels
    Secteur : Service public

    Informations forums :
    Inscription : Avril 2013
    Messages : 341
    Points : 566
    Points
    566
    Par défaut
    Bonjour,

    Alors pourquoi tu n'as pas de jsp et pourquoi utilises tu 2 servlets?
    la sortie standard 'out' va être écrasée à chaque fois que l'une des deux servlets est appelées. Tu devrais plutôt utiliser une seule servlet et selon tel ou tel paramètre effectuer ta requete...

    Je pense que tu devrais re-penser un peu ton appli et repartir sur de bonne base (y a plein de petit tuto sur le net) car ton code va vite devenir illisible.

    Bon courage ++.
    "Aucun de nous ne sait ce que nous savons tous, ensemble."
    Lien vers mon appli Funcash n'hésitez pas à donner votre avis

  3. #3
    Candidat au Club
    Homme Profil pro
    Stagiaire développeur Java
    Inscrit en
    Juin 2014
    Messages
    6
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 33
    Localisation : France, Val de Marne (Île de France)

    Informations professionnelles :
    Activité : Stagiaire développeur Java
    Secteur : Conseil

    Informations forums :
    Inscription : Juin 2014
    Messages : 6
    Points : 3
    Points
    3
    Par défaut
    Oui j'ai finalement tout mis dans une même servlet ResultSearch.
    J'arrive a afficher le formulaire et la reponse en appelant la methode doGet() au debut de ma methode doPost() .
    Cependant, en procedant de cette façon , mon formulaire se recharge deux fois ce qui prend un peu de temps mais pas tres beau a voir.
    Aurais tu une solution?
    Merci encore.

    resultSearch.java:
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
    136
    137
    138
    139
    140
    141
    142
    143
    144
    145
    146
    147
    148
    149
    150
    151
    152
    153
    154
    155
    156
    157
    158
    159
    160
    161
    162
    163
    164
    165
    166
    167
    168
    169
    170
    171
    172
    173
    174
    175
    176
    177
    178
    179
    180
    181
    182
    183
    184
    185
    186
    187
    188
    189
    190
    191
    192
    193
    194
    195
    196
    197
    198
    199
    200
    201
    202
    203
    204
    205
    206
    207
    208
    209
    210
    211
    212
    213
    214
    215
    216
    217
    218
    219
    220
    221
    222
    223
    224
    225
    226
    227
    228
    229
    230
    231
    232
    233
    234
    235
    236
    237
    238
    239
    240
    241
    242
    243
    244
    245
    246
    247
    248
    249
    250
    251
    252
    253
    254
    255
    256
    257
    258
    259
    260
    261
    262
    263
    264
    265
    266
    267
    268
    269
    270
    271
    272
    273
    274
    275
    276
    277
    278
    279
    280
    281
    282
    283
    284
    285
    286
    287
    288
    289
    290
    291
    292
    293
    294
    295
    296
    297
    298
    299
    300
    301
    302
    303
    304
    305
    306
    307
    308
    309
    310
    311
    312
    313
    314
    315
    316
    317
    318
    319
    320
    321
    322
    323
    324
    325
    326
    327
    328
    329
    330
    331
    332
    333
    334
    335
    336
    337
    338
    339
    340
    341
    342
    343
    344
    345
    346
    347
    348
    349
    350
    351
    352
    353
    354
    355
    356
    357
    358
    359
    360
    361
    362
    363
    364
    365
    366
    367
    368
    369
    370
    371
    372
    373
    374
    375
    376
    377
    378
    379
    380
    381
    382
    383
    384
    385
    386
    387
    388
    389
    390
    391
    392
    393
    394
    395
    396
    397
    398
    399
    400
    401
    402
    package servlet.classes;
     
    import java.io.IOException;
    import java.io.PrintWriter;
    import java.sql.Connection;
    import java.sql.DriverManager;
    import java.sql.ResultSet;
    import java.sql.SQLException;
    import java.sql.Statement;
     
    import javax.servlet.FilterChain;
    import javax.servlet.RequestDispatcher;
    import javax.servlet.ServletConfig;
    import javax.servlet.ServletContext;
    import javax.servlet.ServletException;
    import javax.servlet.ServletRequest;
    import javax.servlet.ServletResponse;
    import javax.servlet.annotation.WebServlet;
    import javax.servlet.http.HttpServlet;
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;
    import javax.swing.JOptionPane;
     
    import org.apache.log4j.BasicConfigurator;
     
    public class ResultSearch extends HttpServlet {
    	private static final long serialVersionUID = 1L;
    	private static String driverName = "org.apache.hive.jdbc.HiveDriver";
    	private static Connection con;
     
    	/**
    	 * @see HttpServlet#HttpServlet()
    	 */
    	public ResultSearch() {
    		super();
    		// TODO Auto-generated constructor stub
    	}
     
    	/**
    	 * @see Servlet#init(ServletConfig)
    	 */
    	public void init(ServletConfig config) throws ServletException {
    		// TODO Auto-generated method stub
    		con = null;
     
    		try {
    			Class.forName(driverName);
    			con = DriverManager.getConnection("jdbc:hive2://192.168.236.203:21050/;auth=noSasl");
     
    		} catch (Exception e1) {
    			// TODO Auto-generated catch block
    			e1.printStackTrace();
    		}
     
    	}
     
    	/**
    	 * @see Servlet#destroy()
    	 */
    	public void destroy() {
    		// TODO Auto-generated method stub
    	}
     
    	/**
    	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
    	 */
    	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    		// TODO Auto-generated method stub
    		response.setContentType("text/html");
    		PrintWriter out= response.getWriter();
     
     
    		out.println("<link type=\"text/css\" rel=\"stylesheet\" href=\"inc/formSearch.css\" />");
    		out.println("<html>");
    		out.println(" <img src=\"inc/logo.jpg\" width=\"10%\" height=\"10%\" align=\"right\" margin-right:50px/>");
    		out.println("<head>");
    		out.println("<meta charset='utf-8'/>");
    		out.println("<title>Test</title>");
    		out.println("</head>");
    		out.println("<body onload=\"doPost(request,response)\">");
    		out.println("<p>Rechercher un professionnel près de chez vous</p>");
    		out.println("<form method=\"POST\" action=>");
    		out.println("<div align=\"center\"> <fieldset>");
    		out.println("<legend>Rechercher un professionnel</legend>");
    		out.println("<label for=\"activite\">Activité <span class=\"requis\">*</span></label>");
    		out.println("<select name=\"activite\">");
     
     
     
     
    		Statement stmt = null;
    		try {
    			stmt = con.createStatement();
    		} catch (SQLException e) {
    			// TODO Auto-generated catch block
    			e.printStackTrace();
    		}
    		String tableName = "new_search_db.activite";
     
     
    		String sql;
    		BasicConfigurator.configure();
     
     
    		sql = "select no_rub_an9,li_activite from " +tableName +" order by li_activite asc limit 3000";
    		//System.out.println("Requete: " + sql);
    		ResultSet res = null;
    		try {
    			res = stmt.executeQuery(sql);
    		} catch (SQLException e) {
    			// TODO Auto-generated catch block
    			e.printStackTrace();
    		}
    		try {
    			while (res.next()){
    				out.println("<option value=\""+res.getInt(1)+"\">"+res.getString(2)+"</option>");
    			}
    		} catch (SQLException e) {
    			// TODO Auto-generated catch block
    			e.printStackTrace();
    		}
    		try {
    			stmt.close();
    		} catch (SQLException e1) {
    			// TODO Auto-generated catch block
    			e1.printStackTrace();
    		}
    		out.println("</select> <br>");
     
    		out.println("<label for=\"localite\">Localité <span class=\"requis\">*</span></label>");
    		out.println("<select  name=\"localite\"> ");
    		Statement stmt2 = null;
    		try {
    			stmt2 = con.createStatement();
    		} catch (SQLException e) {
    			// TODO Auto-generated catch block
    			e.printStackTrace();
    		}
    		String tableName2 = "new_search_db.localite";
     
     
    		String sql2;
    		BasicConfigurator.configure();
     
     
    		sql2 = "select no_localite,li_localite from " +tableName2 +" order by li_localite asc limit 1000";
    		//System.out.println("Requete: " + sql2);
    		ResultSet res2 = null;
    		try {
    			res2 = stmt2.executeQuery(sql2);
    		} catch (SQLException e) {
    			// TODO Auto-generated catch block
    			e.printStackTrace();
    		}
    		try {
    			while (res2.next()){
    				out.println("<option value=\""+res2.getInt(1)+"\">"+res2.getString(2)+"</option>");
     
    			}
    		} catch (SQLException e) {
    			// TODO Auto-generated catch block
    			e.printStackTrace();
    		}
     
    		out.println("</select><br><br>");
    		out.println("<labelfor=\"Rayon\">Rayon Max <span class=\"requis\">");
    		out.println("</label>");
    		out.println("<input type=\"text\" id=\"rayonMax\" name=\"rayonMax\" size=\"2\" maxlength=\"4\" /> <br />");
    		out.println("<label for=\"nombreCible\">Nombre d'etablissements </label> <input type=\"text\" id=\"nbCible\" name=\"nombreCible\" size=\"1\" maxlength=\"6\" /><br /> <br>");
    		out.println("<input type=\"submit\" value=\"Search\" onclick=\"<% doPost(request,response);%>\"/>");
    		out.println("<div id=\"Gif\" align=\"center\" style=\"visibility: hidden; \"> <img src=\"inc/Gif.gif\"/> </div>");
    		out.println("<script language=\"javascript\"> function showGif(){ document.getElementById(\"Gif\").style.visibility=\"visible\"} </script>");
    		out.println("<footer align=\"center\">  Business Solutions <br> Copyright © 2014 </footer>");
    		out.println("</fieldset></div>");
    		out.println("</form>");
     
     
    		out.println("</body>");
     
    		out.println("</html>");
    		try {
    			stmt2.close();
    		} catch (SQLException e) {
    			// TODO Auto-generated catch block
    			e.printStackTrace();
    		}
     
    		//doPost(request,response);
    	}
     
     
     
     
     
     
     
     
    	/**
    	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
    	 */
    	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    		// TODO Auto-generated method stub
    		//doGet(request,response);
     
    		String activiteSaisie=request.getParameter("activite");
    		String localiteSaisie=request.getParameter("localite");
    		String rayonMax=request.getParameter("rayonMax");
    		String nbCible=request.getParameter("nombreCible");
     
     
    		response.setContentType("text/html");
    		PrintWriter out= response.getWriter();
     
    		out.println("<link type=\"text/css\" rel=\"stylesheet\" href=\"inc/formResultSearch.css\" />");
     
    		out.println("<html>");
    		out.println(" <img src=\"inc/logo.jpg\" width=\"18%\" height=\"15%\" align=\"right\" margin-right:50px/>");
    		out.println("<head>");
    		out.println("<meta charset='utf-8'/>");
    		out.println("<title>Test</title>");
    		out.println("</head>");
     
    		out.println("<body>");
     
     
    		out.println("<fieldset>");
     
    		out.println("<legend>Résultat de votre recherche</legend>");
     
     
    		Statement stmt = null;
    		try {
    			stmt = con.createStatement();
    		} catch (SQLException e) {
    			// TODO Auto-generated catch block
    			e.printStackTrace();
    		}
     
    		String sql;
    		BasicConfigurator.configure();
     
    		long debut=System.currentTimeMillis();
    		//sql = "select distinct * from new_search_db.etablissement as etablissement join new_search_db.etablissement_activite as etablissement_activite on etablissement.no_etab=etablissement_activite.no_etab join new_search_db.activite as activite on activite.no_rub_an9=etablissement_activite.no_rub_an9 where etablissement_activite.no_rub_an9="+activiteSaisie+" and cast(co_loc_resid_pj as int)="+localiteSaisie;
    		//sql="select count(distinct etablissement.no_etab) from new_search_db.etablissement as etablissement join new_search_db.etablissement_activite_lien as etablissement_activite_lien on etablissement.no_etab=etablissement_activite_lien.no_etab join new_search_db.activite as activite on activite.no_rub_an9=etablissement_activite_lien.no_rub_an9 join new_search_db.localite as localite on localite.co_localite_pj=cast(etablissement.co_loc_resid_pj as int) and localite.no_departement=cast(etablissement.co_dep_resid as int) where activite.no_rub_an9="+activiteSaisie+" and no_localite="+localiteSaisie;
    		sql="select count(distinct etablissement.no_etab) from new_search_db.etabs as etablissement join new_search_db.etablissement_activite_lien as etablissement_activite_lien on etablissement.no_etab=etablissement_activite_lien.no_etab join new_search_db.activite as activite on activite.no_rub_an9=etablissement_activite_lien.no_rub_an9 where activite.no_rub_an9=" + activiteSaisie+" and no_localite="+localiteSaisie;
    		//System.out.println("Requete: " + sql);
     
     
    		ResultSet res = null;
    		try {
    			res = stmt.executeQuery(sql);
     
    		} catch (SQLException e) {
    			// TODO Auto-generated catch block
    			e.printStackTrace();
    		}
    		try {
    			//res.last();
     
    			int i=0;
    			while(res.next()) i=res.getInt(1);
    			//System.out.println("  valeur de i "+i);
    			stmt.close();	
     
     
    			//if(res.getRow() >= Integer.parseInt(nbCible))
    			if (i>=Integer.parseInt(nbCible)){
     
    				out.println("Nombre de professionnels dans la localite : "+i+"<br>");
     
    				out.println("  UV=localité ");
     
     
     
    			}
     
    			else{ 
    				try {
    					stmt = con.createStatement();
    				} catch (SQLException e) {
    					// TODO Auto-generated catch block
    					e.printStackTrace();
    				}
    				String sql2;
    				//sql2="select distinct lepj.no_localite,lepj.no_etab,distance from new_search_db.localite_epj as lepj join new_search_db.localite as loc on lepj.no_localite=loc.no_localite join new_search_db.etablissement as eta on eta.no_etab=lepj.no_etab join new_search_db.etablissement_activite ea on ea.no_etab=eta.no_etab join new_search_db.activite as act on act.no_rub_an9=ea.no_rub_an9 where distance <="+rayonMax+" AND cast(co_localite_pj as int)="+localiteSaisie+" and act.no_rub_an9="+activiteSaisie +" order by distance ASC limit "+nbCible;
    				sql2="select distinct lepj.no_etab,distance from new_search_db.localite_epj_partitioned as lepj join new_search_db.localite as loc on lepj.no_localite=loc.no_localite join new_search_db.etablissement_activite_lien ea on ea.no_etab=lepj.no_etab join new_search_db.activite as act on act.no_rub_an9=ea.no_rub_an9 where distance <= "+rayonMax +" AND lepj.no_localite="+localiteSaisie +" and act.no_rub_an9="+activiteSaisie +" order by distance asc limit "+nbCible ;
    				double distance = 0;
    				//System.out.println("Requete :"+sql2);
    				ResultSet res2=null;
    				try {
    					res2 = stmt.executeQuery(sql2);
    					//System.out.println("aaaa");
    				} catch (SQLException e) {
    					// TODO Auto-generated catch block
    					e.printStackTrace();
    				}
    
    				//res.last();
    
    				int j=0;
    				while(res2.next()){
    					j++;
    					distance=res2.getDouble("distance");
    					//	System.out.println("  distance "+distance);
    
    
    
    
    				}
    				stmt.close();
    
    				distance=Math.round(100d*distance)/100d; 
    				out.println("Nombre de professionnels dans l'UV : "+j+"<br>");
    				out.println(" Rayon réel de l'UV: "+distance +" km <br> ");
    
    
    				String sql3;
    				try {
    					stmt = con.createStatement();
    				} catch (SQLException e) {
    					// TODO Auto-generated catch block
    					e.printStackTrace();
    				}
    				//sql3="select count( distinct lepj.no_etab), max(distance)from new_search_db.localite_epj as lepj join new_search_db.localite as loc on lepj.no_localite=loc.no_localite join new_search_db.etablissement as eta  on eta.no_etab=lepj.no_etab  join new_search_db.etablissement_activite ea  on ea.no_etab=eta.no_etab join new_search_db.activite as act on act.no_rub_an9=ea.no_rub_an9 where distance >"+distance+" AND cast(co_loc_resid_pj as int)="+localiteSaisie+" and cast(co_localite_pj as int)="+localiteSaisie+" and act.no_rub_an9="+activiteSaisie ; 
    				//sql3="select count(distinct lepj.no_etab),max(distance) from new_search_db.localite_epj_partitioned as lepj join new_search_db.localite as loc on lepj.no_localite=loc.no_localite join new_search_db.etablissement as eta on eta.no_etab=lepj.no_etab AND cast(co_loc_resid_pj as int)=loc.co_localite_pj and cast(co_dep_resid as int)=loc.no_departement join new_search_db.etablissement_activite_lien ea on ea.no_etab=eta.no_etab join new_search_db.activite as act on act.no_rub_an9=ea.no_rub_an9 where distance > "+rayonMax +" and act.no_rub_an9="+activiteSaisie +" and lepj.no_localite="+localiteSaisie;
    				sql3="select count(distinct lepj.no_etab),max(distance) from new_search_db.localite_epj_partitioned as lepj join new_search_db.etabs as eta on eta.no_etab=lepj.no_etab AND eta.no_localite=lepj.no_localite join new_search_db.etablissement_activite_lien ea on ea.no_etab=eta.no_etab join new_search_db.activite as act on act.no_rub_an9=ea.no_rub_an9 where distance >"+rayonMax +" and act.no_rub_an9= "+activiteSaisie +" and lepj.no_localite=" +localiteSaisie;
    				//System.out.println("Requete :"+sql3);
    				ResultSet res3=null;
    				try {
    					res3 = stmt.executeQuery(sql3);
    					//System.out.println("aaaa");
    				} catch (SQLException e) {
    					// TODO Auto-generated catch block
    					e.printStackTrace();
    				}
     
    				while (res3.next()){
    					double distanceMax=res3.getDouble(2);
    					distanceMax=Math.round(100d*distanceMax)/100d;
    					int nbPro=res3.getInt(1);
    					System.out.println("  nb "+nbPro);
    					if (nbPro>0){
    						out.println("Commune non couverte : "+nbPro+" pros de la localité ne sont pas dans l'UV <br>"); 
    						out.println("La distance entre le centre de la localité et le pro le plus éloigné dans la localité est de : "+distanceMax+" km<br>");
    					}
     
     
    				}stmt.close();
    			}
     
     
     
     
     
     
     
     
    			long fin=System.currentTimeMillis();
    			long diff=fin-debut;
    			out.println("<br> <div align=\"right\"> <font size=1> Opération terminée en: "+(diff/1000f)+"sec</div></font>");
     
     
    			//out.println("<iframe src=\"http://www.ma-carte-geographique.com/my-google-maps.php?SID=1402919269-fofW5\" width=\"470\" height=\"300\" frameborder=\"0\" hspace=\"0\" vspace=\"0\" marginheight=\"0\"marginwidth=\"0\" scrolling=\"no\"></iframe>");			
    			//out.println("<footer align=\"center\">  Business Solution <br> Copyright © 2014 </footer>");
    			out.println("</fieldset");
     
     
    		} catch (SQLException e) {
    			// TODO Auto-generated catch block
    			e.printStackTrace();
    		}
     
    		//response.sendRedirect("*.html");
     
    		out.println("</body>");
     
    		out.println("</html>");
    		//request.getRequestDispatcher("*.html").forward(request, response);
     
    	}
     
    	@Override
    	public void init() throws ServletException {
    		// TODO Auto-generated method stub
    		super.init();
     
    		con = null;
     
    		try {
    			Class.forName(driverName);
    			con = DriverManager.getConnection("jdbc:hive2://192.168.236.203:21050/;auth=noSasl");
     
    		} catch (Exception e1) {
    			// TODO Auto-generated catch block
    			e1.printStackTrace();
    		}
     
     
     
    	}
     
    }

  4. #4
    Candidat au Club
    Homme Profil pro
    Stagiaire développeur Java
    Inscrit en
    Juin 2014
    Messages
    6
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 33
    Localisation : France, Val de Marne (Île de France)

    Informations professionnelles :
    Activité : Stagiaire développeur Java
    Secteur : Conseil

    Informations forums :
    Inscription : Juin 2014
    Messages : 6
    Points : 3
    Points
    3
    Par défaut
    En fait je voudrais arriver à appeler ma methode doPost(requête,reponse) juste lorsque l'utilisateur clique sur le boutton search;
    Par là , je voudrais afficher le resultat de ma recherche juste en bas du formulaire ..
    J'ai pensé a créer une fonction JS qui fera appel à ma méthode Java (doPOst()) en rajoutant un "onClick="functionJS()" mais je n'ai pas réussi.
    Un peu d'aide svp..

  5. #5
    Membre confirmé Avatar de freddou17
    Homme Profil pro
    Ingénieur développement logiciels
    Inscrit en
    Avril 2013
    Messages
    341
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 35
    Localisation : France, Sarthe (Pays de la Loire)

    Informations professionnelles :
    Activité : Ingénieur développement logiciels
    Secteur : Service public

    Informations forums :
    Inscription : Avril 2013
    Messages : 341
    Points : 566
    Points
    566
    Par défaut
    Slt, regarde du coté de l'AJAX ICI

    ++
    "Aucun de nous ne sait ce que nous savons tous, ensemble."
    Lien vers mon appli Funcash n'hésitez pas à donner votre avis

Discussions similaires

  1. afficher deux champs dans la meme cellue d'une DBgrid
    Par kroma23 dans le forum Bases de données
    Réponses: 4
    Dernier message: 19/06/2011, 21h29
  2. Réponses: 29
    Dernier message: 26/04/2011, 09h28
  3. Confusion entre deux fond definie dans une meme page web
    Par marry dans le forum Balisage (X)HTML et validation W3C
    Réponses: 1
    Dernier message: 08/03/2010, 16h45
  4. Afficher deux colonnes dans une ListBox
    Par deubal dans le forum Composants VCL
    Réponses: 4
    Dernier message: 25/10/2005, 19h53
  5. plusieurs document.write dans une meme page
    Par Loko dans le forum Général JavaScript
    Réponses: 4
    Dernier message: 05/07/2005, 17h16

Partager

Partager
  • Envoyer la discussion sur Viadeo
  • Envoyer la discussion sur Twitter
  • Envoyer la discussion sur Google
  • Envoyer la discussion sur Facebook
  • Envoyer la discussion sur Digg
  • Envoyer la discussion sur Delicious
  • Envoyer la discussion sur MySpace
  • Envoyer la discussion sur Yahoo