여러 요청 한 서블릿에서 처리하기
여러 요청 한 서블릿에서 처리하기
JAVA - 여러 요청 한 서블릿에서 처리하기
여러 요청을 하나의 서블릿에서 처리할수 있다.
서블릿에서 request의 주소 반환
1
2
String uri = request.getRequestURI();
//반환값 : /프로젝트이름/요청된 jsp페이지
프로젝트이름 반환
1
2
String conPath = request.getContextPath();
//반환값: /프로젝트이름
요청된 jsp페이지 반환
1
2
String com = uri.substring(conPath.length());
//반환값 : /요청된 jsp패이지
서블릿 어노테이션
1
2
@WebServlet("/")
//어떤 요청을 받을지 모르기 때문에 이렇게 작성한다.
주어진 URL에 따라 지정된 동작 수행
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
String view = null;
//주어진 URL에 따라 지정된 동작 수행
if(com.equals("/a") || com.equals("/")) {
view = "a.jsp";
} else if(com.equals("/b")) {
view = "redirect:b.jsp";
} else if(com.equals("/view")) {
view = "redirect:view.jsp";
} else if(com.equals("/regist")) {
view = "redirect:regist.jsp";
} else if(com.equals("/signup")) {
String id = request.getParameter("id");
System.out.println(id);
view = "redirect:index.jsp";
}
//view에 담긴 문자열에 따라 포워딩 또는 리다이렉팅
if(view.startsWith("redirect:")) {
response.sendRedirect(view.substring(9));
} else {
request.getRequestDispatcher(view).forward(request, response);
}
This post is licensed under CC BY 4.0 by the author.