开发者

how to use string as reference variable in java?

开发者 https://www.devze.com 2023-03-29 23:48 出处:网络
i have a servlet class as shown below public class test extends HttpServlet { private adv1 adv1; private adv2 adv2;

i have a servlet class as shown below

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()]

   }

}

Now what i want is if i get a parameter value as adv2 then i should call a method in adv2, if parameter value is adv 49 i should call the method in adv 49.Is there any simple way to do this without using if(req.getParameter("type").equals("adv2")) {adv2.getText();} 50 times开发者_如何转开发?


What you have is a String, and you want to look up an Object that corresponds to it. Sounds like a Map to me:

// 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...
}

Depending on the fragility of the config, it would be a very good idea to refactor the init method implementation too. For example, define a static Set of strings containing adv1, adv2, ... and then use a foreach loop over this set to populate the map. If you're sure that the IDs will never change format, you could even use a simple loop from 1 to 50, looking up "adv" + i each time.


Your adv* variables should extend common superclass or implement common interface. Thus see the following code:

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
    }
}


You can use reflection. If you have methods adv1, adv2 etc into your servlet you can say:

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


Use a Map<String, TheTypeOfYourAdvVariables>. Your code snippet is not correct, so I suppose you meant

private String adv1;
private String adv2;

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

So, you could just use a Map<String, String> and populate it like this:

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);
}

Then you could do

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

Note that this explanation is useful because it explains how to use Maps. But in your case, all the strings are already stored in the servlet context, so you can in fact just do:

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

精彩评论

暂无评论...
验证码 换一张
取 消