Merge pull request #7440 from pazis/master

BAEL-2856 Difference between getRequestURI and getPathInfo in HttpServletRequest
This commit is contained in:
Eric Martin 2019-07-31 21:16:54 -05:00 committed by GitHub
commit 6dbeb46d7b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 57 additions and 0 deletions

View File

@ -0,0 +1,57 @@
package com.baeldung.servlets;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
@WebServlet(name = "MyHttpServlet", urlPatterns = "/servlet-mapping")
public class MyHttpServlet extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException {
PrintWriter writer = response.getWriter();
writer.println(request.getParameter("function"));
if ("getContextPath".equals(request.getParameter("function"))) {
writer.println(request.getContextPath());
} else if ("getLocalAddr".equals(request.getParameter("function"))) {
writer.println(request.getLocalAddr());
} else if ("getLocalName".equals(request.getParameter("function"))) {
writer.println(request.getLocalName());
} else if ("getLocalAPort".equals(request.getParameter("function"))) {
writer.println(request.getLocalPort());
} else if ("getMethod".equals(request.getParameter("function"))) {
writer.println(request.getMethod());
} else if ("getParameterNames".equals(request.getParameter("function"))) {
writer.println(request.getParameterNames());
} else if ("getPathInfo".equals(request.getParameter("function"))) {
writer.println(request.getPathInfo());
} else if ("getProtocol".equals(request.getParameter("function"))) {
writer.println(request.getProtocol());
} else if ("getQueryString".equals(request.getParameter("function"))) {
writer.println(request.getQueryString());
} else if ("getRequestedSessionId".equals(request.getParameter("function"))) {
writer.println(request.getRequestedSessionId());
} else if ("getRequestURI".equals(request.getParameter("function"))) {
writer.println(request.getRequestURI());
} else if ("getRequestURL".equals(request.getParameter("function"))) {
writer.println(request.getRequestURL());
} else if ("getScheme".equals(request.getParameter("function"))) {
writer.println(request.getScheme());
} else if ("getServerName".equals(request.getParameter("function"))) {
writer.println(request.getServerName());
} else if ("getServerPort".equals(request.getParameter("function"))) {
writer.println(request.getServerPort());
} else if ("getServletPath".equals(request.getParameter("function"))) {
writer.println(request.getServletPath());
} else {
writer.println("INVALID FUNCTION");
}
writer.flush();
}
}