Solved

jsp:forward ?

Posted on 2006-06-21
10
349 Views
Last Modified: 2010-04-01
Hi,
I'm new to JSP
I code a simple game in JSP,
where the user has to guess a number between 0 and 9

if he wins, he gets a congrats message, if he looses, another message is displayed.
If he wins OR loses, the statistics (number games played, ...) are displayed. So I have this code :

if user wins
   display "congrats" message
   display stats
else
   display "u lose" message
   display stats

I read in a book that I need , after displaying the messages, use
<jsp:forward page = stats.jsp"/>
to display the statistics because it is common code and better for refactoring.
(its just an educational example)
So I tried to use the forward:jsp action but I lose the messages 'congrats' and 'u lose'

<html>
  <head>
    <title>Jeu de devinette</title>
  </head>
  <body>
    <h3>Jeu de devinette</h3>
    <hr/>

<%// récupération de la partie en cours, au besoin création et rangement d'une nouvelle partie

  jeu.JeuDeDevinette jeu
              = (jeu.JeuDeDevinette)session.getAttribute("jeu");
  if (jeu == null) {
      jeu = new jeu.JeuDeDevinette();
      session.setAttribute("jeu", jeu);
  }

   String essai = request.getParameter("essai");
   out.print("ESSAIIII "+essai+"<br/>");
  // si l'utilisateur arrive pour la premiere fois sur la page, il n'a pas encore fourni de valeur
  if (essai != null) {
      jeu.setEssai(essai);
  }

  if (jeu.isGagne()) {
      out.println("Bravo, vous avez gagn&eacute;<br/>");
      out.println("<a href='jeu.jsp'>Rejouer</a>");
      // si l'utilisateur revient sur la page, il doit commencer automatiquement une nouvelle partie
      session.removeAttribute("jeu");
  %>
  <jsp:forward page="stat.jsp"/>
  <%
  }
  else if (jeu.getNombreDEssaisRestant() == 0) {
            out.println("D&eacute;sol&eacute;, vous avez perdu<br/>");
        out.print("Il fallait trouver ");
        out.println(jeu.getValeur());
        out.println("<br/><a href='jeu.jsp'>Rejouer</a>");
        // si l'utilisateur revient sur la page, il doit commencer automatiquement une nouvelle partie
        session.removeAttribute("jeu");
  }
  else {  // partie en cours, on n'a pas gagné ni perdu
      out.print("Il vous reste ");
      out.print(jeu.getNombreDEssaisRestant());
      out.println(" essai(s)<br/>");
      out.println("<form>");
      out.println("  essai : <input type='text' name='essai'/><br/>");
      out.println("  <input type='submit' value='Essayer'/>");
      out.println("</form>");

      // on triche en affichant la valeur à  trouver, pour faciliter les tests
      out.println(jeu.getValeur());
  }
%>

</body>
</html>

0
Comment
Question by:matthew016
  • 4
  • 3
  • 2
  • +1
10 Comments
 
LVL 23

Accepted Solution

by:
Siva Prasanna Kumar earned 250 total points
ID: 16953210
if you want to show both Message as well as stats use <jsp:include page="stat.jsp"/> instead of
<jsp:forward page="stat.jsp"/>

Thank You.

0
 
LVL 3

Assisted Solution

by:runa_paathak
runa_paathak earned 150 total points
ID: 16957308
Else if you want to use <jsp:forward page = stats.jsp"/>, add the message string into request (request.setAttribute("msg"), msg) and then do <jsp:forward page = stats.jsp"/>. Display the message itself on stats.jsp by doing out.println(request.getAttribute("msg"))

Hope this helps.
0
 
LVL 9

Author Comment

by:matthew016
ID: 16962381
Thank u,
What is the real use of jsp:forward then ?
Thx
0
Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

 
LVL 4

Assisted Solution

by:evnafets
evnafets earned 100 total points
ID: 16964216
You use a jsp:forward, when you want to cancel the current page and display a different one.
A jsp:include, includes the RESULT of invoking the url specified into the current page, at the current point.

0
 
LVL 23

Expert Comment

by:Siva Prasanna Kumar
ID: 16965750
Actuattly jsp:forward is very useful when you want to navigate from one JSP to another.
and when you say include it jsp:includes the secified JSP to the current JSP at runtime.

check out for better details

http://java.sun.com/products/jsp/tags/11/syntaxref11.fm9.html 
0
 
LVL 9

Author Comment

by:matthew016
ID: 16968614
isn't this already possible with html to navigate trough pages ?
why use jsp:forward and not an html request
0
 
LVL 23

Expert Comment

by:Siva Prasanna Kumar
ID: 16969579
what if you want to pass some active stuff between the pages??? and how will you maintain the sessions??? Lot many other reasons are there.
0
 
LVL 9

Author Comment

by:matthew016
ID: 16969645
Thank you :-)
0
 
LVL 4

Expert Comment

by:evnafets
ID: 16976994
A forward/include is different from HTML navigating the page, because it is all done in the same HTTP request.
To the client browser, all of the forwards/includes are invisible.
All it knows is it requested a url - eg stat.jsp

To build stat.jsp the server might use header.jsp, footer.jsp, statHeader.jsp and queryResults.jsp to make the final page - but the client knows nothing about it - all it knows is "stat.jsp" was requested, and it got back some HTML.

Forward lets you change your mind about which page to display, include lets you use a template multiple times in your site, without writing it every time.

0
 
LVL 9

Author Comment

by:matthew016
ID: 16977045
Thanks for the additional info .
0

Featured Post

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
unit test DAO layer 1 137
Coldfusion 10 - JSP File Not Found - 404 Error 5 93
java operators 3 116
eclipse buid path vs tomcat lib path 10 24
If you thought ransomware was bad, think again! Doxware has the potential to be even more damaging.
Each year, investment in cloud platforms grows more than 20% (https://www.immun.io/hubfs/Immunio_2016/Content/Marketing/Cloud-Security-Report-2016.pdf?submissionGuid=a8d80a00-6fee-4b85-81db-a4e28f681762) as an increasing number of companies begin to…
This video shows how to use Hyena, from SystemTools Software, to bulk import 100 user accounts from an external text file. View in 1080p for best video quality.
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…

803 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question