[英]how to use string as reference variable in java?
我有一个servlet类,如下所示
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中调用一个方法,如果参数值是adv 49我应该在adv 49中调用该方法。是否有任何简单的方法可以做到而无需使用if(req .getParameter(“ type”)。equals(“ adv2”)){adv2.getText();} 50次?
您拥有的是一个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
方法的实现也是一个很好的主意。 例如,定义静态Set
包含字符串adv1
, adv2
,...然后使用foreach循环对这个集合来填充地图。 如果您确定ID不会更改格式,则甚至可以使用1到50的简单循环,每次都查找"adv" + i
。
您的adv *变量应扩展公共超类或实现公共接口。 因此请参见以下代码:
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
}
}
您可以使用反射。 如果您的servlet中有adv1,adv2等方法,您可以说:
getClass().getMethod(name).invoke(this);
使用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);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.