0% found this document useful (0 votes)
110 views

Guessing Game

This document describes creating a guessing game using an MVC architecture in Java servlets. It involves: 1) Creating servlets Guess.java and GuessAction.java that initialize the target value, compare guesses, and direct flow. 2) Creating JSP pages like start.jsp and continueGuessing.jsp that display the game interface and redirect guesses. 3) Using a Info.java bean to track attempts and feedback between requests. 4) The GuessAction servlet compares guesses to the target, updates the bean, and determines which page is rendered next.

Uploaded by

prasath
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
110 views

Guessing Game

This document describes creating a guessing game using an MVC architecture in Java servlets. It involves: 1) Creating servlets Guess.java and GuessAction.java that initialize the target value, compare guesses, and direct flow. 2) Creating JSP pages like start.jsp and continueGuessing.jsp that display the game interface and redirect guesses. 3) Using a Info.java bean to track attempts and feedback between requests. 4) The GuessAction servlet compares guesses to the target, updates the bean, and determines which page is rendered next.

Uploaded by

prasath
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 9

GUESSING GAME - MVC JAVASERVLET

AIM:

To create a MVC architecture for guessing game using JavaServlet.

ALGORITHM:

Step 1:Start

Step 2: First create a new directory named as GuessingGame in Tomcat 6.0->webapps.


In that create another directory WEB-INF.

Step 3: Create a directory in WEB-INF named as Guessing.In that,create directory


classes and web.xml file.

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 6: Inside GuessingGame directory, create files named as start.jsp, continue


Guessing.jsp, GameOver.jsp and GuessRequest.html. Now start the Tomcat server and
then execute GuessingGame.

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.

Step 11: In GuessingOver, It displays the message “Congrats”. If user wants to


continue, then click “Yes”, else “No”.

Step 12: Stop.


PROGRAM:

//Servlet

Guess.java

import javax.servlet.*;

import javax.servlet.http.*;

import Guessing.Info;

public class Guess extends HttpServlet

public void doGet(HttpServletRequest request,

HttpServletResponse response)

throws ServletException, java.io.IOException

HttpSession session = request.getSession();

int target = (int)(Math.random() * 100);

session.setAttribute("target", "" + target);

session.setAttribute("info", new Info());

request.getRequestDispatcher("/start.jsp").

forward(request, response);

public void doPost(HttpServletRequest request,

HttpServletResponse response)

throws ServletException, java.io.IOException

{ doGet(request, response);}}
Info.java

package Guessing;

public class Info

private int attempts = 1;

private String message = "";

public int getAttempts() { return attempts; }

public void setAttempts(int value) { attempts = value; }

public String getMessage() { return message; }

public void setMessage(String value) { message = value; }

start.jsp

<H1>Guessing Game</H1>

<B>Welcome to Guessing Game</B>

<%@ include file="GuessRequest.htm" %>

GuessRequest.html

<FORM action="http:GuessAction" method="GET">

Enter your guess:

<INPUT id="guessInput" type="text" name="guess"/>

<INPUT type="submit" value="Send"/>

</FORM>
//Servlet

GuessAction.java

import javax.servlet.*;

import javax.servlet.http.*;

import Guessing.Info;

public class GuessAction extends HttpServlet

public void doGet(HttpServletRequest request,

HttpServletResponse response)

throws ServletException, java.io.IOException

int attempts = 1;

int guess = -1;

int target = 0;

String forwardPage = "/ContinueGuessing.jsp";

String message = "Aim higher";

HttpSession session = request.getSession();

target =

Integer.parseInt(

session.getAttribute("target").toString());

Info info = (Info) session.getAttribute("info");

attempts = info.getAttempts();

attempts++;
info.setAttempts(attempts);

guess = Integer.parseInt(

request.getParameter("guess"));

if (guess == target)

forwardPage = "/GameOver.jsp";

message = "Congratulations!";

else

if (guess < target)

message = "Aim higher!";

else

message = "Aim lower!";

info.setMessage(message);

request.getRequestDispatcher(forwardPage).

forward(request, response);

public void doPost(HttpServletRequest request,

HttpServletResponse response)

throws ServletException, java.io.IOException

{ doGet(request, response); }}
ContinueGuessing.jsp

<H1>Guessing Game</H1>

<jsp:useBean id="info" scope="session" class="Guessing.Info" />

Number of attempts

<jsp:getProperty name="info" property="attempts"/><BR>

<jsp:getProperty name="info" property="message"/>

<%@ include file="GuessRequest.html" %>

GameOver.jsp

<H1>Guessing Game</H1>

<jsp:useBean id="info" scope="session" class="Guessing.Info" />

Number of Attempts

<jsp:getProperty name="info" property="attempts"/><BR>

<jsp:getProperty name="info" property="message"/><BR>

Would you like to start a new game?

<FORM action="Guess" method="POST">

<INPUT type="submit" value="Yes"/>

<INPUT type="submit" value="No" onclick="window.close()"/>

</FORM>
OUTPUT:
RESULT
Thus the Guessing Game using MVC architecture in Java servlet has been
created and executed successfully.

You might also like