I want to get the Http Response of URL in gwt before the Async method calls.
Below is my working code but i want to execute it before the Async call.
RequestBuilder builder = new RequestBuilder(RequestBuilder.GET, "http://localhost:8080");
try {
builder.sendRequest(null, new RequestCallback() {
#Override
public void onResponseReceived(Request request, Response response) {
}
#Override
public void onError(Request request, Throwable exception) {
throw new UnsupportedOperationException("Not supported yet.");
}
});
} catch (RequestException ex) {
}
return response.getStatusCode();
Also note that i am using JavaScript in my project. Is there any way to write this code in javaScript and retrieve the response in my OnModuleLoad method?
Any suggestions will be highly appreciated.
I have made the simple function in javascript with async : false. Then i hit the url and get the response.
This response will be taken in onModuleLoad using native method. Sync calling.
Solved :-)
Related
I need to make a simple upload, just to send some files to the back end from the front end. But for some odd reason I can't make a POST request.
When I make a GET request, the controller is called and the simple printing that I have in it executes.
But when I create a POST request with which I wish to send the file to the back end, all that happens is that the request is created, the java code/Spring controller is not executed, not called. But the request returns with a success, and the callback function is executed.
Here is the controller, its supper simple. Nothing serious because I can't even get it to execute.
#RequestMapping(value = "/testingPost", method = RequestMethod.POST)
public void postTest(final HttpServletRequest pRequest, final HttpServletResponse pResponse){
#RequestMapping(value = "/testingPost", method = RequestMethod.POST)
public void postTest(final HttpServletRequest pRequest, final HttpServletResponse pResponse){
System.out.println("TESTING....POSST.........................................................................................");
}
And here is the Javascript snipped from the controller.
var data = {
testing: 'testInfo'
};
var config = {
'Content-Type': 'application/json; charset=UTF-8'
};
$http.post(urlBuilder.create("/S/S/S/" + "usglGed" + "/" + "testingPost"), angular.toJson(data), config)
.
then(function(response){
console.log("in success post");
}, function(response){
console.log("in fail");
});
Again nothing special. And I have no clue as to why spring controller is not executing the method. I'm not sure if there is some configuration in the project. Or I'm just not experienced enough, and I'm not seeing something.
Thank you in advance!
I am fairly new to spring and want to the correct way of making post request. I have a list of json object that I want to post to my server
for example
var list = [{name:"abc",age:23},{name:"xyz",age:22},{name:"xcx",age:33}]
I am making a post request in google closure using xhr to my server in this fashion
model.xhrPost(id,url,"list="+JSON.stringify(this.list),callback);
This is what my controller looks like
#RequestMapping(value={"/getInput"}, method = RequestMethod.POST)
#ResponseBody
public String logClientError(ModelMap model, HttpServletRequest request, HttpServletResponse response) throws Exception{
JSONObject result = new JSONObject();
try{
String errorObj = request.getParameter("list");
JSONArray errors = new JSONArray(errorObj);
some more code here which loops through the list...
result.put("isSuccess", true);
return result.toString();
}catch(JSONException e){
result.put("isSuccess", false);
return result.toString();
}
}
So in short I am making a post request by passing querystring parameter. Is this the correct way or should the content be posted in the body? If I post in the body what changes do I have to make ?
This is definitely not how you should post the data to REST endpoint. Going this way you can use GET instead of POST and it will work as well. However POST should be definitely used to create new resource and the content should be carried in message body not as a query param.
On the backend side you can catch and parse the content yourself or create a class (see below) that will be filled with data from body.
DTO:
class Person {
String name
Integer age
}
class PersonList {
List<Person> persons
}
Endpoint:
public String logClientError(#RequestBody PersonList list, HttpServletRequest request, HttpServletResponse response) throws Exception
Body:
{
"persons": [{name:"abc",age:23},{name:"xyz",age:22},{name:"xcx",age:33}]
}
#ResponseBody can be used in the same manner for responses.
How do I obtain the response from google volley? I'm able to display the response in my logcat using System.out.println("Volley"+response);
I cant figure out how to pull it out of the google volley function. Heres my code:
final String url = MAIN_URL;
// prepare the Request
StringRequest sr = new StringRequest(Request.Method.GET, url,
new Response.Listener<String>() {
#Override
public void onResponse(String response) {
System.out.println("Volley"+response);
//I want to be able to get this response and use it in other functions.
}
}, new Response.ErrorListener() {
#Override
public void onErrorResponse(VolleyError error) {
System.out.println("Error"+error);
}
});
// add it to the RequestQueue
sr.setRetryPolicy(new DefaultRetryPolicy(
10000,
DefaultRetryPolicy.DEFAULT_MAX_RETRIES,
DefaultRetryPolicy.DEFAULT_BACKOFF_MULT));
queue.add(sr);
I always put queue into singleton - Application and request can be called and received wherewer in the code, fom example:
android volley caching asynchronous requests - How te get url of the request in ResponseListener
So you just create your own response listener in the class where you need it
I have a web application with java and EXT JS(3.4).
For all servlets, this is what I have in my doGet method:
protected void doGet(HttpServletRequest request, HttpServletResponse response) {
try {
response.sendRedirect("ErrorPage.jsp");
} catch (Throwable t) {
//do some logging
}
}
I simulate a GET request, with EXT JS using:
Ext.Ajax.request({
method : 'GET',
url : 'web'//web is the servlet name
});
The page does not redirect. This is the output from tamperData plugin of firefox:
If you are unable to see image, it says that the AJAX Get request responded with status 302:meaning redirect, and there was a request for ErrorPage.jsp which returned a status code of 200:meaning that the request completed successfully.
If i alert the response in the callback of the ajax request, it alerts the contents of the ErrorPage.jsp. I have a large number of such AJAX requests. Is there any reason why the redirect is not working.
I added the following code as well. But i always get status as 200. When though i can see in firebug that for a get request first request gave status 302 and the second one for Error Page gave 200.
Ext.Ajax.on('requestcomplete', function(conn, response, options) {
alert('successful'+response.status)
});
Ext.Ajax.on('requestexception', function(conn, response, options) {
alert('failed'+response.status)
});
Details:
OS : Windows 7
Server : Apache Tomcat 7.0.42
Servlet Api: Using servlet-api-3.0.jar
Based on #AnthonyGrist's post, this is the solution i came up with for EXT JS.
In the controller I changed doGet to :
#Override
protected void doGet(HttpServletRequest request, HttpServletResponse response) {
try {
response.setStatus(403);
} catch (Throwable t) {
//do something
}
}
A status code of 403 is recognised in EXT JS as a failure.So in the jsp page :
Ext.Ajax.on('requestexception', function(conn, response, options) {
if(response.status=403){
window.location.assign("ErrorPage.jsp");
}
});
I want to call Servlet doPost() method by using the javascript code but iam getting http 405(HTTP method GET is not supported by this URL) exception.
Here is my javascript code:
url="RedirectServlet?&FD="+FD+"&TD="+TD+"&actionid="+status+"&usercode="+usercode+"&action=reports"+"";
RedirectServlet.java:
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException
{
if(action.equals("reports")){
System.out.println("inside reports");
//Getting values from Reports_arb.jsp
String Fromdate=request.getParameter("FD");
String Todate=request.getParameter("TD");
String status=request.getParameter("actionid");
String usercode=request.getParameter("usercode");
//placing given values in a session
request.setAttribute("FD", Fromdate);
request.setAttribute("TD", Todate);
request.setAttribute("actionid", status);
request.setAttribute("usercode", usercode);
//Redirecting to showReport_arb.jsp
//response.sendRedirect("showReport_arb.jsp");
request.getRequestDispatcher("showReport_arb.jsp").include(request, response);
}
}
By seeing you URL, you are sending the data along with the URL. which as servlet get request.
So URL is trying access doGet, but where there is no implemention of doGet in servlet causing problem.
EDIT
use this to make access of your servlet doPost
<form ... method="post">...</form>