Skip to content

Commit

Permalink
RequestDispatcher added to servlet
Browse files Browse the repository at this point in the history
  • Loading branch information
sbmaggarwal committed Dec 1, 2016
1 parent 0be0117 commit 782e169
Showing 1 changed file with 10 additions and 4 deletions.
14 changes: 10 additions & 4 deletions javax-servlets/src/main/java/com/root/FormServlet.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

@WebServlet(name = "FormServlet", urlPatterns = "/calculateServlet")
public class FormServlet extends HttpServlet {
Expand All @@ -21,10 +20,10 @@ protected void doPost(HttpServletRequest request, HttpServletResponse response)

try {
double bmi = calculateBMI(Double.parseDouble(weight), Double.parseDouble(height));
request.setAttribute("bmi", bmi);

PrintWriter out= response.getWriter();
out.write(String.valueOf(bmi));
request.setAttribute("bmi", bmi);
response.setHeader("Test", "Success");
response.setHeader("BMI", String.valueOf(bmi));

RequestDispatcher dispatcher = request.getRequestDispatcher("index.jsp");
dispatcher.forward(request, response);
Expand All @@ -34,6 +33,13 @@ protected void doPost(HttpServletRequest request, HttpServletResponse response)
}
}

@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);
Expand Down

0 comments on commit 782e169

Please sign in to comment.