# Q23

### Ask a user's name Java Server Pages by Hans Bergsten age on a HTML form. Then display Hello on a JSP. On the same page ask the product the user would like to buy. Then redirect to another JSP which would display: Hello , You have ordered . (Use Session Scope Variable using setTag)

{% hint style="info" %}
Created **OrderServlet.java** which handles **GET** requests of url pattern **"/Q23"** and **"/order"** and renders the page **GetUserInfo.jsp. GetUserInfo.jsp** takes user input and using session scopes it parses input values to **WelcomeUser.jsp** which then takes input for product order and subsequently parses values to **OrderConfirmed.jsp** which displays desired output.

[Browse Source Code](https://github.com/MJK618/InternetTechnologies/tree/main/JSP/ITJSP)
{% endhint %}

## Code

{% tabs %}
{% tab title="GetUserInfo.jsp" %}

```markup
<%--
  Created by IntelliJ IDEA.
  User: HP
  Date: 25-04-2021
  Time: 11:14
  To change this template use File 
  | Settings 
  | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" 
language="java" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" 
prefix="c" %>
<html>
<head>

    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.0.0-beta3/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-eOJMYsd53ii+scO/bJGFsiCZc+5NDVN2yr8+0RDqr0Ql0h+rP48ckxlpbzKgwra6" crossorigin="anonymous">

    <title> Take User Credentials | JSP-6 </title>
</head>
<body>

<div style="text-align: center;">
    <div class="container-fluid">

<h1>Take User Credentials | JSP-6 </h1>
    </div>
</div>
<div class="container-fluid mt-5 ">
    <form action="WelcomeUser.jsp">
        <div class="container mt-2">
            <label for="name">Enter Name</label>
        </div>
        <div class="container mt-2">
            <input type="text" 
            class="form-control w-75 p-3" 
            id="name" name="name" 
            placeholder="Enter Your Name" 
            required>
        </div>
        <div class="container mt-2">
            <label for="age">Age</label>
        </div>
        <div class="container mt-2">
            <input type="number" 
            class="form-control w-75 p-3" 
            id="age" 
            name="age" 
            placeholder="Enter Your Age" 
            required>
        </div>
        <div class="container mt-3 mx-10">
            <input type="submit" 
            formmethod="post" 
            class="btn btn-primary" 
            value="Submit">
        </div>

    </form>
</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>
```

[Download GetUserInfo.jsp](https://github.com/MJK618/InternetTechnologies/raw/main/JSP/ITJSP/src/main/webapp/GetUserInfo.jsp)
{% endtab %}

{% tab title="WelcomeUser.jsp" %}

```markup
<%--
  Created by IntelliJ IDEA.
  User: HP
  Date: 25-04-2021
  Time: 11:23
  To change this template use File 
  | Settings 
  | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" 
language="java" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" 
prefix="c" %>
<html>
<head>
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.0.0-beta3/dist/css/bootstrap.min.css" rel="stylesheet"
          integrity="sha384-eOJMYsd53ii+scO/bJGFsiCZc+5NDVN2yr8+0RDqr0Ql0h+rP48ckxlpbzKgwra6" crossorigin="anonymous">

    <title> Welcome User | JSP-6 </title>
</head>
<body>

<div style="text-align: center;">
    <div class="container-fluid">
        <h3>Welcome User | JSP-6 </h3>
    </div>
</div>
<div class="container-fluid mt-5 ">
    <div class="container mt-2">
        <div style="text-align: center;">
            <p class="font-weight-normal">
            <h1>Hello ${param.name}</h1></p><br><br>
        </div>
        <form action="OrderConfirmed.jsp">
            <div class="container mt-2">
                <label for="prod">
                Which Product do you wish to buy?
                </label>
                <c:set 
                var="name" 
                value="${param.name}" 
                scope="session"/>
                <c:set var="age" 
                value="${param.age}" 
                scope="session"/>
            </div>
            <div class="container mt-2">
                <input 
                type="textbox" 
                class="form-control w-75 p-3" 
                id="prod" 
                name="product" 
                placeholder="Enter Product Name" 
                required>
            </div>

            <div class="container mt-3 mx-10">
                <input type="submit" 
                formmethod="post" 
                class="btn btn-primary" 
                value="Order Now">
            </div>
        </form>

    </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>
```

[Download WelcomeUser.info](https://github.com/MJK618/InternetTechnologies/raw/main/JSP/ITJSP/src/main/webapp/WelcomeUser.jsp)
{% endtab %}

{% tab title="OrderConfirmed.jsp" %}

```markup
<%--
  Created by IntelliJ IDEA.
  User: HP
  Date: 25-04-2021
  Time: 11:26
  To change this template use File 
  | Settings 
  | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" 
language="java" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" 
prefix="c" %>
<html>
<head>
  <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.0.0-beta3/dist/css/bootstrap.min.css" rel="stylesheet"
        integrity="sha384-eOJMYsd53ii+scO/bJGFsiCZc+5NDVN2yr8+0RDqr0Ql0h+rP48ckxlpbzKgwra6" crossorigin="anonymous">

  <title>Order Confirmation | JSP-6</title>
</head>
<body>
<div style="text-align: center;">
  <div class="container-fluid">
    <h1>Order Confirmation | JSP-6 </h1>
  </div>
</div>
<div class="container-fluid mt-3 ">
  <div class="container mt-2">
    <div style="text-align: center;">
      <p class="font-weight-normal">
      <h3>Hello <c:out value="${name}"/> <br/></h3><br>
      Aged: <c:out value="${age}"/></p>
      <div class="container mt-2">
      Your order for <c:out value="${param.product}" /> 
      is now confirmed.<br>
      </div><br><br>
    </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>
```

[Download OrderConfirmed.jsp](https://github.com/MJK618/InternetTechnologies/raw/main/JSP/ITJSP/src/main/webapp/OrderConfirmed.jsp)
{% endtab %}

{% tab title="OrderServlet.java" %}

```java
import javax.servlet.*;
import javax.servlet.http.*;
import java.io.IOException;

import javax.servlet.annotation.*;

@WebServlet(name = "OrderServlet", 
value = "/OrderServlet", 
urlPatterns = {"/Q23","/order"})

public class OrderServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request,
     HttpServletResponse response) 
     throws ServletException,
      IOException { 
      getServletContext().getRequestDispatcher(
        "/GetUserInfo.jsp").forward(request,response);
    }

    @Override
    protected void doPost(HttpServletRequest request,
     HttpServletResponse response) 
     throws ServletException, IOException {

    }
}
```

{% file src="<https://2549503256-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-MYBDzNEDANjFDzIvzi2%2F-MZSuMhpYK4t59Djerzh%2F-MZTb_J7pAAG5_a6HjIj%2FOrderServlet.java?alt=media&token=63c2f7cf-a373-4ed7-90f1-4e1aab2e2c5f>" %}
Download OrderServlet.java
{% endfile %}
{% endtab %}
{% endtabs %}

## Output

![Main Page (1/5)](https://2549503256-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-MYBDzNEDANjFDzIvzi2%2F-MZW2sLupya9QvNpoGpC%2F-MZWOzjBGjugtbHNAHN6%2Fimage.png?alt=media\&token=e5de9cbe-7de8-4f2c-8636-d8bc0695fa8d)

![Giving User Inputs (2/5)](https://2549503256-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-MYBDzNEDANjFDzIvzi2%2F-MZW2sLupya9QvNpoGpC%2F-MZWP4zKcbipIxXPqJNp%2Fimage.png?alt=media\&token=774aaea0-4d74-436b-a931-11da154d7ed1)

![Welcome Page with Passed Values (3/5)](https://2549503256-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-MYBDzNEDANjFDzIvzi2%2F-MZW2sLupya9QvNpoGpC%2F-MZWPC_IYByfil8hnGFc%2Fimage.png?alt=media\&token=83c2ca3d-d19b-40e5-807d-88f6c7e793f1)

![Adding Product (4/5)](https://2549503256-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-MYBDzNEDANjFDzIvzi2%2F-MZW2sLupya9QvNpoGpC%2F-MZWPJSNhXh8LdZKV7T4%2Fimage.png?alt=media\&token=36813bb8-4b9f-44cd-97fa-3099068508c6)

![Final Output with values from both the previous pages (5/5)](https://2549503256-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-MYBDzNEDANjFDzIvzi2%2F-MZW2sLupya9QvNpoGpC%2F-MZWPQdTyQrVolzC7tub%2Fimage.png?alt=media\&token=8919ef17-2a04-41b6-9721-876d839e9992)

{% hint style="success" %}
Try or Test The Corresponding Code Here
{% endhint %}

{% embed url="<https://jatin.in1.cloudjiffy.net/Q23>" %}
Run Code Here
{% endembed %}
