본문 바로가기

Web

JSP 내장 객체

JSP를 실행하면 서블릿 소스가 생성되고 실행된다.

내장 객체란?

JSP에 입력한 스크립트릿 코드는 서블릿 소스의 _jspService() 메소드 안에 삽입되는데,

_jspService()에는 삽입된 코드 외에도 윗부분에 미리 선언된 객체들이 있고, 해당 객체들을 내장 객체라고 부른다.

내장 객체에는 response, request, application, session, out와 같은 변수가 존재하고, JSP에서도 사용 가능하다.

내장객체의 종류

 

 

 

내장 객체를 활용한 JSP 코드

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>

<%
//out, request는 내장객체
StringBuffer url = request.getRequestURL();

out.print("url :" + url.toString());
out.print("<br>");
%>
</body>
</html>

실행 결과

jsp실행시 생성된 java파일의 _jspService() 메소드 코드

  public void _jspService(final javax.servlet.http.HttpServletRequest request, final javax.servlet.http.HttpServletResponse response)
      throws java.io.IOException, javax.servlet.ServletException {

    final java.lang.String _jspx_method = request.getMethod();
    if (!"GET".equals(_jspx_method) && !"POST".equals(_jspx_method) && !"HEAD".equals(_jspx_method) && !javax.servlet.DispatcherType.ERROR.equals(request.getDispatcherType())) {
      response.sendError(HttpServletResponse.SC_METHOD_NOT_ALLOWED, "JSP들은 오직 GET, POST 또는 HEAD 메소드만을 허용합니다. Jasper는 OPTIONS 메소드 또한 허용합니다.");
      return;
    }
	
    final javax.servlet.jsp.PageContext pageContext;
    javax.servlet.http.HttpSession session = null;
    final javax.servlet.ServletContext application;
    final javax.servlet.ServletConfig config;
    javax.servlet.jsp.JspWriter out = null;
    final java.lang.Object page = this;
    javax.servlet.jsp.JspWriter _jspx_out = null;
    javax.servlet.jsp.PageContext _jspx_page_context = null;


    try {
 // 내장 객체
      response.setContentType("text/html; charset=UTF-8");
      pageContext = _jspxFactory.getPageContext(this, request, response,
      			null, true, 8192, true);
      _jspx_page_context = pageContext;
      application = pageContext.getServletContext();
      config = pageContext.getServletConfig();
      session = pageContext.getSession();
      out = pageContext.getOut();
      _jspx_out = out;

      out.write("\r\n");
      out.write("<!DOCTYPE html>\r\n");
      out.write("<html>\r\n");
      out.write("<head>\r\n");
      out.write("<meta charset=\"UTF-8\">\r\n");
      out.write("<title>Insert title here</title>\r\n");
      out.write("</head>\r\n");
      out.write("<body>\r\n");
      out.write("\r\n");

//out, request는 내장객체
StringBuffer url = request.getRequestURL();

out.print("url :" + url.toString());
out.print("<br>");

      out.write("\r\n");
      out.write("</body>\r\n");
      out.write("</html>");
    } catch (java.lang.Throwable t) {
      if (!(t instanceof javax.servlet.jsp.SkipPageException)){
        out = _jspx_out;
        if (out != null && out.getBufferSize() != 0)
          try {
            if (response.isCommitted()) {
              out.flush();
            } else {
              out.clearBuffer();
            }
          } catch (java.io.IOException e) {}
        if (_jspx_page_context != null) _jspx_page_context.handlePageException(t);
        else throw new ServletException(t);
      }
    } finally {
      _jspxFactory.releasePageContext(_jspx_page_context);
    }
  }
}

 

 

 

 

 

출처 : https://www.boostcourse.org/web316/lecture/19882/?isDesc=false

'Web' 카테고리의 다른 글

Servlet , JSP 연동하기 (forward)  (0) 2021.08.19
Redirect & Forward  (0) 2021.08.08
[eclipse] 자바 웹 어플리케이션 생성 (+ Finish버튼 비활성화 시)  (0) 2021.08.01
JSP란?  (0) 2021.08.01
WAS와 Web Server  (0) 2021.07.22