Internet Technologies
1.0.0
1.0.0
  • Introduction
  • Contents
  • Practical List
  • HTML & CSS
    • Q1
    • Q2
    • Q3
    • Q4
    • Q5
  • JAVA Programs
    • Q6
    • Q7
    • Q8
    • Q9
    • Q10
    • Q11
    • Q12
  • JAVASCRIPT
    • Q13
    • Q14
    • Q15
  • JDBC
    • Q16
    • Q17
  • JSP
    • Side Note
    • Q18
    • Q19
    • Q20
    • Q21
    • Q22
    • Q23
  • End
Powered by GitBook
On this page
  • Code
  • Output
  1. JSP

Q22

Question 22

PreviousQ21NextQ23

Last updated 4 years ago

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.

Created OddEvenGameServlet.java which handles GET requests of url pattern "/Q22" and "/game" also POST requests of url pattern "/q22/game/result" . For GET requests it renders oddeven.jsp page and asks user to input the string to play with and for POST requests it analyses the game logic with the servlet and then renders oddevenresult.jsp page with desired output.

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

Browse Source Code
Download oddeven.jsp
Download oddevenresult.jsp
https://jatin.in1.cloudjiffy.net/Q22jatin.in1.cloudjiffy.net
Run Code Here
2KB
OddEvenGameServlet.java
Download OddEvenGameServlet.java
Main Game Page (1/17)
A Loosing String(for both) Input for Odd Indexes (2/17)
Loosing String(for both) Output for Odd Indexes (3/17)
A Loosing String(for both) Input for Even Indexes (4/17)
Loosing String(for both) Output for Even Indexes (5/17)
Winning Input(for both) For Odd Indexes (6/17)
Winning String(for both) Output for Odd Indexes (7/17)
Winning Input(for both) for Even Indexes (8/17)
Winning String(for both) Output for Even Indexes (9/17)
Input of Odd Losing and Even Winning String (10/17)
Output for Only Odd Losing String (11/17)
Input of Only Odd Losing and Even Winning String (12/17)
Output for Only Even Winning String (13/17)
Input of Only Even Losing and Odd Winning String (14/17)
Output for Only Odd Winning (15/17)
Input of Only Even Losing and Odd Winning String (16/17)
Output for Only Even Loosing String (17/17)