[jsp] 기본 설명

java 코드를 web에서 사용하기 위해 고안된 jsp를 학습합니다.

jsp

  • jsp는 java server page의 약자이다.
  • java + html = jsp
  • 웹페이지를 생성하는 언어이다.
  • jsp는 html코드 안에서 자바 코드를 사용하기 위한 기능이다.
    • 서블릿은 자바 코드 안에 html이 있다.
  • java코드가 있는 곳 => script + applet = scriptlet
  • jsp 목적 : 클라이언트와 통신을 하기 위해서 (클라이언트를 생성)

index.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
  <p>hello world</p>

  <% System.out.println("Hello world"); %>
  <% out.println("Hello world"); %>
</body>
</html>
  • 기본 jsp 페이지
  • sciptlet을 사용할 수 있다.
  • System.out.println()은 자바 콘솔에 Hello world가 출력된다.
  • out.println()은 html에 Hello world가 출력된다.
    • out은 내장객체.
    • new 를 하지 않고 클래스명 그대로 사용할 수 있는 객체
    • request(요청), response(응답)

스크립트릿의 종류

<%  %>  : 페이지를 로드하면 값이 초기화 된다.
<%! %>  : 값이 초기화 되지 않는다. static변수

index.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>

<%!
public class Human{
  private int age;
  private String name;

  public Human(){
  	age = 10;
  	name = "parker";
  }
  public String toString(){
  	return "age: "+ this.age +"<br>" +"name: " +this.name;
  }
}
%>

<%
Human human = new Human();
out.println("<p>"+human+"</p>");
%>

</body>
</html>
  • jsp는 객체를 생성하고 불러오는 것도 가능하다.
  • 위의 index.jsp는 아래의 html파일로 변환되어 클라이언트에 보여진다.
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>

<p>age: 10<br>name: parker</p>

</body>
</html>

Comments