Mimi1979
asked on
How to send a parameter from a JSP to another JSP
I have an HTLM page 'index.html' who gives the user a link to click on. This link calls a servlet where the query is performed and then it dispatches the request to a JSP page that display several categories in a table from a Database and where the user needs to select one of the categories to display the items of that categorie. I want to use another JSP to retrieve the request (category selected) and query the DB and then display the items into another html page.
ASKER
Hi
Any ideas, suggestions or comments....??????
Any ideas, suggestions or comments....??????
in order to submit the a form to a jsp / servlet you have to use the ACTION parameter of that form..
just set to whatever jsp/servlet you want it to go..
then in that servlet/jsp you use request.getparameter("cate gorie");..
run your query then then forward to wherever you want to display the result.
just set to whatever jsp/servlet you want it to go..
then in that servlet/jsp you use request.getparameter("cate
run your query then then forward to wherever you want to display the result.
You can store your parameters in your request itself using request.setAttribute("para mName", value);
and in the destination jsp you can retrieve the same parameter using request.getParameter("para mName").
You can even use session variable to shere values between pages.
or you can pass as an argument thru url like: <jsp:forward page="sam.jap?paramName=va lue" />
-Vinod
and in the destination jsp you can retrieve the same parameter using request.getParameter("para
You can even use session variable to shere values between pages.
or you can pass as an argument thru url like: <jsp:forward page="sam.jap?paramName=va
-Vinod
You can try using session or even passing param thru request.
for request.. in your JSP
<jsp:forward page="a.jap?paramName=valu e" />
and at another JSP, you can get the parameter by calling..
<%
...
String paramNameStr = request.getParameter("para mName");
%>
-------------------------- ---------- ---------- ---------- -------
Another way would be storing it in session where in your jsp...
<%
..
session.setAttribute("para mName", "value");
%>
<jsp:forward page="a.jap?" />
and at another JSP, you can get the parameter by calling..
<%
...
String paramNameStr = session.getAttrribute("par amName");
%>
for request.. in your JSP
<jsp:forward page="a.jap?paramName=valu
and at another JSP, you can get the parameter by calling..
<%
...
String paramNameStr = request.getParameter("para
%>
--------------------------
Another way would be storing it in session where in your jsp...
<%
..
session.setAttribute("para
%>
<jsp:forward page="a.jap?" />
and at another JSP, you can get the parameter by calling..
<%
...
String paramNameStr = session.getAttrribute("par
%>
ASKER
Hi amateur83
If I want to use session, before setting the parameter here is where I am not sure how to proceed to get the value from the input 'categorie'
Should I use the following
<%! String nomCate=null; %>
<% nomCate=request.getParamet er("catego rie");
session.setAttribute("cate gorie", nomCate); %>
<jsp:forward page="a.jsp?" />
If I want to use session, before setting the parameter here is where I am not sure how to proceed to get the value from the input 'categorie'
Should I use the following
<%! String nomCate=null; %>
<% nomCate=request.getParamet
session.setAttribute("cate
<jsp:forward page="a.jsp?" />
that should work.
ASKER
Well, when I call the parameter nomCate and display the value I get 'on'.
I am not sure why
I am not sure why
which means your radio button is selected.
ASKER
But how do I get the value from nomCate to do my query in the DB?? if I am getting on I tried the query and I am not getting any rows...
ASKER
I think I am a little confused here. This is what I have done so far and it`s not working for me
queryCategories.java --> query the DB (works) and forward the request to AffichageCategories.jsp
try {
RequestDispatcher dispatcher =
getServletContext().getReq uestDispat cher("/Aff ichageCate gories.jsp ");
dispatcher.forward(request , response);
AffichageCategories --> display the categories
......
<% HttpSession uneSession = (HttpSession)request.getAt tribute("s ession");
Vector donnees = (Vector)request.getAttribu te("donnee s");
Categorie uneCate;
for( int i = 0;i<donnees.size();i++){
uneCate = (Categorie)donnees.element At(i); %>
<tr>
<td>
<%=uneCate.getNomCategorie ()%>
</td>
<td>
<input type="radio" name="categorie"/>
</td>
</tr>
<%
}
%>
</table>
<input type="submit" name="Soumettre" value="Soumettre"></input>
<br></br>
<br></br>
</form>
</center>
</body>
</html>
PiecesParCate.jsp --> to get the param categorie from request and forward it to the getCateChoisie.java
.....
<%
String cate = request.getParameter("cate gorie");
if((cate!=null) && (!cate.equals("")))
{
%>
<jsp:forward page="/getCateChoisie.java " >
<jsp:param name="categorie" value= "<%= cate %>" />
</jsp:forward>
<h3>Categorie choisie dans jsp:<%= cate %> </H3>
<table cellspacing="2" border="1" width="100%">
<tr>
<td></td>
<td> </td>
</tr>
<tr>
<td>
</td>
<td>
</td>
</tr>
</table>
<%
}
else
{
%>
<h3> Cate est null </H3>
<%
}
%>
</BODY>
</HTML>
getCateChoisie---> this dont display any rows
.........
public void doPost(HttpServletRequest request,
HttpServletResponse response) throws ServletException,
IOException {
PrintWriter sortie;
String numCatChoisie = request.getParameter("cate gorie");
response.setContentType("t ext/html") ;
sortie = response.getWriter();
// envoie la page HTML au client avec la categorie chosie
sortie.println("<HTML><HEA D><TITLE>" );
sortie.println("Table de la categorie choisie");
sortie.println("</TITLE>") ;
sortie.println("<link rel=stylesheet type=\"text/css\" href=\"style1.css\"></HEAD ><BODY>");
sortie.println("<p align=\"center\">Categorie choisie : " + numCatChoisie + "</P>");
//Creation de l<objet pour la requete
Statement stat = null;
//Creating a ResulSet object
ResultSet resultat = null;
try {
//Creating the connexion
stat = connexion.createStatement( );
String requete = "SELECT Pieces.no_categorie, Pieces.description, Fabricants.nom_fabricant, " +
"Pieces.prix_vente FROM Pieces, Fabricants " +
"WHERE Pieces.no_fabricant=Fabric ants.no_fa bricant " +
"AND Fabricants.nom_fabricant IN " +
"(SELECT nom_fabricant FROM Fabricants " +
"WHERE no_fabricant IN (SELECT no_fabricant FROM Pieces WHERE no_categorie='" + numCatChoisie+ "')) " +
"AND Pieces.no_categorie='" + numCatChoisie+ "'";
resultat = stat.executeQuery(requete) ;
} catch (Exception e) {
System.err.println("ERREUR : problème à la lecture de la selection");
e.printStackTrace();
}
// Reading the ResulSet object
if (resultat != null) {
sortie.println("<TABLE table cellspacing=\"3\" cellpadding=\"2\" border=\"1\" width=\"100%\">");
sortie.println("<TR><TH><H 2>" + "Numero de categorie" + "</H2></TH>");
sortie.println("<TD><H2>" + "Description" + "</H2></TD>");
sortie.println("<TD><H2>" + "Nom fabricant" + "</H2></TD>");
sortie.println("<TD><H2>" + "Prix vente" + "</H2></TD></TR>");
try {
while (resultat.next()) {
//prender les valeurs de la premiere requete
String numCat = resultat.getString(1);
String des = resultat.getString(2);
String nomFab = resultat.getString(3);
String prix = resultat.getString(4);
sortie.println("<TR><TH>" + numCat + "</TH>");
sortie.println("<TD>" + des + "</TD>");
sortie.println("<TD>" + nomFab + "</TD>");
sortie.println("<TD>" + prix + "</TD></TR>");
}
}
catch (SQLException ex) {
ex.printStackTrace();
}
sortie.println("</TABLE>") ;
} else
sortie.print("<H2>Une erreur s'est produite. " +
"Veuillez réessayer plus tard.</H2>");
sortie.println("</BODY></H TML>");
sortie.close(); // clôturer le flux.
}
}
queryCategories.java --> query the DB (works) and forward the request to AffichageCategories.jsp
try {
RequestDispatcher dispatcher =
getServletContext().getReq
dispatcher.forward(request
AffichageCategories --> display the categories
......
<% HttpSession uneSession = (HttpSession)request.getAt
Vector donnees = (Vector)request.getAttribu
Categorie uneCate;
for( int i = 0;i<donnees.size();i++){
uneCate = (Categorie)donnees.element
<tr>
<td>
<%=uneCate.getNomCategorie
</td>
<td>
<input type="radio" name="categorie"/>
</td>
</tr>
<%
}
%>
</table>
<input type="submit" name="Soumettre" value="Soumettre"></input>
<br></br>
<br></br>
</form>
</center>
</body>
</html>
PiecesParCate.jsp --> to get the param categorie from request and forward it to the getCateChoisie.java
.....
<%
String cate = request.getParameter("cate
if((cate!=null) && (!cate.equals("")))
{
%>
<jsp:forward page="/getCateChoisie.java
<jsp:param name="categorie" value= "<%= cate %>" />
</jsp:forward>
<h3>Categorie choisie dans jsp:<%= cate %> </H3>
<table cellspacing="2" border="1" width="100%">
<tr>
<td></td>
<td> </td>
</tr>
<tr>
<td>
</td>
<td>
</td>
</tr>
</table>
<%
}
else
{
%>
<h3> Cate est null </H3>
<%
}
%>
</BODY>
</HTML>
getCateChoisie---> this dont display any rows
.........
public void doPost(HttpServletRequest request,
HttpServletResponse response) throws ServletException,
IOException {
PrintWriter sortie;
String numCatChoisie = request.getParameter("cate
response.setContentType("t
sortie = response.getWriter();
// envoie la page HTML au client avec la categorie chosie
sortie.println("<HTML><HEA
sortie.println("Table de la categorie choisie");
sortie.println("</TITLE>")
sortie.println("<link rel=stylesheet type=\"text/css\" href=\"style1.css\"></HEAD
sortie.println("<p align=\"center\">Categorie
//Creation de l<objet pour la requete
Statement stat = null;
//Creating a ResulSet object
ResultSet resultat = null;
try {
//Creating the connexion
stat = connexion.createStatement(
String requete = "SELECT Pieces.no_categorie, Pieces.description, Fabricants.nom_fabricant, " +
"Pieces.prix_vente FROM Pieces, Fabricants " +
"WHERE Pieces.no_fabricant=Fabric
"AND Fabricants.nom_fabricant IN " +
"(SELECT nom_fabricant FROM Fabricants " +
"WHERE no_fabricant IN (SELECT no_fabricant FROM Pieces WHERE no_categorie='" + numCatChoisie+ "')) " +
"AND Pieces.no_categorie='" + numCatChoisie+ "'";
resultat = stat.executeQuery(requete)
} catch (Exception e) {
System.err.println("ERREUR
e.printStackTrace();
}
// Reading the ResulSet object
if (resultat != null) {
sortie.println("<TABLE table cellspacing=\"3\" cellpadding=\"2\" border=\"1\" width=\"100%\">");
sortie.println("<TR><TH><H
sortie.println("<TD><H2>" + "Description" + "</H2></TD>");
sortie.println("<TD><H2>" + "Nom fabricant" + "</H2></TD>");
sortie.println("<TD><H2>" + "Prix vente" + "</H2></TD></TR>");
try {
while (resultat.next()) {
//prender les valeurs de la premiere requete
String numCat = resultat.getString(1);
String des = resultat.getString(2);
String nomFab = resultat.getString(3);
String prix = resultat.getString(4);
sortie.println("<TR><TH>" + numCat + "</TH>");
sortie.println("<TD>" + des + "</TD>");
sortie.println("<TD>" + nomFab + "</TD>");
sortie.println("<TD>" + prix + "</TD></TR>");
}
}
catch (SQLException ex) {
ex.printStackTrace();
}
sortie.println("</TABLE>")
} else
sortie.print("<H2>Une erreur s'est produite. " +
"Veuillez réessayer plus tard.</H2>");
sortie.println("</BODY></H
sortie.close(); // clôturer le flux.
}
}
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
RequestDispatcher dispatcher = getServletContext().getReq
dispatcher.forward(request
This is my first JSP
........
<body><center>
Voici les categories de la table, veuillez faire votre choix et soumettre
<form name="form1" method="POST" action="AffichagePieces.js
<table>
<tr>
<td>Nom</td>
<td> </td>
</tr>
<!-- directive 'page' qui importe un paquetage java et labo -->
<%@ page import="labo4_servletjsp.*
<!-- Scriplet: bloc de code Java qui est place dans _jspService() -->
<%
Vector donnees = (Vector)request.getAttribu
Categorie uneCate;
for( int i = 0;i<donnees.size();i++){
uneCate = (Categorie)donnees.element
%>
<tr>
<td>
<%=uneCate.getNomCategorie
</td>
<td>
<input type="radio" name="categorie"/>
</td>
</tr>
<%
}
%>
</table>
<input type="submit" name="Soumettre" value="Soumettre"></input>
</form>
<%! String nomCate=null; %>
<% nomCate=request.getParamet
<jsp:forward page="AffichagePieces.jsp"
<jsp:param name="categorie" value="nomCate"/>
</jsp:forward>
</center></body>
</html>
I want to use the forward method to send the input to another JSP page, but I am not sure how if I am calling the jsp from a FORM, even here I am not sure if I am doing correctly
Thank you in advance
Mimi