JSP

1. main.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
<script src="http://code.jquery.com/jquery-3.2.1.min.js"></script>

</head>
<body>

<%
 RequestDispatcher rd=
  request.getRequestDispatcher("template.jsp?CONTENTPAGE=content.jsp");
 rd.forward(request, response);
%>

<jsp:forward page="template.jsp">
  <jsp:param value="content.jsp" name="CONTENTPAGE" />
</jsp:forward>



</body>
</html>

 

 

2. includeParamTest.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
<script src="http://code.jquery.com/jquery-3.2.1.min.js"></script>

</head>
<body>

<!-- includeParamTest.jsp -->

<%
 request.setCharacterEncoding("utf-8");	
 String name="김철수";
 String pageName="includedParamTest.jsp";

%>

포함하는 페이지 입니다.

<jsp:include page="<%= pageName %>" >
	<jsp:param value="<%= name %>" name="name" />
	<jsp:param value="<%= pageName %>" name="pageName" />
</jsp:include>


</body>
</html>

 

 

 

includedParamTest.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
<script src="http://code.jquery.com/jquery-3.2.1.min.js"></script>

</head>
<body>


<!-- includedParamTest.jsp -->
<%-- request.getParameter("변수명") => ${param.변수명 } --%>

<h2>포함되는 페이지 : ${param.pageName }</h2>
<b>${param.name }</b>님 환영합니다.


</body>
</html>

 

 

=>출력

포함하는 페이지 입니다.

포함되는 페이지 : includedParamTest.jsp

김철수님 환영합니다.

 

 

 

 

3. forwardForm.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
<script src="http://code.jquery.com/jquery-3.2.1.min.js"></script>

</head>
<body>

<!-- forwardFrom.jsp -->

<%

//pageContext < request <session <application
//request 저장 영역에 변수 저장
request.setAttribute("id", "kim");
request.setAttribute("name", "김철수");

%>
<!-- 
forward : url 은 고정, 화면 이동+ 데이터 전달
redirect : url 변경

 -->

<jsp:forward page="forwardTo.jsp?age=33"></jsp:forward>

</body>
</html>

 

 

forwardTo.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
<script src="http://code.jquery.com/jquery-3.2.1.min.js"></script>

</head>
<body>

<!-- forwardTo.jsp -->
<%-- request.getAttribute("변수명") => ${변수명 } --%>
<%-- request.getParameter("변수명") =>${param.변수명 } --%>
  
id : ${id }<br>

name : ${name} <br>

age :${param.age }



</body>
</html>

 

출력 =>

 

id : kim
name : 김철수 
age :33

 

 

 

 

 

 4. fowardParamForm.jsp

 

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
<script src="http://code.jquery.com/jquery-3.2.1.min.js"></script>

</head>
<body>

<!-- fowardParamForm.jsp -->

<form method="post" 
 action="<%= request.getContextPath() %>/ch05_servlet/forward.do">

이름 : <input name="name"><br>
색상 :
<select name="color">
 <option value="blue">파랑</option>
 <option value="green">초록</option>
 <option value="red">빨강</option>
</select>

<br>
<input type="submit" value="확인">
</form>




</body>
</html>

 

 

class ForwardController

package ch05;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;


@WebServlet("/ch05_servlet/forward.do")
public class ForwardController extends HttpServlet {
	private static final long serialVersionUID = 1L;
 
 
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

		//자바의 기본인코딩이 iso -8859-1(서유럽언어)
		//폼의 입력값
		request.setCharacterEncoding("utf-8");
		String name=request.getParameter("name");
		String color =request.getParameter("color");
		System.out.println("이름:" + name);
		System.out.println("색상 : " + color);
		String message = "";
		switch(color){
		
		   case "blue":
			   message ="자기탐구와 내적성장을 상징";
			   break;
			   
		   case "green":
			   message ="기분의 안정과 온화함을 상징";
			   break;
			   
		   case "red":
			   message ="생명을 상징";
			   break;
		}
		
		message +=" 하는 색입니다.";
//		request.setAttribute("name", name);
//		request.setAttribute("color", color);
//		request.setAttribute("message", message);
		
		Map<String, Object> map =new HashMap<>();
		map.put("name", name);
		map.put("color", color);
		map.put("message", message);
		request.setAttribute("map", map);//맵을 저장
		String page="/ch05_2/color.jsp";
		RequestDispatcher rd=
				request.getRequestDispatcher(page);
		
		rd.forward(request, response);//포워딩
	
	}
	
	
	
	

	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

		doGet(request, response);
	}

}

 

color.jsp

<%@page import="java.util.Map"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
<script src="http://code.jquery.com/jquery-3.2.1.min.js"></script>

</head>
<body>

<!-- color.jsp -->
<% 
// request.getAttribute("변수명"); 

Map<String, String> map
=(Map<String, String>)request.getAttribute("map");
String name=map.get("name");//맵에 저장된 값을 조회
String color=map.get("color");
String message=map.get("message");

%>

<%= name %> 님이 좋아하는 색상은

<span style="color:<%=color %>" >
<%= color %>
</span>
이고
<%= message %>

</body>
</html>

 

출력 => 

이름 : 
색상 :  

 

 

 

 

홍길동 님이 좋아하는 색상은 blue 이고 자기탐구와 내적성장을 상징 하는 색입니다.

 

 

 

 

 

 

 

 

 

 

about author

PHRASE

Level 60  머나먼나라

우리의 육체에서 가장 천한 부분은 성기이다. 또한 인간이 스스로를 신이라 생각하는 데 가장 장애가 되는 것도 바로 이 성기이다. -니체

댓글 ( 4)

댓글 남기기

작성