Merge pull request #12278 from keerthigadde/JAVA-10004

JAVA-10004 --fixing path issues
This commit is contained in:
kwoyke 2022-06-08 08:45:52 +02:00 committed by GitHub
commit bc7ee742d6
8 changed files with 16 additions and 25 deletions

View File

@ -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) {

View File

@ -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);
}

View File

@ -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());

View File

@ -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);
}
}

View File

@ -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);
}

View File

@ -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)

View File

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