I am working on a servlet that retrieves values from properties files. I need to pass these values to my JavaScript using AJAX and set them in a textbox. As I am new to Ajax, I would appreciate if someone could help me by analyzing the code.
SERVLET
@WebServlet("/PopulateProperties")
public class PopulateProperties extends HttpServlet {
private static final long serialVersionUID = 1L;
Properties prop = new Properties();
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
String serverName=request.getParameter("servernameDD");
List<Map<String,String>> returnObj = new ArrayList<Map<String,String>>();
String[] servers = getSearchAttr("servers");
if (servers != null && servers.length > 0) {
String[] lookfor = getSearchAttr("servers_lookfor");
for (String server : servers) {
Map<String,String> obj = new HashMap<String,String>();
if (lookfor != null) {
for (String look : lookfor) {
//System.out.println("looking for :"+look);
String value = prop.getProperty(server+"_"+look);
if(server.equalsIgnoreCase(serverName)){
if(look.equalsIgnoreCase("Links")){
String getlook=prop.getProperty(look);
String getlink=prop.getProperty(server+'_'+look,"Links");
System.out.println("Hello" +getlink);
System.out.println(getlook);
request.setAttribute("serverLink", getlink);
}
}
if (value != null) {
obj.put(look, value);
}
request.setAttribute("servers", server);
request.setAttribute("lookfor", look);
}
}
//System.out.println("Object :"+obj);
returnObj.add(obj);
}
}
response.sendRedirect("updateserverstatus.html");
}
private String[] getSearchAttr( String property){
String lookfor = prop.getProperty(property,"");
String[] ret = {lookfor};
if (lookfor.contains(",")) {
return lookfor.split(",");
} else {
//System.out.println("comma not present in "+property);
//System.out.println("webservice :"+lookfor);
return ret;
}
}
I have attempted the following Ajax request, but I keep encountering an "undefined error" for serverLink variable.
function OnSelectionChange(){
$.ajax({
type: "POST",
url: "/PopulateProperties",
data: "{Links: " + serverLink + "}",
success: function(result){
alert(result.d);
console.log(result);
}
});
}