[Servlet] URL 매핑
🧶 𝗪𝗲𝗯/Servlet

[Servlet] URL 매핑

 

 

 

 query 로 매핑 

JSP

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>URL 매핑하기</title>
</head>
<body>
	<form method="GET" action="/Basic/test/">
		<input type="submit" name="action" value="list"/>
		<br/>
		<input type="submit" name="action" value="detail"/>
		<br/>
		<input type="submit" name="action" value="regist"/>
	</form>
</body>
</html>

다양한 방법이 있겠지만 일단 대충 input submit 에 name, value 를 사용하여 action 쿼리와 값으로 url 을 매핑해보았습니다.

form 태그에서 action 은 submit 버튼을 누르면 이동하는 url 이 할당될 수 있습니다.

 

Servlet

package servlet;


import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;


@WebServlet("/test/")
public class ServletTest extends HttpServlet {
	private static final long serialVersionUID = 1L;


	@Override
	protected void doGet(HttpServletRequest request, HttpServletResponse response)
	    throws ServletException, IOException {
		request.setCharacterEncoding("utf-8");
		response.setContentType("text/html;charset=utf-8");

		System.out.println(request.getRequestURL());

		// query 로 매핑하기
		String action = request.getParameter("action");
		System.out.println(action);

		switch (action) {
		case "list":
			list(request, response);
			break;
		case "detail":
			detail(request, response);
			break;
		case "regist":
			regist(request, response);
			break;
		default:
			break;
		}

	}


	@Override
	protected void doPost(HttpServletRequest request, HttpServletResponse response)
	    throws ServletException, IOException {
		doGet(request, response);
	}


	protected void list(HttpServletRequest request, HttpServletResponse response)
	    throws ServletException, IOException {
		System.out.println("list!!!");
		response.getWriter().append("LIST");
	}


	protected void detail(HttpServletRequest request, HttpServletResponse response)
	    throws ServletException, IOException {
		response.getWriter().append("DETAIL");
	}


	protected void regist(HttpServletRequest request, HttpServletResponse response)
	    throws ServletException, IOException {
		response.getWriter().append("REGIST");
	}

}

request.getParameter() 를 통해서 쿼리 값을 가져올 수 있습니다.

 

 

 path 로 매핑 

JSP

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8" isELIgnored="false"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<c:set var="root" value="${ pageContext.request.contextPath }"/>

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>URL 매핑하기</title>
</head>
<body>
	<a href="${root}/ServletTest/list">list</a>
	<br>
	<a href="${root}/ServletTest/detail">detail</a>
	<br>
	<a href="${root}/ServletTest/regist">regist</a>
	<br>
</body>
</html>

el 를 이용했습니다.

 

Servlet

package servlet;


import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;


@WebServlet("/ServletTest/*")
public class ServletTest extends HttpServlet {
	private static final long serialVersionUID = 1L;


	@Override
	protected void doGet(HttpServletRequest request, HttpServletResponse response)
	    throws ServletException, IOException {
		request.setCharacterEncoding("utf-8");
		response.setContentType("text/html;charset=utf-8");

		System.out.println(request.getRequestURL());

		// path 로 매핑하기
		String contextPath = request.getContextPath();
		String path = request.getRequestURI().substring(contextPath.length());
		System.out.println(contextPath);
		System.out.println(path);

		switch (path) {
		case "/ServletTest/list":
			list(request, response);
			break;
		case "/ServletTest/detail":
			detail(request, response);
			break;
		case "/ServletTest/regist":
			regist(request, response);
			break;
		default:
			break;
		}

	}


	@Override
	protected void doPost(HttpServletRequest request, HttpServletResponse response)
	    throws ServletException, IOException {
		doGet(request, response);
	}


	protected void list(HttpServletRequest request, HttpServletResponse response)
	    throws ServletException, IOException {
		System.out.println("list!!!");
		response.getWriter().append("LIST");
	}


	protected void detail(HttpServletRequest request, HttpServletResponse response)
	    throws ServletException, IOException {
		response.getWriter().append("DETAIL");
	}


	protected void regist(HttpServletRequest request, HttpServletResponse response)
	    throws ServletException, IOException {
		response.getWriter().append("REGIST");
	}

}

총 URI 에서 contextPath 길이만큼 빼서 path 를 구할 수 있습니다.

보면 /ServletTest/list 이런식으로 들어가야 합니다. 앞에 / 를 빼면 메서드 실행 안됩니다..

항상 까먹어서 에러를 마주치곤 합니다..

 

 

 Web.xml 로 매핑 

설정한 Tomcat Server 에서 web.xml 을 선택합니다.

<servlet>
    <servlet-name>ServletTest</servlet-name>
    <servlet-class>servlet.ServletTest</servlet-class>
</servlet>

<servlet-mapping>
    <servlet-name>ServletTest</servlet-name>
    <url-pattern>/ServletTest/*</url-pattern>
</servlet-mapping>

Servlet Class 를 <servlet> 태그를 이용해서 매핑을 하고 <servlet-mapping> 태그를 이용하여 URL 을 매핑합니다.

url-pattern 에는 / 슬래시가 들어가야합니다.

@WebServlet 을 이용해서 매핑했듯이 똑같이 url-pattern 에 집어넣으면 됩니다.

package servlet;


import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;


//@WebServlet("/ServletTest/*")
public class ServletTest extends HttpServlet {
	private static final long serialVersionUID = 1L;


	@Override
	protected void doGet(HttpServletRequest request, HttpServletResponse response)
	    throws ServletException, IOException {
		request.setCharacterEncoding("utf-8");
		response.setContentType("text/html;charset=utf-8");

		System.out.println(request.getRequestURL());

		// path 로 매핑하기
		String contextPath = request.getContextPath();
		String path = request.getRequestURI().substring(contextPath.length());
		System.out.println(contextPath);
		System.out.println(path);

		switch (path) {
		case "/ServletTest/list":
			list(request, response);
			break;
		case "/ServletTest/detail":
			detail(request, response);
			break;
		case "/ServletTest/regist":
			regist(request, response);
			break;
		default:
			break;
		}

	}


	@Override
	protected void doPost(HttpServletRequest request, HttpServletResponse response)
	    throws ServletException, IOException {
		doGet(request, response);
	}


	protected void list(HttpServletRequest request, HttpServletResponse response)
	    throws ServletException, IOException {
		System.out.println("list!!!");
		response.getWriter().append("LIST");
	}


	protected void detail(HttpServletRequest request, HttpServletResponse response)
	    throws ServletException, IOException {
		response.getWriter().append("DETAIL");
	}


	protected void regist(HttpServletRequest request, HttpServletResponse response)
	    throws ServletException, IOException {
		response.getWriter().append("REGIST");
	}

}

 

 

 

 

 

 

 

 

 


 

728x90

'🧶 𝗪𝗲𝗯 > Servlet' 카테고리의 다른 글

[Servlet] JSP 사용에서 MVC 패턴  (0) 2022.10.04
[Servlet] forward, sendRedirect  (1) 2022.10.04
[Servlet] Life Cycle  (1) 2022.10.04
[Servlet] Servlet 관련 객체 및 API  (1) 2022.10.04