RequestDispatcher added to servlet

This commit is contained in:
Shubham Aggarwal 2016-11-28 09:36:42 +05:30
parent 0be0117470
commit 782e169f46
1 changed files with 10 additions and 4 deletions

View File

@ -7,7 +7,6 @@ import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpServletResponse;
import java.io.IOException; import java.io.IOException;
import java.io.PrintWriter;
@WebServlet(name = "FormServlet", urlPatterns = "/calculateServlet") @WebServlet(name = "FormServlet", urlPatterns = "/calculateServlet")
public class FormServlet extends HttpServlet { public class FormServlet extends HttpServlet {
@ -21,10 +20,10 @@ public class FormServlet extends HttpServlet {
try { try {
double bmi = calculateBMI(Double.parseDouble(weight), Double.parseDouble(height)); double bmi = calculateBMI(Double.parseDouble(weight), Double.parseDouble(height));
request.setAttribute("bmi", bmi);
PrintWriter out= response.getWriter(); request.setAttribute("bmi", bmi);
out.write(String.valueOf(bmi)); response.setHeader("Test", "Success");
response.setHeader("BMI", String.valueOf(bmi));
RequestDispatcher dispatcher = request.getRequestDispatcher("index.jsp"); RequestDispatcher dispatcher = request.getRequestDispatcher("index.jsp");
dispatcher.forward(request, response); dispatcher.forward(request, response);
@ -34,6 +33,13 @@ public class FormServlet extends HttpServlet {
} }
} }
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
// do something else here
}
private Double calculateBMI(Double weight, Double height) { private Double calculateBMI(Double weight, Double height) {
return weight / (height * height); return weight / (height * height);