JSP -5

김정현·2024년 6월 12일
0

JSP

목록 보기
6/13

JSP

:(Java Server Page) - 서블릿 코드 번역기술

  1. JSP는 서블릿 기술의 확장
  2. JSP는 유지 관리가 용이
  3. JSP는 빠른 개발이 가능
  4. JSP로 개발하면 코드 길이를 줄일 수 있다.
  • JSP의 페이지 처리과정

post.jsp -> post_jsp.java -> 컴파일 -> post_jsp.class -> 실행 -> _jspInit() - 한번 -> _jspService() - 매 요청시 -> _jspDestroy();
//post.jsp에서

//_jspService 메서드 지역 내부 안에 정의 됨.

<%@ page contentType="text/html; charset=UTF-8" %>
<h1>게시글 작성</h1>
<form method="post" action="post_ps.jsp">
    제목 : <input tpye='text' name = 'subject'><br>
    내용 : <textarea name = "content"></textarea><br>
    <button type='subimt'>작성하기</button>
</form>

//post_jsp 에서 (자동 번역)

out.write("\r\n");
      out.write("<h1>게시글 작성</h1>\r\n");
      out.write("<form method=\"post\" action=\"post_ps.jsp\">\r\n");
      out.write("    제목 : <input tpye='text' name = 'subject'><br>\r\n");
      out.write("    내용 : <textarea name = \"content\"></textarea><br>\r\n");
      out.write("    <button type='subimt'>작성하기</button>\r\n");
      out.write("</form>");

JSP 생명 주기

_jspInit() : 초기화시(처음만 호출)
_jspService(....) : 매 요청시
_jspDestroy() : 소멸시

스크립트 태그의 종류

1) 선언문
- 번역 위치가 클래스명 바로 아래쪽 추가 (멤버 변수, 메서드)

<%!
		자바코드 ...
	%>

<%!
    // 클래스명 바로 아래쪽 - 멤버 변수
    String name = "이이름";
    void print() {
        System.out.println(name);
    }
%>

2) 스크립틀릿
_jspService 메서드의 지역 내에 코드 추가 (메서드 정의 X, 변수 -> 지역변수)

<%
		자바코드 ...   
	%>
    
<%
    // _jspService() 지역 내부
    int num1 = 100;
    int num2 = 200;
    int result = num1 + num2;
    out.write("result=" + result);

    out.write("<br>name : " + name);
    print();
%>    

3) 표현문(expression)
_jspService 메서드의 지역 내에 번역

<%=변수%> = out.print(변수)

<%=num1%> + <%=num2%> = <%=result%>

반드시 암기

HttpServletRequest request
HttpServletResponse response
PageContext pageContext (내장객체 만들어줌)
ServletContext application
ServletConfig config
JspWriter out
Object page = this

0개의 댓글