[Web] 서블릿 URL 패턴

2024. 8. 21. 14:08자바 웹 개발/세션 쿠키 필터 리스너

URL 정확히 일치)

@WebServlet("/first/test")
public class TestServlet1 extends HttpServlet{
	public void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		req.setCharacterEncoding("utf-8");
		resp.setContentType("text/html;charset=utf-8");
		PrintWriter out = resp.getWriter();
		
		String context = req.getContextPath();
		String url = req.getRequestURL().toString();
		String mapping = req.getServletPath();
		String uri = req.getRequestURI();
		
		out.println("<html>");
		out.println("<head>");
		out.println("<title>Test Servlet1</title>");
		out.println("</head>");
		out.println("<body bgcolor='green'>");
		out.println("<b>TestServlet1입니다.</b><br>");
		out.println("<b>컨텍스트 이름 :" + context + "</b><br>");
		out.println("<b>전체 이름 :" + url + "</b><br>");
		out.println("<b>매핑 이름 :" + mapping + "</b><br>");
		out.println("<b>URI :" + uri + "</b>");
		out.println("</body>");
		out.println("</html>");
		out.close();
	}
	
}

 

디렉터리 이름 일치)

@WebServlet("/first/*")
public class TestServlet2 extends HttpServlet{
	public void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		req.setCharacterEncoding("utf-8");
		resp.setContentType("text/html;charset=utf-8");
		PrintWriter out = resp.getWriter();
		
		String context = req.getContextPath();
		String url = req.getRequestURL().toString();
		String mapping = req.getServletPath();
		String uri = req.getRequestURI();
		
		out.println("<html>");
		out.println("<head>");
		out.println("<title>Test Servlet2</title>");
		out.println("</head>");
		out.println("<body bgcolor='yellow'>");
		out.println("<b>TestServlet2입니다.</b><br>");
		out.println("<b>컨텍스트 이름 :" + context + "</b><br>");
		out.println("<b>전체 이름 :" + url + "</b><br>");
		out.println("<b>매핑 이름 :" + mapping + "</b><br>");
		out.println("<b>URI :" + uri + "</b>");
		out.println("</body>");
		out.println("</html>");
		out.close();
	}
}

 

확장자 일치)

@WebServlet("*.do")
public class TestServlet3 extends HttpServlet{
	public void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		req.setCharacterEncoding("utf-8");
		resp.setContentType("text/html;charset=utf-8");
		PrintWriter out = resp.getWriter();
		
		String context = req.getContextPath();
		String url = req.getRequestURL().toString();
		String mapping = req.getServletPath();
		String uri = req.getRequestURI();
		
		out.println("<html>");
		out.println("<head>");
		out.println("<title>Test Servlet3</title>");
		out.println("</head>");
		out.println("<body bgcolor='red'>");
		out.println("<b>TestServlet3입니다.</b><br>");
		out.println("<b>컨텍스트 이름 :" + context + "</b><br>");
		out.println("<b>전체 이름 :" + url + "</b><br>");
		out.println("<b>매핑 이름 :" + mapping + "</b><br>");
		out.println("<b>URI :" + uri + "</b>");
		out.println("</body>");
		out.println("</html>");
		out.close();
	}
}

 

주의할 점은 /base.do를 입력하면 앞의 디렉터리 이름이 우선하여 TestServlet2가 실행된다. 반면에, /second/base.do로 요청하면 TestSErvlet3이 실행된다.