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