<예시_1>
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@page import="java.util.Date" %>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>jsp Test import test</title>
</head>
<body>
<h2> page지시자 import 속성</h2>
<%--import속성 : java의 import문장을 대체하는 속성 --%>
<% Date abc = new Date(); %>
현재 시각1 : <%= new Date().toLocaleString() %><br>
현재 시각2 : <%= abc.toString() %><br>
현재 시각3 : <%= new Date() %>
</body>
</html>
↓결과
Directive(지시자)
- 일반적인 프로그램 언어와는 달리 태그 형식을 이용하여 jsp페이지에 대한 속성 또는 지시사항을 지정하는 태그
종류 | 형태 | 의미 |
page | <%@ page property="property-value"%> | jsp 페이지에 대한 속성 지정 |
include | <%@ include file ="file-name" %> | 태그 부분에 지정한 페이지를 정적으로 삽입 |
taglib | <%@ taglib uri="uri-value" prefix = "prefix-value" %> | 새로운 태그를 정의하여 사용 |
Implicit Object(내장 객체)
- jsp페이지의 스크립트릿과 표현에서 객체의 생성없이 이용할 수 있는 객체 변수
- 웹 브라우저 출력에 이용하던 out는 jsp 서블릭의 _jspService()메소드에서 자동으로 선언되므로 jsp 페이지의 스크립트릿에서 생성과 선언없이 out.print() 사용가능
(I는 interface의미, C는 context를 의미)
내장객체 | 패키지 | 클리스명 | 사용용도 |
request | javax.sevlet.http | HttpServletRequest(I) | 클라이언트 요청에 의한 폼 양식 정보 처리 |
response | javax.servlet.http | HttpServletResponse(I) | 클라이언트 요청에 대한 응답 |
session | javax.servlet.http | HttpSesstion(I) | 클라이언트에 대한 세션 정보처리 |
application | javax.servlet | ServletContext(I) | 웹 어플리케이션 정보처리 |
config | javax.servlet | ServletConfig(I) | 현재 JSP페이지에 대한 환경처리 |
exception | java.lang | Throwable(I) | 예외처리를 위한 객체 |
page | java.lang | Object(C) | 현재 JSP페이지에 대한 클래스 정보 |
pageContext | javax.servlet.jsp | PageContext(C) | 현재 JSP 페이지에 대한 컨텍스트 |
out | javax.servlet.jsp | JspWriter(C) | 출력 스트림 |
1. request의 자료 유형과 인자 전달
- 클라이언트가 서버에게 전송하는 정보를 처리하는 객체
→ HTML폼에 입력해 값을 전송하는 경우 내장객체 request를 사용 - 인터페이스 HttpServletRequest로 상위 인터페이스는 Javax.servlet.ServletRequest를 가짐
- jsp class docs: http://docs.oracle.com/javaee/7/api/toc.htm
- request 메소드 종류
반환값 | 메소드 | 사용용도 |
void | setCharacterEncoding(String env) | 요청 페이지에 인코딩 방법 적용 |
String | getParameter(String name) | name의 요청 인자값을 반환 없으면 null 여러개의 값이면 첫번째 값 반환 |
String[] | getParameterValues(String name) | 지정한 name 요청 인자값을 문자열 배열로 변환 |
Enumeration | getParameterNames() | 모든 인자의 이름을 Enumeration으로 반환 |
String | getProtocol() | 사용중인 프로토콜 반환 |
String | getRemoteAddr() | 클라이언트의 IP주소 반환 |
String | getRemoteHost() | 클라이언트의 호스트이름 반환 |
String | getServerName() | 요청된 서버의 호스트이름 반환 |
int | getServerPort() | 요청된 서버의 포트번호 반환 |
Cookie[] | getCookies() | 클라이언트에 보내진 쿠키 배열 반환 |
String | getQueryString() | URL에 추가된 Query 문자열 반환 |
String | getRequestURI() | 클라이언트가 요청한 URI반환 URI는 프로토콜, 서버이름, 포트번호를 제외한 서버의 컨텍스트와 파일의 문자열 |
→ 예시) http://localhost:8088/fist_jsp/jsp_12/request_3.jsp URI : fist_jsp/jsp_12/request_3.jsp URL : http://localhost:8088/fist_jsp/jsp_12/request_3.jsp |
||
String | getRequestURL() | 클라이언트가 요청한 URL반환 URL은 프로토콜과 함께 주소부분에 기술된 모든 문자열 |
HttpSession | getSesstion() | 현재의 세션을 반환, 세션이 없으면 새로 만들어 반환 |
String | getMethod() | 요청방식인 get, post중 하나를 반환 |
<예시_1>
include.jsp파일 생성)
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>jsp test include test</title>
</head>
<body>
<%@ include file="header.jsp" %>
<hr size="5" color="red">
<p>
include 지사자 : <%@ include file="file_name" %>
</p>
<hr size="5" color="gren">
<p>
<%@ include file="footer.html" %>
</p>
</body>
</html>
header.jsp 파일 생성)
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>include header file</title>
</head>
<body>
<hr size="9" color="blue">
<h2>
<font color="red" size="24">include header est</font>
</h2>
머릿말<br>
머릿말<br>
</body>
</html>
footer.jsp 파일 생성)
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>include footer test</title>
</head>
<body>
<hr color="green" size="10">
<font color="orange" size="12">Copyright</font>
</body>
</html>
↓결과
<예시_2>
errorpage.jsp 파일생성)
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>jsp test errorpage test</title>
</head>
<body>
<%@ page errorPage="exception.jsp" %>
<%
String[] str ={"감사합니다","Thank you"};
%>
한국어 : [<%=str[0] %>]<br>
영어 : [<%=str[2] %>]
</body>
</html>
exception.jsp 파일 생성)
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>jsp test exception.jsp</title>
</head>
<body>
<%@ page isErrorPage="true" %>
<p>
<h2>처리 중 에러발생</h2>
<img src="..\images\error.jpg"><br>
빠른 시일내에 복구하겠습니다
<p>
exception.getMessage() : <%=exception.getMessage() %>
exception.toString() : <%=exception.toString() %>
</body>
</html>
↓결과
<예시_3>
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>jsp test declaration test</title>
</head>
<body>
<%! double radius =4.8; %>
<%!
public double getArea(double r){
return r*r*3.14;
}
%>
반지름: <%= radius %><br>
원의 면적 : <%= getArea(radius) %>
</body>
</html>
↓결과
<예시_4_1>
request.html 파일 생성)
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Request</title>
</head>
<body>
<h2>학생 정보 입력</h2>
<form action="request_1.jsp" method="post">
이름 : <input type="text" name="name">
<p>
학번 : <input type="text" name="studentNum">
<p>
주소 : <input type="text" name="addr">
<p>
성별 : 남자 <input type="radio" name="sex" value="man" checked="checked">
여자 <input type="radio" name="sex" value="woman">
<p>
국적 : <select name="country">
<option selected="selected" value="한국">한국</option>
<option value="일본">일본</option>
<option value="영국">영국</option>
<option value="프랑스">프랑스</option>
<option value="미국">미국</option>
<option value="중국">중국</option>
</select>
<p>
<input type="submit" value="전송">
</form>
</body>
</html>
request_1.jsp 파일 생성)
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>jsp test request_1.jsp test</title>
</head>
<body>
<%
request.setCharacterEncoding("UTF-8");
%>
<%
/*"name값"의 value값 리턴
1개의 값만 리턴받을 경우 getParameter사용
*/
String name = request.getParameter("name");
String studentNum = request.getParameter("studentNum");
String address = request.getParameter("addr");
String sex = request.getParameter("sex");
String country = request.getParameter("country");
%>
<h2>학생 정보 입력 결과</h2>
<hr size="5" color="red">
성명 :<%=name%><p>
학번 :<%=studentNum%><p>
주소 :<%=address%><p>
성별 :<%=sex%><p>
국적 :<%=country%><p>
</body>
</html>
↓결과
<예시_4_2>
예시4_1에서 tel 추가 및 성별을 한글로 변경
방법1)
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Request</title>
</head>
<body>
<h2>학생 정보 입력</h2>
<form action="request_1.jsp" method="post">
이름 : <input type="text" name="name">
<p>
학번 : <input type="text" name="studentNum">
<p>
주소 : <input type="text" name="addr">
<p>
전화번호 : <input type="text" name="tel">
<p>
성별 : 남자 <input type="radio" name="sex" value="남" checked="checked">
여자 <input type="radio" name="sex" value="여">
<p>
국적 : <select name="country">
<option selected="selected" value="한국">한국</option>
<option value="일본">일본</option>
<option value="영국">영국</option>
<option value="프랑스">프랑스</option>
<option value="미국">미국</option>
<option value="중국">중국</option>
</select>
<p>
<input type="submit" value="전송">
</form>
</body>
</html>
방법2)
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>jsp test request_1.jsp test</title>
</head>
<body>
<%
/* 이 문장을 적지 않으면 한글이 깨짐
*/
request.setCharacterEncoding("UTF-8");
%>
<%
String name = request.getParameter("name");
String studentNum = request.getParameter("studentNum");
String address = request.getParameter("addr");
String tel = request.getParameter("tel");
String sex = request.getParameter("sex");
String country = request.getParameter("country");
/* equalsIgrnoreCase() : 대소문자 구분없이 비교
*/
if(sex.equalsIgnoreCase("man")){
sex="남자";
}else{
sex="여자";
}
--%>
<h2>학생 정보 입력 결과</h2>
<hr size="5" color="red">
성명 : <%=name%><p>
학번 : <%=studentNum%><p>
주소 : <%=address%><p>
전화번호 : <%=tel%><p>
성별 : <%=sex%><p>
국적 : <%=country%><p>
</body>
</html>
<예시_5>
request_2.html 파일 생성)
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>jsp request_2 test</title>
</head>
<body>
<h2>학생 정보 입력</h2>
<form action="request_2.jsp" method="post">
학번 : <input type="text" name="studentNum"><p>
전공 : <select multiple ="multiple" name="major">
<option value="전산과" selected="selected">전산과</option>
<option value="국문과">국문과</option>
<option value="기계과">기계과</option>
<option value="문화창작과">문학창작과</option>
<option value="영문과">영문과</option>
<option value="수학과">수학과</option>
</select><p>
취미 : <select multiple ="multiple" name="hobby">
<option value="등산" selected="selected">등산</option>
<option value="낚시" selected="selected">낚시</option>
<option value="책읽기">책읽기</option>
<option value="영화보기">영화보기</option>
<option value="자전거타기">자전거타기</option>
</select><p>
<input type="submit" value="확인">
</form>
</body>
</html>
request_2.jsp 파일 생성)
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>jsp test request_2.jsp test</title>
</head>
<body>
<%
request.setCharacterEncoding("utf-8");
String studentNum = request.getParameter("studentNum");
String[] majors = request.getParameterValues("major");
String[] hobbies = request.getParameterValues("hobby");
%>
<h2>학생 정보 입력 결과</h2>
<hr size="5" color="red">
학번 : <%=studentNum %><p>
전공 : <%
if(majors==null){
out.print("전공없음");
}else{
for(int i =0;i<majors.length;i++)
out.print(majors[i]+" ");
}
%><p>
취미 : <%
if(hobbies==null){
out.print("취미없음");
}else{
for(String eachHobby : hobbies) //향상된 for문
out.print(eachHobby+" ");
}
%><p>
</body>
</html>
↓결과
<예시_6>
request_3.html 파일 생성)
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>jsp request_3 test</title>
</head>
<body>
<hr size="5" color="blue">
<h2 >국가 및 도시 선택</h2>
<form action="request_3.jsp" method="post">
1. 국가 선택<p>
영국<input type="checkbox" name="country" value="영국"><br>
미국<input type="checkbox" name="country" value="미국"><br>
프랑스<input type="checkbox" name="country" value="프랑스"><br>
이탈리아<input type="checkbox" name="country" value="영국"><br>
중국<input type="checkbox" name="country" value="중국"><br>
<p>
<hr size="5" color="red">
2. 도시 선택<p>
인천<input type="radio" name="city" value="인천"><br>
서울<input type="radio" name="city" value="서울"><br>
부산<input type="radio" name="city" value="부산"><br>
제주도<input type="radio" name="city" value="제주도"><br>
<hr size="5" color="green">
<input type="submit" value="확인">
</form>
</body>
</html>
request_3.jsp 파일 생성)
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>jsp request_3 test</title>
</head>
<body>
<%@ page import="java.util.Enumeration" %>
<%
request.setCharacterEncoding("utf-8");
%>
<h2>국가, 도시 선택</h2>
<hr size="5" color="red">
<%
Enumeration<String> ee= request.getParameterNames();
while(ee.hasMoreElements()){
String name = ee.nextElement();
String[] data = request.getParameterValues(name);
if(data!= null){
for(String eachData : data){
out.println(eachData+" ");
}
}
out.println("<p>");
}
%>
</body>
</html>
↓결과
<예시_7>
request_4.html파일 생성)
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>request_4 test</title>
</head>
<body>
<h1>Request Test</h1>
<form action="request_4.jsp" method="post">
<table border="1">
<tr>
<td>이름</td>
<td><input type="text" name="name"></td>
</tr>
<tr>
<td>성별</td>
<td>남<input type="radio" name="gender" value="male">
여<input type="radio" name="gender" value="female"></td>
</tr>
<tr>
<td>취미</td>
<td>독서 <input type = "checkbox" name = hobby value="독서">
영화보기 <input type = "checkbox" name = hobby value="영화보기">
등산 <input type = "checkbox" name = hobby value="등산"></td>
</tr>
<tr>
<td colspan="2" align="center"><input type="submit" value="전송"></td>
</tr>
</table>
</form>
</body>
</html>
request_4.jsp 파일 생성)
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>request_4 test</title>
</head>
<body>
<% request.setCharacterEncoding("utf-8"); %>
<h1>request test</h1>
<table border="1">
<tr>
<td>이름 </td>
<td><%=request.getParameter("name") %></td>
</tr>
<tr>
<td>성별</td>
<td>
<%
if(request.getParameter("gender").equals("male")){
%>
남자
<%
}else{
%>
여자
<% } %>
</td>
</tr>
<tr>
<td>취미</td>
<td>
<%
String [] hobbies = request.getParameterValues("hobby");
for(int i = 0; i<hobbies.length;i++){
%>
<%=hobbies[i]%>
<%} %>
</td>
</tr>
</table>
</body>
</html>
↓결과
<예시_8>
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<h1>url, uri 요청방식, cookis, 정보</h1>
<table border="1">
<tr>
<td>쿠키 정보</td>
<%
Cookie[] cookie = request.getCookies();
if(cookie==null){
%>
<td>쿠키가 존재하지 않습니다</td>
<%}else{
for(int i=0; i<cookie.length;i++){
%>
<td><%=cookie[i].getName() %>( <%=cookie[i].getValue() %>) </td>
<%
}
}
%>
</tr>
<tr>
<td>서버 도메인 명</td>
<td><%= request.getServerName() %></td>
</tr>
<tr>
<td>서버 포트번호</td>
<td><%=request.getServerPort() %></td>
</tr>
<tr>
<td>요청 URL</td>
<td><%=request.getRequestURL()%></td>
</tr>
<tr>
<td>요청 URI</td>
<td><%=request.getRequestURI()%></td>
</tr>
<tr>
<td>요청 쿼리</td>
<td><%=request.getQueryString()%></td>
</tr>
<tr>
<td>클라이언트 호스트명</td>
<td><%=request.getRemoteHost()%></td>
</tr>
<tr>
<td>클라이언트 IP주소</td>
<td><%=request.getRemoteAddr()%></td>
</tr>
<tr>
<td>프로토콜</td>
<td><%=request.getProtocol()%></td>
</tr>
<tr>
<td>요청방식</td>
<td><%=request.getMethod()%></td>
</tr>
<tr>
<td>컨텍스트 경로</td>
<td><%=request.getContextPath()%></td>
</tr>
</table>
<%
//
%>
</body>
</html>
JESSIONID
- 톰캣 컨테이너에서 세션을 유지하기 위해 발급하는 키
- HTTP프로토콜은 stateless(상태 정보를 유지하지 않는다)하다. 요청시 마다 새로운 연결이 생성되고 응답 후 연결은 끊기게 되므로 상태를 유지 할 수 없음
→ 상태를 저장하기 위해 톰캣은 JESSIONID 쿠키를 클라이언트에게 발급해주고 이 값을 통해 세션을 유지할 수 있도록 함
↓결과
'JSP' 카테고리의 다른 글
221011_JSP_JDBC_국비_기초 (0) | 2022.10.11 |
---|---|
221011_JSP_국비_자바빈즈2 (0) | 2022.10.11 |
221010_JSP_국비_session/ cookie/ 자바빈즈 (0) | 2022.10.10 |
221006_JSP_국비_response/ application/ pageContext/ 액션태그 (0) | 2022.10.06 |
221004_JSP_국비_JSP엔진/ 기본문법/ 실행/ 라이프사이클 (1) | 2022.10.04 |