-
[JSP] variable, import, request, includeWEB/JSP 2022. 9. 9. 18:47
variable
선언부(Declare)에 선언된 변수는 전역변수와 같이 사용되어 jsp 페이지 어디서나 사용 가능하며 값도 일관되게 유지됨. 또한 페이지를 새로고침해도 이전 값이 유지되는 특성이 있으며 이는 나중에 공부하게 될 세션값과 비슷하게 작용되어 서버가 재설정되거나 브라우저가 닫힐때까지 값이 유지되는 특성이 있음
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>03_Variable</title> <%! int global_cnt=0; %> </head> <body> <% int local_cnt=0; %> <h1>local_cnt: <%=++local_cnt %></h1> <h1>global_cnt: <%=++global_cnt %></h1> </body> </html>
창을 띄운 상태에서 새로고침(F5)하면 global_cnt는 1씩 추가됨
import
-모든 jsp파일에는 response 객체와 request 객체가 포함되어 있음
-jsp 파일에는 이미 존재하는 객체로 바로 out.print로 출력이 가능함
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <%@ page import="java.text.SimpleDateFormat" %> <%@ page import="java.util.Calendar" %> <%@ page import="java.util.Date" %> <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>04_Import.jsp</title> </head> <body> <% Calendar date=Calendar.getInstance(); SimpleDateFormat today=new SimpleDateFormat("yyyy년 MM월 dd일"); SimpleDateFormat now=new SimpleDateFormat("hh시 mm분 ss초"); %> <h1>오늘은 <% Date d=date.getTime(); out.print(today.format(d)); %> </h1> <h1>지금 시각은<% out.print(now.format(date.getTime())); %></h1> </body> </html>
request
-주로 서버에서 정보를 요청할 때 사용하는 객체이며, 객체의 각 정보들을 이용하여 사이트에 유용한 설정 및 실행에 이용함
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>05_Request.jsp</title> </head> <body> <!-- request 객체가 담고 있는 정보들 --> <h2> 컨텍스트 패스: <%=request.getContextPath() %><br> 요청 방식: <%=request.getMethod() %><br> 요청한 URL: <%=request.getRequestURL() %><br> 요청한 URI: <%=request.getRequestURI() %><br> 서버의 이름: <%=request.getServerName() %><br> 프로토콜: <%=request.getProtocol() %><br> </h2> </body> </html>
include
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>061_include.jsp</title> </head> <body> <h3>프론트 페이지</h3> <a href="062_sub.jsp">서브 페이지로 이동</a> <br> <%@ include file="063_footer.jsp" %> </body> </html>
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>062_sub.jsp</title> </head> <body> <h3>서브 페이지</h3> <a href="061_include.jsp">메인 페이지로 이동</a> <%@ include file="063_footer.jsp" %> </body> </html>
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <hr> <div id="copyright"> All contents Copyrigth 2019 all rights reserved<br> Contact mail: abc@abc.com Tel: +82 64 9876 6789 <!-- include 될 파일의 내용은 대상 파일에 html head title body 등이 있는 상태에 중간 삽입 형태로 들어가는 것이므로 내용만 기술하여 파일을 구성함 --> </div>
서브페이지 클릭하면 이동 메인 페이지 클릭하면 이동 'WEB > JSP' 카테고리의 다른 글
[JSP] servlet, action tag form (0) 2022.09.09 [JSP] login form, forward form, server object (0) 2022.09.09 [JSP] table, calendar, operator, declare (0) 2022.09.09 [JSP] sum, form, checkbox, select (0) 2022.09.09 [JSP] Java Server Pasge (0) 2022.09.09