<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>sum10</title>
</head>
<body>
<%
int total = 0;
for(int i = 1; i <= 10; i++){
total += i;
}
%>
1부터 10까지의 합 : <%=total %>
</body>
</html>
<% %>안에 java 코드를 사용할 수 있다.
<%=total%> === out.print(total)
public void _jspInit() {
}
public void _jspDestroy() {
}
public void _jspService(final javax.servlet.http.HttpServletRequest request, final javax.servlet.http.HttpServletResponse response)
throws java.io.IOException, javax.servlet.ServletException {
.....
try {
.....
out.write("\n");
out.write("<!DOCTYPE html PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\" \"http://www.w3.org/TR/html4/loose.dtd\">\n");
out.write("<html>\n");
out.write("<head>\n");
out.write("<meta http-equiv=\"Content-Type\" content=\"text/html; charset=UTF-8\">\n");
out.write("<title>sum10</title>\n");
out.write("</head>\n");
out.write("<body>\n");
out.write("\n");
int total = 0;
for(int i = 1; i <= 10; i++){
total = total + i;
}
스크립틀릿 부분이라고 java 코드를 입력한 부분은 다음과 같이 입력되어 있다.
int total = 0;
for(int i = 1; i <= 10; i++){
total = total + i;
}
표현식으로 출력한 부분은 다음과 같다.
out.print(total );
1.브라우저가 웹서버에 JSP에 대한 요청 정보를 전달한다.
2.브라우저가 요청한 JSP가 최초로 요청했을 경우만 JSP로 작성된 코드가 서블릿으로 코드로 변환한다. 👀(java 파일 생성)
3.서블릿 코드를 컴파일해서 실행가능한 bytecode로 변환한다. 👀(class 파일 생성)
4.서블릿 클래스를 로딩하고 인스턴스를 생성한다.
5.서블릿이 실행되어 요청을 처리하고 응답 정보를 생성한다.
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
hello
<%
System.out.println("_jspService()");
%>
<%!
public void jspInit() {
System.out.println("jspInit()!");
}
public void jspDestroy() {
System.out.println("jspDestroy()");
}
%>
</body>
</html>
- 서블릿 라이프 싸이클 - init(), service(), destroy()
- JSP 라이프 싸이클 - _jspInit(), _jspService(), _jspDestroy()
JSP 페이지에서는 선언문(Declaration), 스크립트릿(Scriptlet), 표현식(Expression) 이라는 3가지의 스크립트 요소를 제공

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
id : <%=getId() %>
</body>
</html>
<%!
String id = "u001"; //멤버변수 선언
public String getId( ) { //메소드 선언
return id;
}
%>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<%
for(int i = 1; i <= 5; i++){
%>
<H<%=i %>> 아름다운 한글 </H<%=i %>>
<%
}
%>
</body>
</html>

코드에서 아름다운한글 부분을 보면 <% } %> 이런 식으로 따로 줄수도 있으며, html 태그도 위처럼 같이 사용할 수 있다.
<!--로 시작해서 -->로 끝나는 형태HTML주석의 예시
<!-- html 주석입니다. -->
<%--로 시작해서 --%>로 끝나는 형태JSP주석의 예시
<%-- JSP 주석입니다. --%>
//, /**/을 사용해서 작성.//은 한 줄짜리 주석을 작성할 때 사용되고, /**/은 여러 줄의 주석을 작성할 때 사용자바주석의 예시
//주석
/*주석
여러 줄에 걸친 주석이다.
*/
_jspService() 메소드 안에 삽입되는 코드로 생성된다._jspService()에 삽입된 코드의 윗부분에 미리 선언된 객체들이 있는데, 해당 객체들은 jsp에서도 사용 가능하다.response, request, application, session, out과 같은 변수를 내장객체라고 한다.
<body>
<%
StringBuffer url = request.getRequestURL();
out.println("url : " + url.toString());
out.println("<br>");
%>
request와 out은 내장 객체 이므로 변수를 선언 하지 않아도 오류없이 사용할 수 있다.