wrkbrs

[JSP] Parameter(파라미터) 와 attribute(속성)의 차이 본문

JSP

[JSP] Parameter(파라미터) 와 attribute(속성)의 차이

zcarc 2018. 10. 31. 21:58

1. 간단히 요약


파라미터에는 String만 사용가능

애트리뷰트에는 다른 String외에 Object, Array 등 다양한 데이터 입력이 가능

파라미터는 request에만 저장이 가능하고, 애트리뷰트는 session, context 등에도 저장이 가능

애트리뷰트가 좀더 유연함

파라미터는 HTML의 form 데이터 전송시 key/value 쌍으로 사용된다.



2. 자세한 비교


■  속성이란?

  - ServletContext, HttpServletRequest, HttpServletResponse, HttpSession 객체 중 하나에 설정해 놓는 객체(Object)이다.

 

■  속성과 파라미터의 차이점

 

 

속성 

 파라미터 

 타입

 Application / Context

 Request

 Session 

 Application / context 초기화 파라미터

 Request 파라미터

 Servlet 초기화 파라미터

 설정 메소드

 setAttribute(String name, Object value)

 애플리케이션과 서블릿의 초기화 파라미터 값은 런타임 시 설정할 수 없습니다. 오로지 DD(Web.xml)에서만 가능합니다. 기억나죠? (Request 파라미터를 가지고, 좀 어렵긴 하지만 쿼리 스트링을 설정할 수 있습니다. 

 리턴 타입

 Object

 String

 참조 메소드

 getAttribute(String name)

 getInitParameter(String name) 

 

 

■  request

   -  요청시마다 만들어지는 객체

   -  attribute, parameter를 가지고 있음

 

■  application

   -  톰캣구동시 webcontext별로 만들어지는 객체

   -  톰캣중지시 없어지는 객체 LifeScope가 가장 길다

   -  서블릿 API ver 정보, 실제 경로

   -  parameter, attribute 가지고 있음

 

■  pageContext

   -  현재 실행중인 JSP의 정보를 담고있다. 

   -  가장 일찍 객체가 사라진다 LifeScope가 가장 짧다

   -  JAVA SE : Object, Class, File

   -  JAVA EE : ServletContext, PageContext, EJBContext

   -  attribute 가지고 있음

   -  out / request / response / session 정보 얻는게 가능

      ex) pageContext.getOut();

            pageContext.getRequest();

            pageContext.getSession();

 

■  session

   -  클라이언트별로 만들어지는 객체

   -  클라이언트 단에서 새로운 요청이 있어도 Session객체에 속성들은 그대로 유지된다.

   -  attribute가 있음

 

■  클라이언트가 useBean.jsp?one=1&two=2 를 요청한다면 요청할 때마다 HttpServletRequest와 HttpServletResponse 객체가 생성

     다. 이 안에는 parameter와 attribute가 있는데 one=1과 two=2가 parameter안에 저장된다 (key = value 형식으로)

 

■  속성(Attribute)은 setAttribute() 메소드나  <jsp:setProperty name="userSession"   property="id"  value="session아이디" />로 설정

    이 가능하지만 파라미터는 아까 위에서처럼 url에 값을 전달해주는 방법밖에 없다. (코드단에서는 설정 못함)

 

■  그 후 jsp파일에 객체를 만들어낸다. useBean.jsp라면 useBean_jsp객체 생성된다. 그리고 _jspService() 호출한다

     _jspService() 메소드에선 내장객체들을 생성하게 된다.

 

■  <jsp:useBean id="userSession" class="vo.User" scope="session" /> 라고 하게 되면

     HttpSession 객체 안에 attribute로 아이디가 userSession인 attribute가 만들어진다.

 

■  같은 클라이언트에서 다른 페이지를 요청하게 될 때 Request, Response 객체는 새로 생성하게 되지만

    Application, Session 객체는 원래 생성되어있던 것을 사용하게 된다.

 

■  다른 클라이언트가 Session에 대한 접근은 보지 못하더라도 Application에 대한 정보는 볼 수가 있다.

 

■  Session에 활용 예 : 장바구니, 익스플로러10 버전 로그인 후 새탭열시에도 로그인상태 유지


3. 부가적으로


위 비교내용이 이해가 되었다면 이하 게시물을 이해하는데 큰 어려움은 없을것이다. ( 같은 내용)


What is the difference between the request attribute and request parameter?

Request parameters are the result of submitting an HTTP request with a query string that specifies the name/value pairs, or of submitting an HTML form that specifies the name/value pairs. The name and the values are always strings. For example, when you do a post from html, data can be automatically retrieved by using request.getParameter(). Parameters are Strings, and generally can be retrieved, but not set.

Let's take a real example, you have one html page named RegisterForm.html and JSP page named Register.jsp. The RegisterForm.html contains a form with few parameters for user registration on web application and those parameters you want to store in database.

<html>
<body>
<form name="regform" method="post" action="../Register.jsp">
<table ID="Table1">
<tr>
<td>
First Name : <input type="text" name="FIRSTNAME" size="25" value="">
</td>
</tr>
<tr>
<td>
<input type="Submit" NAME="Submit" value="Submit" >
</td>
</tr>
</table>
</form>
</body>
</html>



When user will enter first name in the text filed and press submit button, it will callRegister.jsp page. Now you want the value entered in text field by user in jsp/servlet so there you use request.getParameter() method.

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

On the server side, the request.getParameter() will retrieve a value that the client has submitted in the First Name text field. Using this method you can retrive only one value. This method i.e. getParameter method is in ServletRequest interface which is part of javax.servlet package.

Request attributes (more correctly called "request-scoped variables") are objects of any type that are explicitly placed on the request object via a call to the setAttribute() method. They are retrieved in Java code via the getAttribute() method and in JSP pages with Expression Language references. Always use request.getAttribute() to get an object added to the request scope on the serverside i.e. using request.setAttribute().

Attributes are objects, and can be placed in the request, session, or context objects. Because they can be any object, not just a String, they are much more flexible. You can also set attributes programaticly and retrieve them later. This is very useful in the MVC pattern. For example, you want to take values from database in one jsp/servlet and display them in another jsp. Now you have resultset filled with data ready in servlet then you use setAttributemethod and send this resultset to another jsp where it can be extracted by using getAttributemethod.

Once a servlet gets a request, it can add additional attributes, then forward the request off to another servlet for processing. Attributes allow servlets to communicate with one another.


출처: 

http://blog.naver.com/susieredrum/150140506990

http://www.xyzws.com/Servletfaq/what-is-the-difference-between-the-request-attribute-and-request-parameter/1



출처: http://yooooooo7se.tistory.com/63 [Learning Machine]