Q23
Question 23
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)
Code
<%--
  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/[email protected]/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><%--
  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/[email protected]/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><%--
  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/[email protected]/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>
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 {
    }
}
Output





Try or Test The Corresponding Code Here
Last updated