How can i call a parametrized method of java class into a servlet doget method












0















I have a java class where i have a method which is returning me a json i want to call that method into my servlet doGet method so that i can make a AJAX call later



but while calling the java class method (Outlet.Outlet) it asks for a parameter to pass i dont know what to pass there
please have a look into my code



this is my java class



public class Outlet {
static Connection con = null;
static Statement statement = null;
ResultSet resultSet = null;

public static String Outlet(String idDB) throws ClassNotFoundException, SQLException {
List<String> list = new ArrayList<String>();
con = DBConnection.createConnection();
statement = con.createStatement();

String sql="select CUSTOMERDESCRIPTOR as OUTLETNAME from ecustomer where CUSTOMERIDENTIFIER in(select CUSTOMERIDENTIFIER from mt_distributrol where mt_distributr_vcdistributrcode = '"+idDB+"')";

System.out.println("iddb :"+idDB);
try {

ResultSet resultSet = statement.executeQuery(sql);
while (resultSet.next()) {
list.add(resultSet.getString("OUTLETNAME"));

}

} catch (SQLException e) {
e.printStackTrace();
}
String json = new Gson().toJson(list);
System.out.println("Json Outlet :"+json);
return json;
}


}



In the above java class i am returning a Json and i want to call that method into my servlet doGost



my doGet is



    try {
String json = Outlet.Outlet(); //what should i pass here as a parameter
response.setContentType("application/json");
response.getWriter().write(json);
System.out.println("dheeraj"+json);
}
catch (Exception e) {

e.printStackTrace();
}

}


if i am passing idDB then it throws error.please anybody having any knowledge help me out










share|improve this question




















  • 1





    And what is the error?

    – cricket_007
    Nov 20 '18 at 5:56






  • 1





    By the way, you might want to research "SQL Injection" before writing more SQL queries like that

    – cricket_007
    Nov 20 '18 at 5:57











  • check you DB for a valid mt_distributr_vcdistributrcode

    – Scary Wombat
    Nov 20 '18 at 5:57











  • @cricket_007 in servlet when i am calling Outlet.Outlet() it says to pass a parameter what parameter should i pass? and what is SQL INJECTION ?

    – dheeraj kumar
    Nov 20 '18 at 5:59











  • @ScaryWombat what ?? i didn't get you

    – dheeraj kumar
    Nov 20 '18 at 6:00
















0















I have a java class where i have a method which is returning me a json i want to call that method into my servlet doGet method so that i can make a AJAX call later



but while calling the java class method (Outlet.Outlet) it asks for a parameter to pass i dont know what to pass there
please have a look into my code



this is my java class



public class Outlet {
static Connection con = null;
static Statement statement = null;
ResultSet resultSet = null;

public static String Outlet(String idDB) throws ClassNotFoundException, SQLException {
List<String> list = new ArrayList<String>();
con = DBConnection.createConnection();
statement = con.createStatement();

String sql="select CUSTOMERDESCRIPTOR as OUTLETNAME from ecustomer where CUSTOMERIDENTIFIER in(select CUSTOMERIDENTIFIER from mt_distributrol where mt_distributr_vcdistributrcode = '"+idDB+"')";

System.out.println("iddb :"+idDB);
try {

ResultSet resultSet = statement.executeQuery(sql);
while (resultSet.next()) {
list.add(resultSet.getString("OUTLETNAME"));

}

} catch (SQLException e) {
e.printStackTrace();
}
String json = new Gson().toJson(list);
System.out.println("Json Outlet :"+json);
return json;
}


}



In the above java class i am returning a Json and i want to call that method into my servlet doGost



my doGet is



    try {
String json = Outlet.Outlet(); //what should i pass here as a parameter
response.setContentType("application/json");
response.getWriter().write(json);
System.out.println("dheeraj"+json);
}
catch (Exception e) {

e.printStackTrace();
}

}


if i am passing idDB then it throws error.please anybody having any knowledge help me out










share|improve this question




















  • 1





    And what is the error?

    – cricket_007
    Nov 20 '18 at 5:56






  • 1





    By the way, you might want to research "SQL Injection" before writing more SQL queries like that

    – cricket_007
    Nov 20 '18 at 5:57











  • check you DB for a valid mt_distributr_vcdistributrcode

    – Scary Wombat
    Nov 20 '18 at 5:57











  • @cricket_007 in servlet when i am calling Outlet.Outlet() it says to pass a parameter what parameter should i pass? and what is SQL INJECTION ?

    – dheeraj kumar
    Nov 20 '18 at 5:59











  • @ScaryWombat what ?? i didn't get you

    – dheeraj kumar
    Nov 20 '18 at 6:00














0












0








0








I have a java class where i have a method which is returning me a json i want to call that method into my servlet doGet method so that i can make a AJAX call later



but while calling the java class method (Outlet.Outlet) it asks for a parameter to pass i dont know what to pass there
please have a look into my code



this is my java class



public class Outlet {
static Connection con = null;
static Statement statement = null;
ResultSet resultSet = null;

public static String Outlet(String idDB) throws ClassNotFoundException, SQLException {
List<String> list = new ArrayList<String>();
con = DBConnection.createConnection();
statement = con.createStatement();

String sql="select CUSTOMERDESCRIPTOR as OUTLETNAME from ecustomer where CUSTOMERIDENTIFIER in(select CUSTOMERIDENTIFIER from mt_distributrol where mt_distributr_vcdistributrcode = '"+idDB+"')";

System.out.println("iddb :"+idDB);
try {

ResultSet resultSet = statement.executeQuery(sql);
while (resultSet.next()) {
list.add(resultSet.getString("OUTLETNAME"));

}

} catch (SQLException e) {
e.printStackTrace();
}
String json = new Gson().toJson(list);
System.out.println("Json Outlet :"+json);
return json;
}


}



In the above java class i am returning a Json and i want to call that method into my servlet doGost



my doGet is



    try {
String json = Outlet.Outlet(); //what should i pass here as a parameter
response.setContentType("application/json");
response.getWriter().write(json);
System.out.println("dheeraj"+json);
}
catch (Exception e) {

e.printStackTrace();
}

}


if i am passing idDB then it throws error.please anybody having any knowledge help me out










share|improve this question
















I have a java class where i have a method which is returning me a json i want to call that method into my servlet doGet method so that i can make a AJAX call later



but while calling the java class method (Outlet.Outlet) it asks for a parameter to pass i dont know what to pass there
please have a look into my code



this is my java class



public class Outlet {
static Connection con = null;
static Statement statement = null;
ResultSet resultSet = null;

public static String Outlet(String idDB) throws ClassNotFoundException, SQLException {
List<String> list = new ArrayList<String>();
con = DBConnection.createConnection();
statement = con.createStatement();

String sql="select CUSTOMERDESCRIPTOR as OUTLETNAME from ecustomer where CUSTOMERIDENTIFIER in(select CUSTOMERIDENTIFIER from mt_distributrol where mt_distributr_vcdistributrcode = '"+idDB+"')";

System.out.println("iddb :"+idDB);
try {

ResultSet resultSet = statement.executeQuery(sql);
while (resultSet.next()) {
list.add(resultSet.getString("OUTLETNAME"));

}

} catch (SQLException e) {
e.printStackTrace();
}
String json = new Gson().toJson(list);
System.out.println("Json Outlet :"+json);
return json;
}


}



In the above java class i am returning a Json and i want to call that method into my servlet doGost



my doGet is



    try {
String json = Outlet.Outlet(); //what should i pass here as a parameter
response.setContentType("application/json");
response.getWriter().write(json);
System.out.println("dheeraj"+json);
}
catch (Exception e) {

e.printStackTrace();
}

}


if i am passing idDB then it throws error.please anybody having any knowledge help me out







java sql servlets jdbc methods






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 20 '18 at 6:12









cricket_007

80.4k1142110




80.4k1142110










asked Nov 20 '18 at 5:53









dheeraj kumardheeraj kumar

40211




40211








  • 1





    And what is the error?

    – cricket_007
    Nov 20 '18 at 5:56






  • 1





    By the way, you might want to research "SQL Injection" before writing more SQL queries like that

    – cricket_007
    Nov 20 '18 at 5:57











  • check you DB for a valid mt_distributr_vcdistributrcode

    – Scary Wombat
    Nov 20 '18 at 5:57











  • @cricket_007 in servlet when i am calling Outlet.Outlet() it says to pass a parameter what parameter should i pass? and what is SQL INJECTION ?

    – dheeraj kumar
    Nov 20 '18 at 5:59











  • @ScaryWombat what ?? i didn't get you

    – dheeraj kumar
    Nov 20 '18 at 6:00














  • 1





    And what is the error?

    – cricket_007
    Nov 20 '18 at 5:56






  • 1





    By the way, you might want to research "SQL Injection" before writing more SQL queries like that

    – cricket_007
    Nov 20 '18 at 5:57











  • check you DB for a valid mt_distributr_vcdistributrcode

    – Scary Wombat
    Nov 20 '18 at 5:57











  • @cricket_007 in servlet when i am calling Outlet.Outlet() it says to pass a parameter what parameter should i pass? and what is SQL INJECTION ?

    – dheeraj kumar
    Nov 20 '18 at 5:59











  • @ScaryWombat what ?? i didn't get you

    – dheeraj kumar
    Nov 20 '18 at 6:00








1




1





And what is the error?

– cricket_007
Nov 20 '18 at 5:56





And what is the error?

– cricket_007
Nov 20 '18 at 5:56




1




1





By the way, you might want to research "SQL Injection" before writing more SQL queries like that

– cricket_007
Nov 20 '18 at 5:57





By the way, you might want to research "SQL Injection" before writing more SQL queries like that

– cricket_007
Nov 20 '18 at 5:57













check you DB for a valid mt_distributr_vcdistributrcode

– Scary Wombat
Nov 20 '18 at 5:57





check you DB for a valid mt_distributr_vcdistributrcode

– Scary Wombat
Nov 20 '18 at 5:57













@cricket_007 in servlet when i am calling Outlet.Outlet() it says to pass a parameter what parameter should i pass? and what is SQL INJECTION ?

– dheeraj kumar
Nov 20 '18 at 5:59





@cricket_007 in servlet when i am calling Outlet.Outlet() it says to pass a parameter what parameter should i pass? and what is SQL INJECTION ?

– dheeraj kumar
Nov 20 '18 at 5:59













@ScaryWombat what ?? i didn't get you

– dheeraj kumar
Nov 20 '18 at 6:00





@ScaryWombat what ?? i didn't get you

– dheeraj kumar
Nov 20 '18 at 6:00












1 Answer
1






active

oldest

votes


















1














Please read OWASP - SQL Injection and learn about PreparedStatements



First, methods should not start with capital letter, so rather you could name it like Outlet.findById rather than Outlet.Outlet (the method should not be the same as the class; it is really confusing to read), and you can get parameters from the request



@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String id = request.getParameter("id");
String s = Outlet.findById(id);


When calling the API, you add ?id=value



Or you can get the final part of the path from request, assuming your API is setup like /path/ids/value - Refer What's the difference between getRequestURI and getPathInfo methods in HttpServletRequest? for options with this



Before doing this, of course you should double-check that query you are running actually returns data when querying the database directly.






share|improve this answer


























  • hey from where you are getting this "id" ?

    – dheeraj kumar
    Nov 20 '18 at 6:05











  • http://your.api.com/someAPI?id=x will pass x into your query

    – cricket_007
    Nov 20 '18 at 6:07













  • on my ui i don't have any input feild for idDB.. its is comming from loging query like there is a form form loging in on mu ui when ever a user looged in i run a query which checks username and pasword and also i get that value idDB from that query now i am passing that value into the java class methot which is Outlet and in that method i am using idDB in a query

    – dheeraj kumar
    Nov 20 '18 at 6:08











  • i didn't get you :(

    – dheeraj kumar
    Nov 20 '18 at 6:10











  • okay... I don't know your API, or your app's logic... The answer is still the same. You need to form that URL so that you can actually get an ID. Either that is part of the path, http/your.api.com/someAPI/ids/x to get that ID, or you can put ?id=x at the end

    – cricket_007
    Nov 20 '18 at 6:10











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53387016%2fhow-can-i-call-a-parametrized-method-of-java-class-into-a-servlet-doget-method%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









1














Please read OWASP - SQL Injection and learn about PreparedStatements



First, methods should not start with capital letter, so rather you could name it like Outlet.findById rather than Outlet.Outlet (the method should not be the same as the class; it is really confusing to read), and you can get parameters from the request



@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String id = request.getParameter("id");
String s = Outlet.findById(id);


When calling the API, you add ?id=value



Or you can get the final part of the path from request, assuming your API is setup like /path/ids/value - Refer What's the difference between getRequestURI and getPathInfo methods in HttpServletRequest? for options with this



Before doing this, of course you should double-check that query you are running actually returns data when querying the database directly.






share|improve this answer


























  • hey from where you are getting this "id" ?

    – dheeraj kumar
    Nov 20 '18 at 6:05











  • http://your.api.com/someAPI?id=x will pass x into your query

    – cricket_007
    Nov 20 '18 at 6:07













  • on my ui i don't have any input feild for idDB.. its is comming from loging query like there is a form form loging in on mu ui when ever a user looged in i run a query which checks username and pasword and also i get that value idDB from that query now i am passing that value into the java class methot which is Outlet and in that method i am using idDB in a query

    – dheeraj kumar
    Nov 20 '18 at 6:08











  • i didn't get you :(

    – dheeraj kumar
    Nov 20 '18 at 6:10











  • okay... I don't know your API, or your app's logic... The answer is still the same. You need to form that URL so that you can actually get an ID. Either that is part of the path, http/your.api.com/someAPI/ids/x to get that ID, or you can put ?id=x at the end

    – cricket_007
    Nov 20 '18 at 6:10
















1














Please read OWASP - SQL Injection and learn about PreparedStatements



First, methods should not start with capital letter, so rather you could name it like Outlet.findById rather than Outlet.Outlet (the method should not be the same as the class; it is really confusing to read), and you can get parameters from the request



@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String id = request.getParameter("id");
String s = Outlet.findById(id);


When calling the API, you add ?id=value



Or you can get the final part of the path from request, assuming your API is setup like /path/ids/value - Refer What's the difference between getRequestURI and getPathInfo methods in HttpServletRequest? for options with this



Before doing this, of course you should double-check that query you are running actually returns data when querying the database directly.






share|improve this answer


























  • hey from where you are getting this "id" ?

    – dheeraj kumar
    Nov 20 '18 at 6:05











  • http://your.api.com/someAPI?id=x will pass x into your query

    – cricket_007
    Nov 20 '18 at 6:07













  • on my ui i don't have any input feild for idDB.. its is comming from loging query like there is a form form loging in on mu ui when ever a user looged in i run a query which checks username and pasword and also i get that value idDB from that query now i am passing that value into the java class methot which is Outlet and in that method i am using idDB in a query

    – dheeraj kumar
    Nov 20 '18 at 6:08











  • i didn't get you :(

    – dheeraj kumar
    Nov 20 '18 at 6:10











  • okay... I don't know your API, or your app's logic... The answer is still the same. You need to form that URL so that you can actually get an ID. Either that is part of the path, http/your.api.com/someAPI/ids/x to get that ID, or you can put ?id=x at the end

    – cricket_007
    Nov 20 '18 at 6:10














1












1








1







Please read OWASP - SQL Injection and learn about PreparedStatements



First, methods should not start with capital letter, so rather you could name it like Outlet.findById rather than Outlet.Outlet (the method should not be the same as the class; it is really confusing to read), and you can get parameters from the request



@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String id = request.getParameter("id");
String s = Outlet.findById(id);


When calling the API, you add ?id=value



Or you can get the final part of the path from request, assuming your API is setup like /path/ids/value - Refer What's the difference between getRequestURI and getPathInfo methods in HttpServletRequest? for options with this



Before doing this, of course you should double-check that query you are running actually returns data when querying the database directly.






share|improve this answer















Please read OWASP - SQL Injection and learn about PreparedStatements



First, methods should not start with capital letter, so rather you could name it like Outlet.findById rather than Outlet.Outlet (the method should not be the same as the class; it is really confusing to read), and you can get parameters from the request



@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String id = request.getParameter("id");
String s = Outlet.findById(id);


When calling the API, you add ?id=value



Or you can get the final part of the path from request, assuming your API is setup like /path/ids/value - Refer What's the difference between getRequestURI and getPathInfo methods in HttpServletRequest? for options with this



Before doing this, of course you should double-check that query you are running actually returns data when querying the database directly.







share|improve this answer














share|improve this answer



share|improve this answer








edited Nov 20 '18 at 6:32

























answered Nov 20 '18 at 6:02









cricket_007cricket_007

80.4k1142110




80.4k1142110













  • hey from where you are getting this "id" ?

    – dheeraj kumar
    Nov 20 '18 at 6:05











  • http://your.api.com/someAPI?id=x will pass x into your query

    – cricket_007
    Nov 20 '18 at 6:07













  • on my ui i don't have any input feild for idDB.. its is comming from loging query like there is a form form loging in on mu ui when ever a user looged in i run a query which checks username and pasword and also i get that value idDB from that query now i am passing that value into the java class methot which is Outlet and in that method i am using idDB in a query

    – dheeraj kumar
    Nov 20 '18 at 6:08











  • i didn't get you :(

    – dheeraj kumar
    Nov 20 '18 at 6:10











  • okay... I don't know your API, or your app's logic... The answer is still the same. You need to form that URL so that you can actually get an ID. Either that is part of the path, http/your.api.com/someAPI/ids/x to get that ID, or you can put ?id=x at the end

    – cricket_007
    Nov 20 '18 at 6:10



















  • hey from where you are getting this "id" ?

    – dheeraj kumar
    Nov 20 '18 at 6:05











  • http://your.api.com/someAPI?id=x will pass x into your query

    – cricket_007
    Nov 20 '18 at 6:07













  • on my ui i don't have any input feild for idDB.. its is comming from loging query like there is a form form loging in on mu ui when ever a user looged in i run a query which checks username and pasword and also i get that value idDB from that query now i am passing that value into the java class methot which is Outlet and in that method i am using idDB in a query

    – dheeraj kumar
    Nov 20 '18 at 6:08











  • i didn't get you :(

    – dheeraj kumar
    Nov 20 '18 at 6:10











  • okay... I don't know your API, or your app's logic... The answer is still the same. You need to form that URL so that you can actually get an ID. Either that is part of the path, http/your.api.com/someAPI/ids/x to get that ID, or you can put ?id=x at the end

    – cricket_007
    Nov 20 '18 at 6:10

















hey from where you are getting this "id" ?

– dheeraj kumar
Nov 20 '18 at 6:05





hey from where you are getting this "id" ?

– dheeraj kumar
Nov 20 '18 at 6:05













http://your.api.com/someAPI?id=x will pass x into your query

– cricket_007
Nov 20 '18 at 6:07







http://your.api.com/someAPI?id=x will pass x into your query

– cricket_007
Nov 20 '18 at 6:07















on my ui i don't have any input feild for idDB.. its is comming from loging query like there is a form form loging in on mu ui when ever a user looged in i run a query which checks username and pasword and also i get that value idDB from that query now i am passing that value into the java class methot which is Outlet and in that method i am using idDB in a query

– dheeraj kumar
Nov 20 '18 at 6:08





on my ui i don't have any input feild for idDB.. its is comming from loging query like there is a form form loging in on mu ui when ever a user looged in i run a query which checks username and pasword and also i get that value idDB from that query now i am passing that value into the java class methot which is Outlet and in that method i am using idDB in a query

– dheeraj kumar
Nov 20 '18 at 6:08













i didn't get you :(

– dheeraj kumar
Nov 20 '18 at 6:10





i didn't get you :(

– dheeraj kumar
Nov 20 '18 at 6:10













okay... I don't know your API, or your app's logic... The answer is still the same. You need to form that URL so that you can actually get an ID. Either that is part of the path, http/your.api.com/someAPI/ids/x to get that ID, or you can put ?id=x at the end

– cricket_007
Nov 20 '18 at 6:10





okay... I don't know your API, or your app's logic... The answer is still the same. You need to form that URL so that you can actually get an ID. Either that is part of the path, http/your.api.com/someAPI/ids/x to get that ID, or you can put ?id=x at the end

– cricket_007
Nov 20 '18 at 6:10


















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53387016%2fhow-can-i-call-a-parametrized-method-of-java-class-into-a-servlet-doget-method%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

MongoDB - Not Authorized To Execute Command

How to fix TextFormField cause rebuild widget in Flutter

Npm cannot find a required file even through it is in the searched directory