JAVA-10004 --fixing path issues

This commit is contained in:
keerthigadde 2022-05-29 15:11:25 +10:00
parent f8edd495ba
commit 660945ed25
8 changed files with 16 additions and 25 deletions

View File

@ -1,6 +1,7 @@
package com.baeldung.servlets; package com.baeldung.servlets;
import javax.servlet.RequestDispatcher; import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet; import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletRequest;
@ -11,30 +12,25 @@ import java.io.IOException;
public class FormServlet extends HttpServlet { public class FormServlet extends HttpServlet {
@Override @Override
protected void doPost(HttpServletRequest request, HttpServletResponse response) protected void doPost(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
throws IOException {
String height = request.getParameter("height"); String height = request.getParameter("height");
String weight = request.getParameter("weight"); String weight = request.getParameter("weight");
try { try {
double bmi = calculateBMI(Double.parseDouble(weight), Double.parseDouble(height)); double bmi = calculateBMI(Double.parseDouble(weight), Double.parseDouble(height));
request.setAttribute("bmi", bmi); request.setAttribute("bmi", bmi);
response.setHeader("Test", "Success"); response.setHeader("Test", "Success");
response.setHeader("BMI", String.valueOf(bmi)); response.setHeader("BMI", String.valueOf(bmi));
request.getRequestDispatcher("/WEB-INF/jsp/index.jsp").forward(request, response);
RequestDispatcher dispatcher = request.getRequestDispatcher("/WEB-INF/jsp/index.jsp");
dispatcher.forward(request, response);
} catch (Exception e) { } catch (Exception e) {
response.sendRedirect("index.jsp"); request.getRequestDispatcher("/WEB-INF/jsp/index.jsp").forward(request, response);
} }
} }
@Override @Override
protected void doGet(HttpServletRequest request, HttpServletResponse response) { protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
RequestDispatcher dispatcher = request.getRequestDispatcher("/WEB-INF/jsp/index.jsp");
// do something else here dispatcher.forward(request, response);
} }
private Double calculateBMI(Double weight, Double height) { private Double calculateBMI(Double weight, Double height) {

View File

@ -12,8 +12,7 @@ public class MainServlet extends HttpServlet {
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
request.getRequestDispatcher("/WEB-INF/jsp/main.jsp").forward(request, response);
response.sendRedirect("main.jsp");
} }

View File

@ -2,6 +2,8 @@ package com.baeldung.servlets;
import java.io.IOException; import java.io.IOException;
import java.io.PrintWriter; import java.io.PrintWriter;
import java.util.Arrays;
import java.util.Collections;
import javax.servlet.annotation.WebServlet; import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServlet;
@ -20,7 +22,7 @@ public class MyHttpServlet extends HttpServlet {
writer.println(request.getLocalAddr()); writer.println(request.getLocalAddr());
} else if ("getLocalName".equals(request.getParameter("function"))) { } else if ("getLocalName".equals(request.getParameter("function"))) {
writer.println(request.getLocalName()); writer.println(request.getLocalName());
} else if ("getLocalAPort".equals(request.getParameter("function"))) { } else if ("getLocalPort".equals(request.getParameter("function"))) {
writer.println(request.getLocalPort()); writer.println(request.getLocalPort());
} else if ("getMethod".equals(request.getParameter("function"))) { } else if ("getMethod".equals(request.getParameter("function"))) {
writer.println(request.getMethod()); writer.println(request.getMethod());

View File

@ -25,9 +25,7 @@ public class UpdateServlet extends HttpServlet {
request.setAttribute("sessionData", session); request.setAttribute("sessionData", session);
} }
RequestDispatcher requestDispather = request.getRequestDispatcher("update.jsp"); request.getRequestDispatcher("/WEB-INF/jsp/update.jsp").forward(request, response);
requestDispather.forward(request, response);
} }
} }

View File

@ -21,9 +21,7 @@ public class UserLoginServlet extends HttpServlet {
request.setAttribute("id", session.getAttribute("userId")); request.setAttribute("id", session.getAttribute("userId"));
RequestDispatcher requestDispather = request.getRequestDispatcher("userlogin.jsp"); request.getRequestDispatcher("/WEB-INF/jsp/userlogin.jsp").forward(request, response);
requestDispather.forward(request, response);
} }

View File

@ -8,7 +8,7 @@ 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;
@WebServlet(name = "UserServlet", urlPatterns = {"/userServlet"}, initParams={ @WebServlet(name = "UserServlet", urlPatterns = "/userServlet", initParams={
@WebInitParam(name="name", value="Not provided"), @WebInitParam(name="name", value="Not provided"),
@WebInitParam(name="email", value="Not provided")}) @WebInitParam(name="email", value="Not provided")})
public class UserServlet extends HttpServlet { public class UserServlet extends HttpServlet {
@ -16,13 +16,13 @@ public class UserServlet extends HttpServlet {
@Override @Override
protected void doGet(HttpServletRequest request, HttpServletResponse response) protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException { throws ServletException, IOException {
processRequest(request, response);
forwardRequest(request, response, "/WEB-INF/jsp/result.jsp");
} }
@Override @Override
protected void doPost(HttpServletRequest request, HttpServletResponse response) protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException { throws ServletException, IOException {
processRequest(request, response);
forwardRequest(request, response, "/WEB-INF/jsp/result.jsp");
} }
protected void processRequest(HttpServletRequest request, HttpServletResponse response) protected void processRequest(HttpServletRequest request, HttpServletResponse response)

View File

@ -1,4 +1,3 @@
<%@ taglib uri="http://java.sun.com/jstl/core" prefix="c" %>
<%@ page contentType="text/html" pageEncoding="UTF-8"%> <%@ page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html> <!DOCTYPE html>
<html> <html>

View File

@ -1,4 +1,3 @@
<%@ taglib uri="http://java.sun.com/jstl/core" prefix="c" %>
<%@ page contentType="text/html" pageEncoding="UTF-8"%> <%@ page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html> <!DOCTYPE html>
<html> <html>