Guessing Game
Guessing Game
AIM:
ALGORITHM:
Step 1:Start
Step 4: Now create Info.java inside the Guessing dir. The Info.java holds two
properties attempts and message. Compile this file and generate Info.class file.
Step 5: In WEB-INF dir, create the java servlets named as Guess.java and
GuessAction.java. Compile these files and generate class files.
Step 7: Guess.java servlet initialize the target value to guess by the user, sets session
variables and transfer the control into the start.jsp page.
Step 8: the start.jsp page, include the file GuessRequest.html.In this, it gets the value
from the user and user to the GuessAction.java servlet.
Step 9: The GuessAction servlet compares the guess value with target, if it is low then
target, it sets the message “Aim Higher”, else “Aim Lower”.
Step 10: If guess=target, then the control transfer into GuessingOver, otherwise
transfer the control into conitueGuessing.jsp. In continueGuessing, it directs user into
GuessRequest.html and repeat step 8 to 9.
//Servlet
Guess.java
import javax.servlet.*;
import javax.servlet.http.*;
import Guessing.Info;
HttpServletResponse response)
request.getRequestDispatcher("/start.jsp").
forward(request, response);
HttpServletResponse response)
{ doGet(request, response);}}
Info.java
package Guessing;
start.jsp
<H1>Guessing Game</H1>
GuessRequest.html
</FORM>
//Servlet
GuessAction.java
import javax.servlet.*;
import javax.servlet.http.*;
import Guessing.Info;
HttpServletResponse response)
int attempts = 1;
int target = 0;
target =
Integer.parseInt(
session.getAttribute("target").toString());
attempts = info.getAttempts();
attempts++;
info.setAttempts(attempts);
guess = Integer.parseInt(
request.getParameter("guess"));
if (guess == target)
forwardPage = "/GameOver.jsp";
message = "Congratulations!";
else
else
info.setMessage(message);
request.getRequestDispatcher(forwardPage).
forward(request, response);
HttpServletResponse response)
{ doGet(request, response); }}
ContinueGuessing.jsp
<H1>Guessing Game</H1>
Number of attempts
GameOver.jsp
<H1>Guessing Game</H1>
Number of Attempts
</FORM>
OUTPUT:
RESULT
Thus the Guessing Game using MVC architecture in Java servlet has been
created and executed successfully.