Post

반응형

JSP 태그, request, response, 지시자, 액션태그


JSP 태그


  • JSP는 html코드에 java를 사용하여 다이나믹 웹을 만들 수 있다.
  • html에서 java를 사용하기 위해 jsp 태그를 사용한다.
  • jsp 태그
    • 지시자 : <%@ @> 페이지 속성
    • 주석 : <%-- --%>
    • 선언 : <%! %> 변수, 메소드 선언
    • 표현식 : <%= %> : 결과값 출력
    • 스크립트릿 : <% %> java 코드
    • 액션태크 : <jsp:action> </jsp:action> : 자바빈 연결

JSP 동작 원리


  • client가 web에 jsp 요청 시 jsp 컨테이너가 jsp파일을 servlet파일(.java)로 변환
  • 그리고 servlet파일(.java)은 컴파일 된 후 클래스 파일(.class)로 변환되고 html 파일로 응답함.
  • jsp가 요청을 받을 시 servlet, class 파일로 변환되고 메모리에 올려지고, 요청 응답을 한다.
  • 다시 요청을 받을 시 메모리에 이미 올려져있으므로, 메모리에 올려진 데이터를 재활용해서 응답 한다. 

JSP 내부 객체


  • 개발자가 객체를 생성하지 않고 바로 사용할 수 있는 객체.
  • JSP컨테이너에 의해 Servlet으로 변화될 때 자동으로 객체가 생성 됨.
  • 내부객체 종류
    • 입출력 객체 : request, response, out
    • 서블릿 객체 : page, config
    • 세션 객체 : session
    • 예외 객체 : exception

JSP 스크립트릿, 선언, 표현식


  • <% java 코드 기술 %>
  • jsp페이지에서 java 언어를 사용하기 위한 요소
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
<%@ page language="java" contentType="text/html; charset=EUC-KR"
    pageEncoding="EUC-KR"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="EUC-KR">
<title>Insert title here</title>
</head>
<body>
<%
int i = 0;
for(i = 0; i < 10++i)
{
       out.println(i+":"+"HelloWorld"+"<br/>");
}
%>
</body>
</html>
cs


  • <% java 선언 기술 %>
  • jsp페이지에서 변수 또는 메소드를 선언할 때 사용함.
  • 변수는 '<%' 사용시 에러 안나는데 함수는 에러남.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
<%@ page language="java" contentType="text/html; charset=EUC-KR"
    pageEncoding="EUC-KR"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="EUC-KR">
<title>Insert title here</title>
</head>
<body>
<%!
int a = 10;
int b = 20;
%>
<%!
public int sum(int a, int b)
{
       return a+b;   
}
%>
<%
out.println(a+"+"+b+"="+sum(a, b)+"<br/>");
%>
</body>
</html>
cs


  • <%= java 코드 기술 %>
  • 변수의 값 또는 메소드 호출 결과값을 출력하기 위해 사용됨.
  • 결과값은 String 타입이며 ;를 사용할 수 없다.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
<%@ page language="java" contentType="text/html; charset=EUC-KR"
    pageEncoding="EUC-KR"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="EUC-KR">
<title>Insert title here</title>
</head>
<body>
<%!
int a = 10;
int b = 20;
%>
<%=%></br>
<%=%></br>
</body>
</html>
cs


JSP 지시자


  • JSP 페이지의 전체적인 속성을 지정할 때 사용.
  • <%@ 속성 %>
  • page : JSP페이지 속성. import 할 때도 쓴다.
1
2
<%@ page language="java" contentType="text/html; charset=EUC-KR"
    pageEncoding="EUC-KR"%>
cs


  • include : 다른 page의 내용을 삽입함.
  • JspEx.jsp, IncludeEx.jsp 파일을 만들고 JspEx.jsp 소스에 include 지시자를 사용한다.
  • 출력결과  JspEx.jsp 페이지 IncludeEx.jsp 페이지 라고 나오는 것을 확인할 수 있다.
JspEx.jsp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
<%@ page import="java.util.Arrays" %>
<%@ page language="java" contentType="text/html; charset=EUC-KR"
    pageEncoding="EUC-KR"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="EUC-KR">
<title>Insert title here</title>
</head>
<body>
JspEx.jsp 페이지
<%@ include file="IncludeEx.jsp" %>
</body>
</html>
cs


IncludeEx.jsp

1
2
3
4
5
6
7
8
9
10
11
12
13
<%@ page language="java" contentType="text/html; charset=EUC-KR"
    pageEncoding="EUC-KR"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="EUC-KR">
<title>Insert title here</title>
</head>
<body>
IncludeEx.jsp 페이지
</body>
 
</html>
cs


taglib 지시자


  • 사용자가 만든 tag들을 태그 라이브러리라고 함.
  • 유명한 라이브러리로 JSTL이 있음.

JSP 주석


  • <%-- 주석 -->
  • JSP 주석은 페이지 소스 보기로 보이지 않음.
  • JSP 안에서 java 주석을 사용해도 됨.

request 객체


  • web client가 server로 정보를 요청하는 것을 request라고 함.
  • 요청정보는 request 객체가 관리함.
  • 메소드
    • getServerName() : 서버 이름 얻어옴
    • getServerPort() : 서버 포트 번호 얻어옴
    • getContextPath() : 웹어플리케이션의 컨텍스트 패스를 얻음.
    • getMethod() : get, post 방식 구분
    • getSession() : session 객체 얻음.
    • getProtocol() : protocol 객체 얻음.
    • getRequestURL() : 요청 URL을 얻음.
    • getRequestURI() : 요청 URI를 얻음.
    • getQueryString() : 쿼리스트링을 얻음.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
<%@ page language="java" contentType="text/html; charset=EUC-KR"
    pageEncoding="EUC-KR"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="EUC-KR">
<title>Insert title here</title>
</head>
<body>
<%
out.println("ServerName:"+request.getServerName()+"<br/>");
out.println("ServerPort:"+request.getServerPort()+"<br/>");
out.println("ContextPath:"+request.getContextPath()+"<br/>");
out.println("Get or Post?:"+request.getMethod()+"<br/>");
out.println("URL:"+request.getRequestURL()+"<br/>");
out.println("URI:"+request.getRequestURI()+"<br/>");
out.println("QueryString:"+request.getQueryString()+"<br/>");
%>
</body>
</html>
cs


Parameter메소드


  • JSP 코드로 요청받은 정보의 값을 얻어온다.
  • 메소드
    • getParameterNames() : Parameter 이름 구함
    • getParameter(String name) : Parameter 이름으로 값을 찾음.
    • getParameterValues(String name) : 배열같이 여러개의 값을 가지고 있을 때 이름을 구함.

form.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
<!DOCTYPE html>
<html>
<head>
<meta charset="EUC-KR">
<title>Insert title here</title>
</head>
<body>
<form action="JspEx.jsp" method="post">
이름:<input type="text" name="name" size="10"><br/>
아이디:<input type="text" name="id" size="10"><br/>
비밀번호:<input type="password" name="pw" size="10"><br/>
취미:<input type="checkbox" name="hobby" value="read">독서
<input type="checkbox" nam="hobbpy" value="cook">요리
<input type="checkbox" nam="hobbpy" value="run">조깅
<input type="checkbox" nam="hobbpy" value="swim">수영
<input type="checkbox" nam="hobbpy" value="sleep">취침<br/>
<input type="radio" name="major" value="kor">국어
<input type="radio" name="major" value="checked">영어
<input type="radio" name="major" value="mat">수학
<input type="radio" name="major" value="des">디자인<br/>
<select name="protocol">
<option value="http">http</option>
<option value="ftp" selected="selected">ftp</option>
<option value="smtp">smtp</option>
<option value="pop">pop</option>
</select><br/>
<input type="submit" value="전송">
<input type="reset" value="초기화">
</form>
</body>
</html>
cs


JspEx.jsp

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
<%@page import ="java.util.Arrays" %>
<%@ page language="java" contentType="text/html; charset=EUC-KR"
    pageEncoding="EUC-KR"%>
<!DOCTYPE html>
<html>
<head>
<meta http-equivb="Content-Type" content="text/html; charset=EUC-KR">
<title>Insert title here</title>
</head>
<body>
<%!
String name, id, pw, major, protocol;
String[] hobbys;
%>
<%
request.setCharacterEncoding("EUC-KR");
name = request.getParameter("name");
id = request.getParameter("id");
pw = request.getParameter("pw");
major = request.getParameter("major");
protocol = request.getParameter("protocol");
hobbys = request.getParameterValues("hobby");
%>
이름:<%= name %><br/>
id:<%=id %><br/>
pw:<%=pw %><br/>
major=<%=major %><br />
protocol=<%=protocol %><br />
</body>
</html>
cs



response 객체의 이해


  • 요청의 응답 정보를 가지고 있는 객체.
  • 메소드
    • getCharacterEncoding() : 응답할 때 문자의 인코딩 형태를 구함.
    • addCookie(Cokkie) : 쿠키 지정.
    • SendRedirect(URL) : 지정한 URL로 이동.
    • 아래 예제는 id가 admin일 경우 admin. 페이지로 이동하고 일반 유저일 경우 user 페이지로 이동하게 해놨다. 

form.html

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
<!DOCTYPE html>
<html>
<head>
<meta charset="EUC-KR">
<title>Insert title here</title>
</head>
<body>
<form action="JspEx.jsp">
id를 입력해주세요
<input type="text" name="id" size="10">
<input type="submit" value="전송">
</form>
</body>
</html>
 
cs


JspEx.jsp

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
<%@ page language="java" contentType="text/html; charset=EUC-KR"
    pageEncoding="EUC-KR"%>
<!DOCTYPE html>
<html>
<head>
<meta http-equivb="Content-Type" content="text/html; charset=EUC-KR">
<title>Insert title here</title>
</head>
<body>
<%
String str = request.getParameter("id");
if(str.equals("admin"))
{
       response.sendRedirect("admin.jsp?id="+str);     
}
else
{
       response.sendRedirect("user.jsp?id="+str);      
}
       
%>
</body>
</html>
cs


JSP 액션태그


  • JSP 파일에서 어떤 동작을 하도록 지시하는 태그.
  • forward, include, param 태그 등.. 이 있다.

forward 태그


  • 다른 페이지로 이동할 때 사용
  • 다른 페이지로 이동해도 이동한 페이지 url로 변경되지 않음.
  • <jsp:forward page="sub.jsp"/>
1
2
3
4
5
6
7
8
9
10
11
12
13
<%@ page language="java" contentType="text/html; charset=EUC-KR"
    pageEncoding="EUC-KR"%>
<!DOCTYPE html>
<html>
<head>
<meta http-equivb="Content-Type" content="text/html; charset=EUC-KR">
<title>Insert title here</title>
</head>
<body>
<h1>jspEx.jsp 페이지</h1>
<jsp:forward page="user.jsp"/>
</body>
</html>
cs


include 태그


  • 현재 페이지에 다른 페이지를 삽입함.
  • <jsp:include page="otherpagename.jsp"/>
1
2
3
4
5
6
7
8
9
10
11
12
13
<%@ page language="java" contentType="text/html; charset=EUC-KR"
    pageEncoding="EUC-KR"%>
<!DOCTYPE html>
<html>
<head>
<meta http-equivb="Content-Type" content="text/html; charset=EUC-KR">
<title>Insert title here</title>
</head>
<body>
<h1>jspEx.jsp 페이지</h1>
<jsp:include page="user.jsp"/>
</body>
</html>
cs


param 태그


  • 데이터를 전달하는 태그.
  • 이름과 값으로 이루어져 있다.
  • forward, include 태그와 같이 사용하면 페이지를 넘길 때 Param을 넘겨줄 수 있다.
  • <jsp:param name="id" value="id" />

JspEx.jsp

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
<%@ page language="java" contentType="text/html; charset=EUC-KR"
    pageEncoding="EUC-KR"%>
<!DOCTYPE html>
<html>
<head>
<meta http-equivb="Content-Type" content="text/html; charset=EUC-KR">
<title>Insert title here</title>
</head>
<body>
<jsp:forward page="user.jsp">
<jsp:param name="id" value="id" />
<jsp:param name="pw" value="!@#!@#" />
</jsp:forward>
</body>
</html
cs


user.jsp

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
<%@ page language="java" contentType="text/html; charset=EUC-KR"
    pageEncoding="EUC-KR"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="EUC-KR">
<title>Insert title here</title>
</head>
<body>
<%!
       String id, pw;
%>
<%
id = request.getParameter("id");
pw = request.getParameter("pw");
%>
유저정보
id : <%=id %>
pass : <%=pw%>
</body>
</html>
cs


반응형

'이전게시판 > JSP' 카테고리의 다른 글

JDBC로 OLACLE 접속  (0) 2018.10.22
JSP 쿠키, Session, 예외 페이지, 자바빈  (0) 2018.10.21
Servlet 값 초기화, 리스너  (0) 2018.10.20
Servlet Get,Post 동작  (0) 2018.10.19
Servlet 설정, 예제  (0) 2018.10.18
JSP 설치, 설정, 실행  (0) 2018.10.18
▲ top