Q22
Question 22
Let the user enter a word in a textbox and let her/him select one of even or odd radio buttons. If she/he selects odd, check the odd positions in the word entered, if they all contain vowels, then display the message ‘You win’, else display ‘You lose’. Similarly, if the user selects even, check for vowels in all even positions in the word entered.
Code
<%--
  Created by IntelliJ IDEA.
  User: HP
  Date: 24-04-2021
  Time: 21:04
  To change this template use File 
  | Settings 
  | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" 
language="java" %>
<html>
<head>
    <link href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-eOJMYsd53ii+scO/bJGFsiCZc+5NDVN2yr8+0RDqr0Ql0h+rP48ckxlpbzKgwra6" crossorigin="anonymous">
    <title>ODD-EVEN Game (Vowel Strings) | JSP-5</title>
</head>
<body>
<div style="text-align: center;">
    <div class="container-fluid">
        <h1> ODD-EVEN Game (Vowel Strings) | JSP-5</h1>
    </div>
</div>
<form method="post" action="/Q22/game/result">
    <div class="form-group">
        <div class="container-fluid mt-5 ">
            <div class="container mt-2">
                <label for="str">Enter the String
                </label>
            </div>
            <div class="container mt-2">
                <input type="textbox" 
                class="form-control w-75 p-3" 
                id="str" name="str" 
                placeholder="Type anything..." 
                required>
            </div>
            <div c
            lass="form-check container mt-2" 
            required>
                <div class="container mt-4">
                    <input class="form-check-input" 
                    type="radio" 
                    name="choice" 
                    id="odd" 
                    value="0">
                    <label class="form-check-label" 
                           for="odd">Odd</label><br>
                    <input class="form-check-input" 
                    type="radio" 
                    name="choice" 
                    id="even" 
                    value="1">
                    <label class="form-check-label" 
                    for="even">Even
                    </label>
                </div>
            </div>
            <div class="container mt-3 mx-10">
                <input type="submit" 
                formmethod="post" 
                class="btn btn-primary" 
                value="Check">
            </div>
        </div>
    </div>
</form>
<div class="card-footer mt-5 text-center">
    <div class="row align-items-start">
        <div class="col-6">
            <div class="container col-lg-6">
                <a class="btn btn-primary" 
                onclick="history.back(-1)" 
                role="button">Back</a>
            </div>
        </div>
        <div class="col-6">
            <div class="container col-lg-6">
                <a class="btn btn-primary" 
                href="http://localhost:8080/" 
                role="button">Home Page</a>
            </div>
        </div>
    </div>
</div>
</body>
</html><%--
  Created by IntelliJ IDEA.
  User: HP
  Date: 24-04-2021
  Time: 22:59
  To change this template use File 
  | Settings 
  | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" 
language="java" %>
<html>
<head>
    <link href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-eOJMYsd53ii+scO/bJGFsiCZc+5NDVN2yr8+0RDqr0Ql0h+rP48ckxlpbzKgwra6" crossorigin="anonymous">
    <title>ODD-EVEN Game Results (Vowel Strings) | JSP-5
    </title>
</head>
<body>
<div style="text-align: center;">
    <div class="container-fluid">
        <h1> ODD-EVEN Game Results (Vowel Strings) | JSP-5
        </h1><br><br>
    </div>
</div>
<div class="container-fluid mt-5 ">
    <div class="container mt-2">
    <div style="text-align: center;">
        <p class="font-weight-normal">
        <h1>${displayMessage}</h1></p><br><br>
        You entered: <samp><b>${str}</b></samp> <br><br>
        After checking <b>${choice}</b> 
        indexes of the entered string, the validated 
        string was: <samp><b>${demo}</b></samp>
    </div>
    </div>
</div>
<div class="card-footer mt-5 text-center">
    <div class="row align-items-start">
        <div class="col-6">
            <div class="container col-lg-6">
                <a class="btn btn-primary"  
                onclick="history.back(-1)" 
                role="button">Back</a>
            </div>
        </div>
        <div class="col-6">
            <div class="container col-lg-6">
                <a class="btn btn-primary"
                 href="http://localhost:8080/" 
                 role="button">Home Page</a>
            </div>
        </div>
    </div>
</div>
</body>
</html>package com.example.ITJSP;
import java.lang.*;
import javax.servlet.*;
import javax.servlet.http.*;
import java.io.IOException;
import javax.servlet.annotation.*;
@WebServlet(name = "OddEvenGameServlet",
 value = "/OddEvenGameServlet",
  urlPatterns = {"/Q22","/Q22/game/result","/game"})
public class OddEvenGameServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request,
     HttpServletResponse response) throws 
     ServletException, IOException {
        getServletContext().getRequestDispatcher(
        "/oddeven.jsp").forward(request,response);
    }
    @Override
    protected void doPost(HttpServletRequest request,
     HttpServletResponse response) throws 
     ServletException,
      IOException {
        String str = request.getParameter("str");
        String choice = request.getParameter("choice");
        response.setContentType("text/html");
        String demo = "";
        String displayMessage = "";
        for (int i=Integer.parseInt(choice);
         i<str.length(); i+=2) {
            demo += str.charAt(i);
        }
        String onlyConsonants = demo.replaceAll(
        "[aeiouAEIOU]", "");
        if (onlyConsonants.length() > 0) {
            displayMessage = "You Loose";
        } else {
            displayMessage = "You Win";
        }
        if (Integer.parseInt(choice) == 0){
            choice = "Odd";
        } else {
            choice = "Even";
        }
        request.setAttribute("displayMessage",
         displayMessage);
        request.setAttribute("str", str);
        request.setAttribute("demo", demo);
        request.setAttribute("choice", choice);
        getServletContext().getRequestDispatcher(
        "/oddevenresult.jsp").forward(request,response);
    }
}Output

















Try or Test The Corresponding Code Here
Last updated