I've set up a Google App Engine application to respond to various HttpPosts and store various bits of information. For some reason in one of my HttpServlets, I am unable to respond back to an HttpPost. I know I've completed this in a similar fashion before but have been unable to figure out the solution neither from trial/attempt nor stackoverflow questions.
Here, my servlet receives the post with no problem:
#Override
public void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException
{
//I do some random processing and storing of data
//Then I respond with a String dependent upon the info I'm given
resp.setContentType("application/JSON");
PrintWriter out = resp.getWriter();
out.print(myResponse);
out.close();
}
I never receive the actual response data back however. The method below is never called. I've set the NSURLConnectionDelegate.
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data;
Does anyone know why this might be?? I'm considering switching to an HttpGet but I know for sure I've done this with an HttpPost before. Any help will be extremely appreciated thanks!
Related
In Google's Appengine with Java, the servlet:
#Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
Writer w=resp.getWriter();
w.write("a");
resp.resetBuffer();
w=resp.getWriter();
w.write("b");
}
gives "ab" in the response, in the development server. I was expected just "b", from reading Servlet Spec 2.5.
What is wrong?
I'm reading the documentation at https://cloud.google.com/appengine/docs/java/requests.
I tried resp.setBufferSize(8192) and then getBufferSize() but just got 1.
appengine version: 1.9.34, java version: openjdk version "1.8.0_66-internal", ubuntu 14.10
Here is what the Java Servlet Specification (Version 3.0) says:
"The reset method clears data in the buffer when the response is not
committed. Headers and status codes set by the servlet prior to the
reset call must be cleared as well. The resetBuffer method clears
content in the buffer if the response is not committed without
clearing the headers and status code.
If the response is committed and the reset or resetBuffer method is
called, an IllegalStateException must be thrown. The response and its
associated buffer will be unchanged."
Facially, that says that resetBuffer should either clear out the "a" or throw an exception.
I guess, you could make the argument that the characters are buffered in the Writer not the response buffer, but the Servlet spec doesn't make any such distinction. Furthermore, such an interpretation of the spec would render resetBuffer effectively useless.
If Google AppEngine is really behaving the way you say, I'd call that a bug.
I have a Java Servlet application running on JBoss 4 and this application receives POST request from another service. I want to acknowledge back to this service before processing. Is it fine to do the following?
protected void doPost(HttpServletRequest req, HttpServletResponse res) {
readReceivedPOSTData();
//send response
PrintWriter out = res.getWriter();
out.print("ack");
out.close();
//Process
processData(); //takes long time
}
I appreciate your help. Thank you.
The basis is ok.
Just some tips:
Use an identifier in the request so you can check in the future the status of that request.
Start another thread to process the data or use a jms queue
remember that you can't write additional data to the response in the processData() method
I'm implementing an enterprise application with Java EE on Glassfish server. I need to my application to execute some logic to show the proper output for a specific subset of URLs.
Problem description:
My web pages folder has this structure:
Web Pages
Protected
- CorrectPage.xhtml
- A.xhtml
- B.xhtml
- Index.xhtml
I want the user to access the URL:
/Protected/CorrectPage.xhtml
But the user must not be able to access the following URLs:
/Protected/A.xhtml
/Protected/B.xhtml
When the URL /Protected/CorrectPage.xhtml is entered I want to execute some logic and depending on the outcome of this logic I want to show either A.xhtml, or B.xhtml, without any visible URL changes (redirects).
Solutions tried so far:
I thought about using a servlet mapped to /Protected/*.xhtml while leaving the Faces Servlet deal with any other URL in my application.
and having :
protected void processRequest(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
if(*Requested URL is /Protected/CorrectPage.xhtml*) {
if(logic())
*Show A.xhtml*
else
*Show B.xhtml*
} else
*Show 404*
My issue is that I don't know how to implement the Show A.xhtml. I basically want to print to the client my xhtml page.
I also thought about solving this last issue by using the response PrintWriter.
PrintWriter pw = response.getWriter();
But than again this doesn't solve my issue since I don't know how to print the xhtml file while also having the evaluation of the expression language contained in it.
Conclusion
Any help is extremely appreciated. Even if that means changing something in the structure I proposed. Naturally if the creation of a servlet isn't the correct solution for my issues I will leave that track.
I'm interested only in the outcome the user will experience.
Thanks in advance
You may use request.getRequestDispatcher("/protected/page[A|B]").forward(request, response)
I'm going to make streaming. I have .jsp file and at the end of .jsp file I include my Async Servlet using following code:
<jsp:include page = '/simple' flush = 'true' />
So I want when whole page is loaded to open an infinite Async request, which will handle Async response.
Here is my Servlet code:
public class SimpleAsyncServlet extends HttpServlet {
public static AsyncContext ctx;
protected void doGet(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException {
req.setAttribute("org.apache.catalina.ASYNC_SUPPORTED", true);
ctx = req.startAsync();
ctx.setTimeout(0);
}
}
From other java classes I'm using the static SimpleAsyncServlet.ctx.getResponse.getWriter() to println some javascript code to current page. It is working without any problem, but browser keep showing that it's loading. According to Async idea page should be loaded and this Async Request should stay alive in background and..that's it, but no....browser keeps loading the page till forever (timeout is 0, cos I want to have infinite open reqeust)
Where am I wrong and how can I make this permanent request without this browser loading ?
P.S. I have tried to access my servlet direct from url (localhost.../simple) and then I see nothing printed on page. It keep loading till forever.
You are trying to achieve the impossible.
The browser will showing that the page is loading until it knows it has received the full request by one of the following methods:
it has received the number bytes stated in a Content-Length header
the connection is closed
it received an end chunk when using chunked encoding
Since you want an 'infinite' response, none of the three options above is ever going to happen.
I already asked this question, but because of missing answers i'll give it another try!
What i want to do
Write a java webapp which logs me, automatically, in any webapp. For an example I chose my zimbra
mailaccount.
How should my Java webapp work
the app should send a login request to the particular app, which is usually corresponding to
the action in the login-form. After executing my app cookies should be set so that I'm logged in
automatically.
What I'm currently trying
I'm using the code below, the doGet() method is part of an extended HttpServlet.
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
response.setContentType("text/html");
PrintWriter out = response.getWriter();
HttpClient httpclient = new HttpClient();
PostMethod postMethod1 = new PostMethod("http://mail.mydomain.at/zimbra/");
postMethod1.getParams().setCookiePolicy(CookiePolicy.BROWSER_COMPATIBILITY);
postMethod1.addParameter("username", "myname#mydomain.at");
postMethod1.addParameter("password", "mypassword");
postMethod1.addParameter("loginOp", "login");
httpclient.executeMethod(postMethod1);
out.println(postMethod1.getResponseBodyAsString());
}
What's the problem
After executing the code I only get a message, saying that my browser(which is in fact my HttpClient) appears to prohibit cookies.
So what, I think, happens is that the cookies are going to be set in the HttpClient, which I initialized.
But what i want to happen is that the cookies are set in my browser. How can i accomplish this?
SOLUTION:
the quest failed, because by logging in with a webapplication (via a self instantiated HTTPClient), this client receives the cookies, but not the browser. No workaround was found.
I don't think you can set a cookie of www.xyz.com domain if your browser is accessing www.abc.com. Otherwise it would be a big security violation. Anybody could set a fake cookie in their browser and access your data.
In your case you will need to excpilitly login to zimbra from your browser in order to get the cookie set on the client.