위로
아래
서블릿 스코프
서블릿 속성
- 서블릿 API 클래스에 저장되는 객체(정보)
- ServletContext
- HttpSession
- HttpServletRequest
스코프
- 서블릿 API에 바인딩된 속성에 대한 접근 범위
- 애플리케이션 스코프 : ServletContext에 바인딩된 속성. 애플리케이션 전체에서 접근 가능
- 세션 스코프 : HttpSession에 바인딩된 속성. HttpSession에 해당하는 브라우저에만 접근 가능
- 리퀘스트 스코프 : HttpServletRequest에 바인딩된 속성. 해당 요청, 응답에 대해서만 접근 가능
예시
더보기
// SetAttribute 서블릿
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
@WebServlet("/set11")
public class SetAttribute extends HttpServlet {
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
response.setContentType("text/html;charset=utf-8");
PrintWriter out= response.getWriter();
String ctxMesg = "context에 바인딩됩니다.";
String sesMesg = "session에 바인딩됩니다.";
String reqMesg = "request에 바인딩됩니다.";
ServletContext ctx = getServletContext();
HttpSession session = request.getSession();
ctx.setAttribute("context", ctxMesg);
session.setAttribute("session", sesMesg);
request.setAttribute("request", reqMesg);
out.print("바인딩을 수행합니다.");
}
}
// GetAttribute 서블릿
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
@WebServlet("/get11")
public class GetAttribute extends HttpServlet {
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
response.setContentType("text/html;charset=utf-8");
PrintWriter out= response.getWriter();
ServletContext ctx = getServletContext();
HttpSession sess = request.getSession();
String ctxMesg = (String)ctx.getAttribute("context");
String sesMesg = (String)sess.getAttribute("session");
String reqMesg = (String)request.getAttribute("request");
out.print("context값 : " + ctxMesg + "<br>");
out.print("session값 : " + sesMesg + "<br>");
out.print("request값 : " + reqMesg + "<br>");
}
}
브라우저에서 context와 session에는 접근할 수 있으나, request는 이미 요청이 끝나서 접근이 불가능하다.
서블릿 URL 패턴 종류
서블릿의 매핑 종류
- 정확히 이름까지 일치하는지
- 디렉터리까지만 일치하는지
- 확장자만 일치하는지
이름까지 일치
더보기
import java.io.IOException;
import java.io.PrintWriter;
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("/first/test")
public class TestServlet1 extends HttpServlet {
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
request.setCharacterEncoding("utf-8");
response.setContentType("text/html;charset=utf-8");
PrintWriter out = response.getWriter();
String context = request.getContextPath();
String url = request.getRequestURL().toString();
String mapping = request.getServletPath();
String uri = request.getRequestURI();
out.println("<html><head><title>Test Servlet1</title></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><br>");
out.println("</body></html>");
out.close();
}
}
디렉터리 이름만 일치
더보기
import java.io.IOException;
import java.io.PrintWriter;
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("/first/*")
public class TestServlet2 extends HttpServlet {
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
request.setCharacterEncoding("utf-8");
response.setContentType("text/html;charset=utf-8");
PrintWriter out = response.getWriter();
String context = request.getContextPath();
String url = request.getRequestURL().toString();
String mapping = request.getServletPath();
String uri = request.getRequestURI();
out.println("<html><head><title>Test Servlet2</title></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><br>");
out.println("</body></html>");
out.close();
}
}
확장자 일치
더보기
import java.io.IOException;
import java.io.PrintWriter;
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("*.do") // 확장자 일치하는 패턴
// @WebServlet("/*") // 모든 패턴
public class TestServlet3 extends HttpServlet {
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
request.setCharacterEncoding("utf-8");
response.setContentType("text/html;charset=utf-8");
PrintWriter out = response.getWriter();
String context = request.getContextPath();
String url = request.getRequestURL().toString();
String mapping = request.getServletPath();
String uri = request.getRequestURI();
out.println("<html><head><title>Test Servlet3</title></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><br>");
out.println("</body></html>");
out.close();
}
}