Action Tag : 인코딩, 디코딩

조수경·2022년 1월 5일
0

JSP

목록 보기
7/45

param01.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@page import="java.net.URLEncoder"%>
<!DOCTYPE html>
<html>
<head>

<title>Action Tag</title>
</head>
<body>
    <h3>param 액션 태그</h3>
    <!-- ...01_data.jsp?id=admin&name=관리자 -->
    <jsp:forward page="/ch04/param01_data.jsp">
       <jsp:param name="id" value="admin" />
       <jsp:param name="name" value='<%=URLEncoder.encode("관리자")%>' />
    </jsp:forward>
    <p>Java Servlet Page</p>
</body>
</html>

param01_data.jsp

<%@page import="java.net.URLDecoder"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<!DOCTYPE html>
<html>
<head>

<title>Action Tag</title>
</head>
<body>
  <p>아이디 : <%=request.getParameter("id") %></p>
  <%
  
  String name = request.getParameter("name");
  out.print("인코딩된 관리자 : " + name);
  
  %>
  <p>이름 : <%=URLDecoder.decode(name)%></p>
</body>
</html>

01에서 인코딩하고 보내서 깨져서 나옴! _data에서 디코딩 해서 관리자 라고 안깨져 나온것!

param02.jsp

<%@page import="java.net.URLEncoder"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@page import="java.util.Calendar"%>
<!DOCTYPE html>
<html>
<head>

<title>Action Tag</title>
</head>
<body>
    <h3>param 액션 태그</h3>
    <jsp:include page="param02_data.jsp">
        <jsp:param name="title" value='<%=URLEncoder.encode("오늘의 날짜와 시각") %>' />
        <jsp:param name="date" value="<%=Calendar.getInstance().getTime()%>" />
    </jsp:include>
</body>
</html>

param02_data.jsp

<%@page import="java.util.Enumeration"%>
<%@page import="java.net.URLDecoder"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<!DOCTYPE html>
<html>
<head>

<title>Insert title here</title>
</head>
<body>
<!-- request객체 내에 title=오늘의 날짜와 시각&date=2021 -->

<%
 String title = request.getParameter("title");
%>

<h3><%=URLDecoder.decode(title)%></h3>
Today is : <%=request.getParameter("date") %>
<br />
<%
  Enumeration<String> attrEnum = request.getAttributeNames();
  while(attrEnum.hasMoreElements()){
	  String name = attrEnum.nextElement();
	  Object value = request.getAttribute(name);
	  
	  out.print("<b>"+name+"</b>="+value+"<br />");
  }
%>

</body>
</html>

profile
신입 개발자 입니다!!!

0개의 댓글