matthew016
asked on
jsp:forward ?
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)sessio n.getAttri bute("jeu" );
if (jeu == null) {
jeu = new jeu.JeuDeDevinette();
session.setAttribute("jeu" , jeu);
}
String essai = request.getParameter("essa i");
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é<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("j eu");
%>
<jsp:forward page="stat.jsp"/>
<%
}
else if (jeu.getNombreDEssaisResta nt() == 0) {
out.println("Dé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("j eu");
}
else { // partie en cours, on n'a pas gagné ni perdu
out.print("Il vous reste ");
out.print(jeu.getNombreDEs saisRestan t());
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>
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)sessio
if (jeu == null) {
jeu = new jeu.JeuDeDevinette();
session.setAttribute("jeu"
}
String essai = request.getParameter("essa
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é<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("j
%>
<jsp:forward page="stat.jsp"/>
<%
}
else if (jeu.getNombreDEssaisResta
out.println("Désol&
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("j
}
else { // partie en cours, on n'a pas gagné ni perdu
out.print("Il vous reste ");
out.print(jeu.getNombreDEs
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>
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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
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
ASKER
isn't this already possible with html to navigate trough pages ?
why use jsp:forward and not an html request
why use jsp:forward and not an html request
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.
ASKER
Thank you :-)
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.
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.
ASKER
Thanks for the additional info .
ASKER
What is the real use of jsp:forward then ?
Thx