RequestDispatcher added to servlet
This commit is contained in:
parent
0a9a0e819a
commit
aa2285f3b4
|
@ -0,0 +1,5 @@
|
|||
Manifest-Version: 1.0
|
||||
Built-By: shubham
|
||||
Created-By: IntelliJ IDEA
|
||||
Build-Jdk: 1.8.0_91
|
||||
|
|
@ -0,0 +1,7 @@
|
|||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
|
||||
version="3.1">
|
||||
|
||||
</web-app>
|
|
@ -0,0 +1,26 @@
|
|||
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
|
||||
<html>
|
||||
<head>
|
||||
<title>Calculate BMI</title>
|
||||
</head>
|
||||
<body>
|
||||
|
||||
<form name="bmiForm" action="calculateServlet" method="POST">
|
||||
|
||||
<table>
|
||||
<tr>
|
||||
<td>Your Weight (kg) :</td>
|
||||
<td><input type="text" name="weight"/></td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td>Your Height (m) :</td>
|
||||
<td><input type="text" name="height"/></td>
|
||||
</tr>
|
||||
<th><input type="submit" value="Submit" name="find"/></th>
|
||||
<th><input type="reset" value="Reset" name="reset" /></th>
|
||||
</table>
|
||||
<h2>${bmi}</h2>
|
||||
</form>
|
||||
|
||||
</body>
|
||||
</html>
|
|
@ -1,5 +1,6 @@
|
|||
package com.root;
|
||||
|
||||
import javax.servlet.RequestDispatcher;
|
||||
import javax.servlet.ServletException;
|
||||
import javax.servlet.annotation.WebServlet;
|
||||
import javax.servlet.http.HttpServlet;
|
||||
|
@ -7,13 +8,37 @@ import javax.servlet.http.HttpServletRequest;
|
|||
import javax.servlet.http.HttpServletResponse;
|
||||
import java.io.IOException;
|
||||
|
||||
@WebServlet(name = "FormServlet", urlPatterns = "/informationServlet")
|
||||
@WebServlet(name = "FormServlet", urlPatterns = "/calculateServlet")
|
||||
public class FormServlet extends HttpServlet {
|
||||
|
||||
@Override
|
||||
protected void doPost(HttpServletRequest request, HttpServletResponse response)
|
||||
throws ServletException, IOException {
|
||||
|
||||
String userName = request.getParameter("userName");
|
||||
String userPlanet = request.getParameter("userPlanet");
|
||||
String height = request.getParameter("height");
|
||||
String weight = request.getParameter("weight");
|
||||
|
||||
try {
|
||||
Double bmi = calculateBMI(Double.parseDouble(weight), Double.parseDouble(height));
|
||||
|
||||
request.setAttribute("bmi", bmi);
|
||||
RequestDispatcher dispatcher = request.getRequestDispatcher("index.jsp");
|
||||
dispatcher.forward(request, response);
|
||||
} catch (Exception e) {
|
||||
|
||||
response.sendRedirect("index.jsp");
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void doGet(HttpServletRequest request, HttpServletResponse response)
|
||||
throws ServletException, IOException {
|
||||
|
||||
// do something else here
|
||||
}
|
||||
|
||||
private Double calculateBMI(Double weight, Double height) {
|
||||
|
||||
return weight / (height * height);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,29 +1,25 @@
|
|||
<%--
|
||||
Created by IntelliJ IDEA.
|
||||
User: shubham
|
||||
Date: 26/11/16
|
||||
Time: 8:52 PM
|
||||
To change this template use File | Settings | File Templates.
|
||||
--%>
|
||||
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
|
||||
<html>
|
||||
<head>
|
||||
<title>Form</title>
|
||||
<title>Calculate BMI</title>
|
||||
</head>
|
||||
<body>
|
||||
|
||||
<form action="informationServlet" method="POST">
|
||||
<form name="bmiForm" action="calculateServlet" method="POST">
|
||||
|
||||
Name:<input type="text" name="userName"/><br/><br/>
|
||||
Planet:
|
||||
<select name="userPlanet">
|
||||
<option>Mercury</option>
|
||||
<option>Venus</option>
|
||||
<option>Mars</option>
|
||||
</select>
|
||||
<br/><br/>
|
||||
|
||||
<input type="submit" value="Submit"/>
|
||||
<table>
|
||||
<tr>
|
||||
<td>Your Weight (kg) :</td>
|
||||
<td><input type="text" name="weight"/></td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td>Your Height (m) :</td>
|
||||
<td><input type="text" name="height"/></td>
|
||||
</tr>
|
||||
<th><input type="submit" value="Submit" name="find"/></th>
|
||||
<th><input type="reset" value="Reset" name="reset" /></th>
|
||||
</table>
|
||||
<h2>${bmi}</h2>
|
||||
</form>
|
||||
|
||||
</body>
|
||||
|
|
Loading…
Reference in New Issue