Programming

(JSP) JSTL을 사용하여 HTML escape하기

steloflute 2021. 10. 8. 23:20

<, >, & 등 특수문자가 HTML 태그로 인식되지 않도록 처리. JSTL의 c:out을 사용한다.

 

# index.jsp:

<%@ page contentType="text/html; charset=UTF-8" %>

 

<!DOCTYPE html>

<html lang="ko">

    <head>

        <meta charset="utf-8">

        <meta name="viewport" content="width=device-width">

        <title>index</title>

    </head>

    <body>

        <form method="POST" action="check.jsp">

            <input name="name" placeholder="이름">

            <button type="submit">입장</button>

        </form>

    </body>

</html>

 

# check.jsp:

<%@ page contentType="text/html; charset=UTF-8" %>

<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>

 

<%

    request.setCharacterEncoding("utf-8");

    String name = request.getParameter("name");

%>

<!DOCTYPE html>

<html lang="ko">

    <head>

        <meta charset="utf-8">

        <meta name="viewport" content="width=device-width">

        <title>check</title>

    </head>

<body>

환영합니다. <c:out value="<%=name%>"/><br>

</body>

</html>