JSP

정태규·2022년 11월 2일
1

JSP(java server page)는 말그대로 서버의 페이지를 보여준다.

경로는 src/main/webapp/ 에 있다.

<%@ page contentType="text/html;charset=utf-8"%>
<%@ page import="java.util.Random" %>
<%-- <%! 클래스 영역 %> --%>
<%!  
	int getRandomInt(int range){
		return new Random().nextInt(range)+1;
	}
%>
<%-- <%  메서드 영역 - service()의 내부 %> --%>
<%
	int idx1 = getRandomInt(6);
	int idx2 = getRandomInt(6);
%>
<html>
<head>
	<title>twoDice.jsp</title>
</head>
<body>
	<img src='resources/img/dice<%=idx1%>.jpg'>
	<img src='resources/img/dice<%=idx2%>.jpg'>
</body>
</html>

이런식으로 나타내어진다. html 안에 java코드를 넣는거라고 볼 수 있다.
값을 넣을때는 <%=idx1%> 이런식으로 쓴다.
html부분은 자동으로 out.println으로 바뀐다고 생각하면 된다.

<% %> 이부분은 메서드 영역이고, 메서드 영역 아래로는 service() 영역이다. 실질적인 구현부라고 할 수 있다.
이곳에 선언된 변수들은 지역변수이다.

<%! %>이 부분은 클래스 영역으로, iv나 cv선언등을 하면된다.

jsp파일을 추가해줄때는 src/main/webapp에서 마우스 오른쪽->new->other->jsp라고 치고 jps file을 선택해주면 된다.

페이지를 호출할때는 title 사이에있는 twoDice.jsp를 적어주면 된다.
ex) http://localhost/ch2/twoDice.jsp

0개의 댓글