JSP - getParameter() 와 getAttribute()의 차이점

우진·2023년 7월 12일

getParameter() 의 리턴 타입은 String 이다.
getAttribute() 의 리턴 타입은 Object 이다. 그래서 주로 빈 객체나 다른 클래스를 받아올 때 사용한다.

getParameter("test") 는 request의 name 값이 "test" 인 값을 가져온다.
getAttribute("test") 는 setAttribute("test", "1234") 라고 설정해주지 않으면 null 값을 리턴한다.

<body>
    <form action="test1.jsp" method="post">
        <input type="text" name="num"/>
        <input type="submit"/>
    </form>
</body>
<%-- num값으로 123을 넘겨받았을때 --%>
 
 
<%=request.getParameter("num") %><br/> <%-- 123 --%>
<%=request.getAttribute("num") %> <%-- null --%>
 
<hr/>
 
<% request.setAttribute("num", "1234"); %>
<%=request.getParameter("num") %><br/> <%-- 123 --%>
<%=request.getAttribute("num") %> <%-- 1234 --%>
profile
지니입니다

2개의 댓글

comment-user-thumbnail
2023년 8월 3일

퍼가요~♥

1개의 답글