Response code from GET request to jsp page using SimpleFormController - java

I am trying to set up a http responder for incoming GET requests using a JSP page with a SimpleFormController.
I am receiving a GET request from a third party company, who are expecting a 200 http response back. However, even though my JSP is basically blank, when a GET request come in, it bypasses my JSP page and is automatically submitted to the onSubmit method in my controller and a http response of 201 is sent back.
How can I send a 200 response back, instead of 201?
Thanks,
Dave

If the onsubmit method is being called, it sounds to me like they are not sending a get request, but doing a post.
Make sure they are sending a get request.

Related

send different response to ajax using executor service

Let's say I am making an ajax call which will get a string as a response. How will I send 3 different response to same ajax call using java's executor service?
You can't send 3 response in only one ajax call.
One request will always return one response.
To solve this problem, you can make 3 request (3 ajax) OR put all your information in your unique ajax response

Unable to redirect to another page in AEM using request dispatcher in sling servlet via POST

I have a scenario where from one page in AEM, I need to call another AEM page in the same application and I need to pass some hidden parameters. I choose to do it via POST and below are the steps which I followed:
From page "A", I did a form submission via POST to the sling servlet and passed some parameters.
2. In the servlet, using request dispatcher I redirected the same request and response to a different page in doPost method using the following code snippet:
request.getRequestDispatcher("/content/company/en/apps/welcomepage.html").forward(request, response);
When I run the code, I am able to call the servlet through form submission but I am not able to redirect to a new page. I see the below error in logs:
18.10.2017 14:41:00.802 ERROR [127.0.0.1 [1508352060795] POST /bin/rap/welcomepage HTTP/1.1] org.apache.sling.servlets.post.impl.operations.ModifyOperation Exception during response processing.
javax.jcr.nodetype.ConstraintViolationException: No matching property definition: appointmentTypeId = platform001d
at org.apache.jackrabbit.oak.jcr.delegate.NodeDelegate.setProperty(NodeDelegate.java:522)
at org.apache.jackrabbit.oak.jcr.session.NodeImpl$35.perform(NodeImpl.java:1375)
at org.apache.jackrabbit.oak.jcr.session.NodeImpl$35.perform(NodeImpl.java:1363)
at org.apache.jackrabbit.oak.jcr.delegate.SessionDelegate.perform(SessionDelegate.java:208)
at org.apache.jackrabbit.oak.jcr.session.ItemImpl.perform(ItemImpl.java:112)
at org.apache.jackrabbit.oak.jcr.session.NodeImpl.internalSetProperty(NodeImpl.java:1363)
If I try the same code in doGet method it works fine. Also if I use response.sendRedirect("/content/company/en/apps/welcomepage.html") it works fine too. But the problem with this is it initiates it as a new request to the page and it looses all the parameters which I get from the form submission. Could someone please let me know like how can I redirect a request to a page in AEM via POST since I need to pass some hidden parameters whic should not be visible in the url ?
Here is the way I understand your question
User visits page "A"
User fills a form, then submits.
Your custom servlet handles the submitted POST request and calls:
request.getRequestDispatcher("/content/company/en/apps/welcomepage.html").forward(request, response);
You get the ConstraintViolationException
Why do you get this exception?
Since you are using forward the POST request is forwarded to /content/company/en/apps/welcomepage.html that node is most likely of type cq:Page, which has constraints on which properties can be added. Think of it as a simple post request trying to store parameters on the cq:Page node.
What can you do?
Since I don't understand your use-case and particularly why you need to preserve the submit params, I cannot recommend a specific solution. However, since you don't want the parameters in the URL, here is a potential solution you can try:
In your servlet handler, see those hidden params to cookies on the response.
use response.sendRedirect("/content/company/en/apps/welcomepage.html")
On any of the components in /content/company/en/apps/welcomepage.html, you can get the request cookies and process them however you like. same way you wanted to process those hidden params.
Now the flow becomes:
User visits page "A"
User fills a form, then submits.
Your custom servlet handles the submitted POST request, adds your special params to cookies on the response, then calls response.sendRedirect("/content/company/en/apps/welcomepage.html")
User's browser receives a 301 response with the cookies, sets the cookies in the browser then requests "/content/company/en/apps/welcomepage.html"
Your components handle the request and get the params from the cookies, then retuns the appropriate response.

FormData getting lost in HTTP 302 to 200

We getting a SSO request using Ping Identity Federation server which opening target URL with a post of opentoken as form data. But at my application I am getting two request one is 302 Found where I can see form data in headers but after that it issue a 200 request in which data is not there.
Is this normal if yes how to get formdata?
Just curious for this behaviour....I can still access that data from referrer header information.
I am using java/jsp.
It was actually coming in request body which need to be read using getReader. that's why it redirecting again to 200 as in first request I was consuming parameter instead of body.

http get from jsp and resolve status code

I need to send http request to certain URL via jsp (by clicking on a link / button). Depending on the http response status code I need to generate corresponding output in the jsp (200 - action OK, 404 - unknown action etc, the content of the URL is irrelevant). Is there any way to achieve this behaviour?
I am using Spring.
First send your request to a servlet using AJAX and call the url that you want from the servlet.From that url response you can use int status = response.getStatus(); on the HttpServletResponse object in your servlet and based on the int value that you get you can write your if else statements and use PrintWriter's println method to send response/text to the ajax call on your jsp and use it wherever you want in that jsp page.
you can use jquery and make an ajax call using its ajax method and use it success/error callback methods to get textStatus

How to handle POST response using Servlet

I'm new to Java Servlet programming and have a question about how to handle POST response from other servers (not user's POST request) using Servlet programming.
Suppose my application needs to consult another server in order to process user's request. I need to
send an asynchronous POST request (i.e. specify a redirect_uri in the POST request body) to the other server;
handle the POST response from the other server;
present some result to the user.
I think I need one Servlet to handle user's request and send a POST request to the other server, and I need another Servlet (since the POST request is asynchronous) to handle the POST response from the other server. My specific questions are:
What's the best way to send a POST request in this case? For example, using HttpUrlConnection?
How to handle a POST response in a Servlet? It confused me because a servlet is supposed to handle "request" not "response" but in this case the incoming message is indeed a POST response from the other server. In particular, if you can point me the relevant API/method that would be really helpful. For example, in doPost()? How to get the POST response body? (I assume we can get it from HttpServletRequest object).
Thanks very much!
Yue
I did not understand the term 'POST Response'.However may be you are talking about servlet chaining scenario if I understood your requirement correctly.
Servlet Chaining means the output of one servlet act as a input to
another servlet. Servlet Aliasing allows us to invoke more than one
servlet in sequence when the URL is opened with a common servlet
alias. The output from first Servlet is sent as input to other Servlet
and so on. The Output from the last Servlet is sent back to the
browser. The entire process is called Servlet Chaining.
Example of Servlet Chaining

Categories