diff --git a/javax-servlets/classes/artifacts/ServletmavenExample_war_exploded/META-INF/MANIFEST.MF b/javax-servlets/classes/artifacts/ServletmavenExample_war_exploded/META-INF/MANIFEST.MF new file mode 100644 index 0000000000..e8d1783f02 --- /dev/null +++ b/javax-servlets/classes/artifacts/ServletmavenExample_war_exploded/META-INF/MANIFEST.MF @@ -0,0 +1,5 @@ +Manifest-Version: 1.0 +Built-By: shubham +Created-By: IntelliJ IDEA +Build-Jdk: 1.8.0_91 + diff --git a/javax-servlets/classes/artifacts/ServletmavenExample_war_exploded/WEB-INF/web.xml b/javax-servlets/classes/artifacts/ServletmavenExample_war_exploded/WEB-INF/web.xml new file mode 100644 index 0000000000..66934d8fd3 --- /dev/null +++ b/javax-servlets/classes/artifacts/ServletmavenExample_war_exploded/WEB-INF/web.xml @@ -0,0 +1,7 @@ + + + + \ No newline at end of file diff --git a/javax-servlets/classes/artifacts/ServletmavenExample_war_exploded/index.jsp b/javax-servlets/classes/artifacts/ServletmavenExample_war_exploded/index.jsp new file mode 100644 index 0000000000..5a2e018a41 --- /dev/null +++ b/javax-servlets/classes/artifacts/ServletmavenExample_war_exploded/index.jsp @@ -0,0 +1,26 @@ +<%@ page contentType="text/html;charset=UTF-8" language="java" %> + + + Calculate BMI + + + +
+ + + + + + + + + + + + +
Your Weight (kg) :
Your Height (m) :
+

${bmi}

+
+ + + diff --git a/javax-servlets/src/main/java/com/root/FormServlet.java b/javax-servlets/src/main/java/com/root/FormServlet.java index c8e8212f30..5c11a586d3 100644 --- a/javax-servlets/src/main/java/com/root/FormServlet.java +++ b/javax-servlets/src/main/java/com/root/FormServlet.java @@ -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); } } diff --git a/javax-servlets/web/index.jsp b/javax-servlets/web/index.jsp index 4054b1acf0..5a2e018a41 100644 --- a/javax-servlets/web/index.jsp +++ b/javax-servlets/web/index.jsp @@ -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" %> - Form + Calculate BMI -
+ - Name:

- Planet: - -

- - + + + + + + + + + + + +
Your Weight (kg) :
Your Height (m) :
+

${bmi}