2011-08-22 62 views
-1

我有一個servlet類如下圖所示如何在java中使用字符串作爲引用變量?

public class test extends HttpServlet { 

private adv1 adv1; 
private adv2 adv2; 
private adv3 adv3; 

... 

private adv50 adv50; 

// INITIALIZING REFERENCE VARIABLES 

init() {      
ServletContext context = getServletContext(); 
    adv1 = context.getServletContext("adv1"); 
    adv2 = context.getServletContext("adv2"); 
    adv3 = context.getServletContext("adv3"); 

    .... 

    adv50 = context.getServletContext("adv50"); 

} 

public void doGet(HttpServletRequest req,HttpServletResponse res) throws java.io.IOException { 
    String type = req.getParameter("type"); 

    // Now what i want is if i get a parameter value as adv2 then i should call a method in adv2 [eg.adv2.getText()], if parameter value is adv 49 i should call the method in adv 49.[eg adv49.getText()] 

    } 

} 

現在我想的是,如果我得到一個參數值ADV2那麼我應該叫在ADV2的方法,如果參數值是進階49我要調用的方法在adv 49.是否有任何簡單的方法來做到這一點,而不使用if(req.getParameter(「type」)。equals(「adv2」)){adv2.getText();} 50次?

+2

你可以在[圖]添加所有這些值(http://download.oracle.com/javase/6/docs/api/java/util/Map.html )... – Augusto

+2

你能解決你的代碼片段嗎? advX變量的類型是什麼? ServletContext沒有getServletContext(String)方法。 –

+0

-1用於顯示無法編譯和誤導性的代碼片段,但未詳細說明功能要求,這使得發佈和/或提出正確答案非常困難。 – BalusC

回答

2

你有什麼是String,你想查找與其對應的Object。聽起來像一個地圖對我說:

// I'm assuming that this interface (or something like it) exists: 
public interface Textual { 
    String getText(); 
} 

// Then in the servlet... 
private Map<String, Textual> advs = new HashMap<String, Textual>(50); 

public init() { 
    advs.put("adv1", context.getServletContext("adv1")); 
    advs.put("adv2", context.getServletContext("adv2")); 
    ... 
} 

public void doGet(HttpServletRequest req,HttpServletResponse res) { 
    String type = req.getParameter("type"); 
    Textual adv = advs.get(type); 
    adv.getText(); // do something with this of course... 
} 

根據配置的脆弱性,這將是重構init方法實現過一個非常好的主意。例如,定義包含adv1adv2,...的字符串的靜態Set,然後在該集合上使用foreach循環來填充地圖。如果你確定ID不會改變格式,你甚至可以使用從1到50的簡單循環,每次查找"adv" + i

0

您可以使用反射。如果你有方法ADV1,ADV2等進入你的servlet,你可以說:

getClass().getMethod(name).invoke(this);

0

使用Map<String, TheTypeOfYourAdvVariables>。您的代碼段是不正確的,所以我想你的意思

private String adv1; 
private String adv2; 

public void init() {      
    ServletContext context = getServletContext(); 
    adv1 = context.getInitParameter("adv1"); 
    adv2 = context.getInitParameter("adv2"); 
} 

所以,你可以只使用一個Map<String, String>和填充像這樣:

private Map<String, String> advs = new HashMap<String, String>(); 

public void init() {      
    ServletContext context = getServletContext(); 
    adv1 = context.getInitParameter("adv1"); 
    advs.put("adv1", adv1); 
    adv2 = context.getInitParameter("adv2"); 
    advs.put("adv2", adv2); 
} 

那麼你可以做

String theAdvType = request.getParameter("type"); 
String theAdvToUse = advs.get(theAdvType); 

請注意,這個解釋很有用,因爲它解釋瞭如何使用地圖。但是,在你的情況下,所有的字符串都已經存儲在servlet上下文,這樣你就可以在事實上只是做:

String theAdvType = request.getParameter("type"); 
String theAdvToUse = servletContext.getInitParameter(theAdvType); 
1

你進階*變量應擴大公共超或實現共同的接口。 因此看到下面的代碼:

protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException 
{ 
    ServletContext context = getServletContext(); 
    String type = request.getParameter("type"); 
    Adv adv = (Adv)context.getAttribute(type); 
    if(adv != null){ 
     ... 
     // do something with your adv 
    }else{ 
     ... 
     // do something else 
    } 
} 
相關問題