2013-05-19 1 views
0

다른 클래스에서 전달 된 각 "서비스"매개 변수에 대한 쿼리 결과를 HashMap에 저장하고 application.setAttribute()에 의해 응용 프로그램 범위에 저장하려는 경우 응용 프로그램 범위에 서비스가있는 경우 해당 값을 얻을 수 있습니다. application.getAttribute(). 각 서비스마다 3 개의 열을 저장할 수 있습니다. 응용 프로그램 범위에 존재하는 경우 서비스 매개 변수에 따라 해당 3 열 값을 어떻게 얻을 수 있습니까? 응용 프로그램 범위에 값을 저장하는 방법을 제안하십시오 (다음 코드에서 잘못했을 경우).데이터베이스 쿼리에 대한 JSP 응용 프로그램 범위 설정

service=request.getParameter("service"); 
Map<String,String> FreetextMap=new HashMap<String,String>(); 

if(application.getAttribute(service)==null) 
{ 
query = "SELECT aUnique,aTitle,aBody FROM FreeTexts WHERE service='" +service+ "'  
rs = DBUtils.getRs(con,query); 
if (rs.next()) { 
clientFreetextMap.put("unique", rs.getString("aUnique")); 
clientFreetextMap.put("body",rs.getString("aBody")); 
clientFreetextMap.put("txt",rs.getString("aTitle")); 
application.setAttribute(service,clientFreetextMap); 

} 

다음 코드는 응용 프로그램 범위에서 값을 가져 오는 역할을 담당하지만 서비스를 선언 할 항목이 누락되었습니다. 나는 어디서 서비스를 쓸지 몰라요 ?? 서비스 매개 변수와 관련하여 값을 얻는 방법 ??

else 
    { 
    txt=(String) application.getAttribute(clientFreetextMap.get("txt")); 
    uniqueid=(String) application.getAttribute(clientFreetextMap.get("unique")); 
    adsbody=(String) application.getAttribute(clientFreetextMap.get("body")); 
    } 
+0

이 코드는 [SQL injection] (http://en.wikipedia.org/wiki/SQL_injection) 취약점이 있습니다. 문자열을 연결하여 쿼리를 작성하지 마십시오. 대신에'PreparedStatement'를 사용하십시오. –

답변

1

사용자는 if 블록에서 clientFreetextMap 자체를 기억한다. else 블록에 같은 Map을 넣은 다음 필요한 데이터를 가져와야합니다.

clientFreetextMap = (Map)application.getAttribute(service); 
txt = clientFreetextMap.get("txt"); 
uniqueid = clientFreetextMap.get("unique"); 
adsbody = clientFreetextMap.get("body"); 
+0

@Joe .. 와우 !! 큰!! 그것은 일했다!! 감사합니다 백만 !! –

+0

@ 조 .. 그래서, 응용 프로그램 범위로 설정 한 후 다음에 데이터베이스 쿼리를하지 않습니까? 그래서 나는 그것이 성능을 향상 시키길 바래! –

관련 문제