diff --git a/javax-servlets/src/main/java/com/baeldung/servlets/FormServlet.java b/javax-servlets/src/main/java/com/baeldung/servlets/FormServlet.java index c78129a9cf..72a2b39a67 100644 --- a/javax-servlets/src/main/java/com/baeldung/servlets/FormServlet.java +++ b/javax-servlets/src/main/java/com/baeldung/servlets/FormServlet.java @@ -1,6 +1,7 @@ package com.baeldung.servlets; import javax.servlet.RequestDispatcher; +import javax.servlet.ServletException; import javax.servlet.annotation.WebServlet; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; @@ -11,30 +12,25 @@ import java.io.IOException; public class FormServlet extends HttpServlet { @Override - protected void doPost(HttpServletRequest request, HttpServletResponse response) - throws IOException { + protected void doPost(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException { String height = request.getParameter("height"); String weight = request.getParameter("weight"); - try { double bmi = calculateBMI(Double.parseDouble(weight), Double.parseDouble(height)); - request.setAttribute("bmi", bmi); response.setHeader("Test", "Success"); response.setHeader("BMI", String.valueOf(bmi)); - - RequestDispatcher dispatcher = request.getRequestDispatcher("/WEB-INF/jsp/index.jsp"); - dispatcher.forward(request, response); + request.getRequestDispatcher("/WEB-INF/jsp/index.jsp").forward(request, response); } catch (Exception e) { - response.sendRedirect("index.jsp"); + request.getRequestDispatcher("/WEB-INF/jsp/index.jsp").forward(request, response); } } @Override - protected void doGet(HttpServletRequest request, HttpServletResponse response) { - - // do something else here + protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { + RequestDispatcher dispatcher = request.getRequestDispatcher("/WEB-INF/jsp/index.jsp"); + dispatcher.forward(request, response); } private Double calculateBMI(Double weight, Double height) { diff --git a/javax-servlets/src/main/java/com/baeldung/servlets/MainServlet.java b/javax-servlets/src/main/java/com/baeldung/servlets/MainServlet.java index d4417b0b4e..12c75444bb 100644 --- a/javax-servlets/src/main/java/com/baeldung/servlets/MainServlet.java +++ b/javax-servlets/src/main/java/com/baeldung/servlets/MainServlet.java @@ -12,8 +12,7 @@ public class MainServlet extends HttpServlet { protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { - - response.sendRedirect("main.jsp"); + request.getRequestDispatcher("/WEB-INF/jsp/main.jsp").forward(request, response); } diff --git a/javax-servlets/src/main/java/com/baeldung/servlets/MyHttpServlet.java b/javax-servlets/src/main/java/com/baeldung/servlets/MyHttpServlet.java index b4d80db0ab..92530c2887 100644 --- a/javax-servlets/src/main/java/com/baeldung/servlets/MyHttpServlet.java +++ b/javax-servlets/src/main/java/com/baeldung/servlets/MyHttpServlet.java @@ -2,6 +2,8 @@ package com.baeldung.servlets; import java.io.IOException; import java.io.PrintWriter; +import java.util.Arrays; +import java.util.Collections; import javax.servlet.annotation.WebServlet; import javax.servlet.http.HttpServlet; @@ -20,7 +22,7 @@ public class MyHttpServlet extends HttpServlet { writer.println(request.getLocalAddr()); } else if ("getLocalName".equals(request.getParameter("function"))) { writer.println(request.getLocalName()); - } else if ("getLocalAPort".equals(request.getParameter("function"))) { + } else if ("getLocalPort".equals(request.getParameter("function"))) { writer.println(request.getLocalPort()); } else if ("getMethod".equals(request.getParameter("function"))) { writer.println(request.getMethod()); diff --git a/javax-servlets/src/main/java/com/baeldung/servlets/UpdateServlet.java b/javax-servlets/src/main/java/com/baeldung/servlets/UpdateServlet.java index 35c9819a6f..721e3afe6b 100644 --- a/javax-servlets/src/main/java/com/baeldung/servlets/UpdateServlet.java +++ b/javax-servlets/src/main/java/com/baeldung/servlets/UpdateServlet.java @@ -25,9 +25,7 @@ public class UpdateServlet extends HttpServlet { request.setAttribute("sessionData", session); } - RequestDispatcher requestDispather = request.getRequestDispatcher("update.jsp"); - - requestDispather.forward(request, response); + request.getRequestDispatcher("/WEB-INF/jsp/update.jsp").forward(request, response); } } diff --git a/javax-servlets/src/main/java/com/baeldung/servlets/UserLoginServlet.java b/javax-servlets/src/main/java/com/baeldung/servlets/UserLoginServlet.java index 6becf04a0c..fd732ef89d 100644 --- a/javax-servlets/src/main/java/com/baeldung/servlets/UserLoginServlet.java +++ b/javax-servlets/src/main/java/com/baeldung/servlets/UserLoginServlet.java @@ -21,9 +21,7 @@ public class UserLoginServlet extends HttpServlet { request.setAttribute("id", session.getAttribute("userId")); - RequestDispatcher requestDispather = request.getRequestDispatcher("userlogin.jsp"); - - requestDispather.forward(request, response); + request.getRequestDispatcher("/WEB-INF/jsp/userlogin.jsp").forward(request, response); } diff --git a/javax-servlets/src/main/java/com/baeldung/servlets/UserServlet.java b/javax-servlets/src/main/java/com/baeldung/servlets/UserServlet.java index 269cee3922..24f2500aa4 100644 --- a/javax-servlets/src/main/java/com/baeldung/servlets/UserServlet.java +++ b/javax-servlets/src/main/java/com/baeldung/servlets/UserServlet.java @@ -8,7 +8,7 @@ import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; -@WebServlet(name = "UserServlet", urlPatterns = {"/userServlet"}, initParams={ +@WebServlet(name = "UserServlet", urlPatterns = "/userServlet", initParams={ @WebInitParam(name="name", value="Not provided"), @WebInitParam(name="email", value="Not provided")}) public class UserServlet extends HttpServlet { @@ -16,13 +16,13 @@ public class UserServlet extends HttpServlet { @Override protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { + processRequest(request, response); + forwardRequest(request, response, "/WEB-INF/jsp/result.jsp"); } @Override protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { - processRequest(request, response); - forwardRequest(request, response, "/WEB-INF/jsp/result.jsp"); } protected void processRequest(HttpServletRequest request, HttpServletResponse response) diff --git a/javax-servlets/src/main/webapp/WEB-INF/jsp/result.jsp b/javax-servlets/src/main/webapp/WEB-INF/jsp/result.jsp index 01cc54858e..7259b96ed0 100644 --- a/javax-servlets/src/main/webapp/WEB-INF/jsp/result.jsp +++ b/javax-servlets/src/main/webapp/WEB-INF/jsp/result.jsp @@ -1,4 +1,3 @@ -<%@ taglib uri="http://java.sun.com/jstl/core" prefix="c" %> <%@ page contentType="text/html" pageEncoding="UTF-8"%> diff --git a/javax-servlets/src/main/webapp/user.jsp b/javax-servlets/src/main/webapp/user.jsp index c6e1bb69f9..2139052a3a 100644 --- a/javax-servlets/src/main/webapp/user.jsp +++ b/javax-servlets/src/main/webapp/user.jsp @@ -1,4 +1,3 @@ -<%@ taglib uri="http://java.sun.com/jstl/core" prefix="c" %> <%@ page contentType="text/html" pageEncoding="UTF-8"%>