Link to home
Start Free TrialLog in
Avatar of Mimi1979
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.
Avatar of Mimi1979
Mimi1979

ASKER

This os where I call my first JPS page to  display the different categories:

 RequestDispatcher dispatcher = getServletContext().getRequestDispatcher("/AffichageCategories.jsp");
       dispatcher.forward(request, response);

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.jsp">
        <table>
          <tr>
            <td>Nom</td>
            <td>&nbsp;</td>
          </tr>
          <!-- directive 'page' qui importe un paquetage java et labo -->
          <%@ page import="labo4_servletjsp.*,java.util.*, javax.servlet.*"%>
          <!-- Scriplet: bloc de code Java qui est place dans _jspService() -->
          <%
    Vector donnees = (Vector)request.getAttribute("donnees");
    Categorie uneCate;

    for( int i = 0;i<donnees.size();i++){
      uneCate = (Categorie)donnees.elementAt(i);
  %>
          <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.getParameter("categorie"); %>
      <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
Hi
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("categorie");..

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("paramName", value);
and in the destination jsp you can retrieve the same parameter using request.getParameter("paramName").
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=value" />
-Vinod
You can try using session or even passing param thru request.

for request.. in your JSP
<jsp:forward page="a.jap?paramName=value" />
and at another JSP, you can get the parameter by calling..
<%
...
String paramNameStr = request.getParameter("paramName");
%>
---------------------------------------------------------------
Another way would be storing it in session where in your jsp...
<%
..
session.setAttribute("paramName", "value");
%>
<jsp:forward page="a.jap?" />
and at another JSP, you can get the parameter by calling..
<%
...
String paramNameStr = session.getAttrribute("paramName");
%>
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.getParameter("categorie");
          session.setAttribute("categorie", nomCate); %>

<jsp:forward page="a.jsp?" />

that should work.
Well, when I call the parameter nomCate and display the value  I get 'on'.
I am not sure why
which means your radio button is selected.
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...
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().getRequestDispatcher("/AffichageCategories.jsp");
                    dispatcher.forward(request, response);

AffichageCategories --> display the categories
......
 <% HttpSession uneSession = (HttpSession)request.getAttribute("session");
            Vector donnees = (Vector)request.getAttribute("donnees");
            Categorie uneCate;

            for( int i = 0;i<donnees.size();i++){
                uneCate = (Categorie)donnees.elementAt(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("categorie");
    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>&nbsp;</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("categorie");

        response.setContentType("text/html");
        sortie = response.getWriter();

        // envoie la page HTML au client avec la categorie chosie
        sortie.println("<HTML><HEAD><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=Fabricants.no_fabricant " +
            "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><H2>" + "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></HTML>");
        sortie.close(); // clôturer le flux.
    }
}
ASKER CERTIFIED SOLUTION
Avatar of Kuldeepchaturvedi
Kuldeepchaturvedi
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial