흐름제어
1. response.sendRedirect("주소명")
2.<jsp:forward page="경로명" / >
**************************************************************************************
FLOW : Forward
<index 파일 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>
<form action="flowForward.jsp">
<input type="text" name="name">
<input type="submit">
</form>
</body>
</html>
<flowForward.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>
<h1>forward</h1>
<jsp:forward page="result.jsp"/>
</body>
</html>
<result.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>
<%
String name = request.getParameter("name");
%>
<h1>parameter values of inedex : <%=name %></h1>
</body>
</html>
index.jsp -------> flowForward.jsp --------> result.jsp
index 페이지에서 입력받은 값에 의해서 result에 출력한다.
index에서 choa 입력하면
result로 가서
parameter values of inedex : choa
**************************************************************************************
FLOW : Redirect
위와 유사하게
index.jsp -------> flowRedirect.jsp ----------> result.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>
<form action="flowForward.jsp">
<input type="text" name="name">
<input type="submit">
</form>
</body>
</html>
리다이렉트
<%@ 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>
<%
response.sendRedirect("result.jsp");
%>
</body>
</html>
리절트
<%@ 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>
<%
String name = request.getParameter("name");
%>
<h1>parameter values of inedex : <%=name %></h1>
</body>
</html>
index에서 choa를 입력하면 결과
parameter values of inedex : null
forward와 redirect 차이
forward는 서버에서 인덱스->포워드->리절트로 이동
redirect는 서버에서 인덱스->리다이렉트->유저에게 스스로 리절트로 가도록 요청 -> 그래서 null
*웹에서 설문지 만들기 => request를 계속 forward 해준다. (아니면 session , request속성? )
**************************************************************************************
JSP 또는 HTML으르 부품(모듈화)하는 방법
1. <@ include file="경로명" >
2. <jsp:include page="경로명" />
(예시)
<%@ 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>
<%@ include file="./sub/head.jsp" %>
<form action="flowRedirect.jsp">
<input type="text" name="name">
<input type="submit">
</form>
<jsp:include page="./sub/foot.jsp" />
</body>
</html>
+head.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>
<h1>This is head page.</h1>
</body>
</html>
+foot.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>
<h2>This is foot page.</h2>
</body>
</html>
**************************************************************************************
web.xml파일
<?xml version="1.0" encoding="UTF-8"?>
<web-app xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" version="3.1">
<jsp-config>
<jsp-property-group>
<url-pattern>*.jsp</url-pattern>
<include-prelude>/sub/head.jsp</include-prelude>
<include-coda>/sub/foot.jsp</include-coda>
</jsp-property-group>
</jsp-config>
</web-app>
**************************************************************************************
액션태그를 사용하는 이유
Bean을 사용하기 위함
Bean : Java에서 사용하는 데이터의 묶음, 데이터 클래스라고 생각하면 된다.
**************************************************************************************
실습
useBean 사용하기
member_form.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>
<form action="process.jsp">
ID:<input type="text" name="id">
PW:<input type="password" name="pw">
AGE:<input type="text" name="age">
<input type="submit">
</form>
</body>
</html>
process.jsp 파일
<%@page import="test.Mem"%>
<%@ 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>
<%
String id = request.getParameter("id"); //form에서 받아온 데이터
String pw = request.getParameter("pw");
int age = Integer.parseInt(request.getParameter("age"));
Mem m = new Mem();
m.setId(id); //form에서 받은 값을 id변수에 저장후 m객체(멤버)에 저장
m.setPw(pw);
m.setAge(age);
m.setGender("f"); //gender는 받아온 값 없으므로 임의값 넣음
%>
ID : <%=m.getId() %>
PW : <%=m.getPw() %>
age : <%=m.getAge() %>
gender : <%=m.getGender() %>
</body>
</html>
process.jsp 파일 (useBean 사용)
<%@page import="test.Mem"%>
<%@ 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>
<%
String id = request.getParameter("id"); //form에서 받아온 데이터
String pw = request.getParameter("pw");
int age = Integer.parseInt(request.getParameter("age"));
// Mem m = new Mem();
// m.setId(id); //form에서 받은 값을 id변수에 저장후 m객체(멤버)에 저장
// m.setPw(pw);
// m.setAge(age);
// m.setGender("f"); //gender는 받아온 값 없으므로 임의값 넣음
%>
<!-- id속성은 참조 변수 m -->
<jsp:useBean id="m" class="test.Mem" />
<!-- bean의 멤버변수 이름 -->
<!-- name:참조 변수 이름 -->
<jsp:setProperty property="id" name="m" value="<%=id %>"/>
<jsp:setProperty property="pw" name="m" value="<%=pw %>"/>
<jsp:setProperty property="age" name="m" value="<%=age %>"/>
<jsp:setProperty property="gender" name="m" value="f"/>
ID : <%=m.getId() %>
PW : <%=m.getPw() %>
age : <%=m.getAge() %>
gender : <%=m.getGender() %>
</body>
</html>
자바클래스
Java Resource에 test 패키지에 Mem.java
package test;
public class Mem {
private String id ;
private String pw;
private int age;
private String gender;
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getPw() {
return pw;
}
public void setPw(String pw) {
this.pw = pw;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getGender() {
return gender;
}
public void setGender(String gender) {
this.gender = gender;
}
}
**************************************************************************************
연습2
member_form.jsp ---> actionTagProcess.jsp ---> resultAction.jsp
1. member_from.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>
<form action="actionTagProcess.jsp">
ID:<input type="text" name="id">
PW:<input type="password" name="pw">
AGE:<input type="text" name="age">
<input type="submit">
</form>
</body>
</html>
2. actionTagProcess.jsp
<%@page import="test.Mem"%>
<%@ 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>
<jsp:useBean id="m" class="test.Mem" scope="session" />
<jsp:setProperty property="*" name="m"/>
<jsp:setProperty property="gender" name="m" value="f"/>
<a href = "resultAction.jsp">action</a>
</body>
</html>
3. resultActio.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>
<jsp:useBean id="m" class="test.Mem" scope="session" />
ID : <jsp:getProperty property="id" name="m" />
PW : <jsp:getProperty property="pw" name="m"/>
AGE : <jsp:getProperty property="age" name="m"/>
Gen : <jsp:getProperty property="gender" name="m"/>
</body>
</html>
**************************************************************************************
개인 프로젝트에
DB연동 로그인, 로그아웃 구현함.
아파치톰캣으로 실습중인데 어떻게 웹서버에 올리고 DB는 어느 서버에 설치해야하는가? 고민
**************************************************************************************
'프로그래밍, 통계학 > JSP' 카테고리의 다른 글
[45일차]2015.09.09.수, 회원가입,로그인,회원탈퇴 (0) | 2015.09.09 |
---|---|
[44일차]2015.09.08.화. 결석 (0) | 2015.09.09 |
[42일차]2015.09.04.금요일, DB연동연습 (0) | 2015.09.04 |
[39,40,41일차] 2박3일 동원예비군훈련 (0) | 2015.09.04 |
[38일차]2015.08.31.월, servlet오류해결, Java 복습, Servlet 초기파라미터, JSP 시작 (0) | 2015.08.31 |