我有一個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次?
你可以在[圖]添加所有這些值(http://download.oracle.com/javase/6/docs/api/java/util/Map.html )... – Augusto
你能解決你的代碼片段嗎? advX變量的類型是什麼? ServletContext沒有getServletContext(String)方法。 –
-1用於顯示無法編譯和誤導性的代碼片段,但未詳細說明功能要求,這使得發佈和/或提出正確答案非常困難。 – BalusC