Getting post data from request - java

I'm writing a server side app in Java using the HttpCore library.
I have an HttpRequest and I'm trying to get the postdata sent from a form. The problem is- when I use request.getEntity() it returns a null object, even though when I look through HTTPFox on what kind of request I'm sending the post data is clearly there.
What am I doing wrong?

There seems to be some confusion. You are sending requests from a browser to the server. The server is likely using the servlet API. There you handle requests using the doPost(..) method of an HttpServlet. You have an HttpServletRequest from which you can get the parameters - request.getParameter("paramName")
HttpCore on the other hand is used to make requests, not to handle requests. It is used as an http client (in the role of the browser).

Related

Receive an http post from an external server with servlet

on my public web application running on JBoss 5 with Java 6 I need to implement something that receive an http post from another external server with some parameters (server to server call) .
The server that call me doesn't need an answer, just standard http code reponse( es 200 - OK).
Can I use a servlet for this?
If not what should I use? (I cant'use restfull or soap ws).
To my knowledge, your best bet for given situation is REST or SOAP. But you already mentioned that you cannot use that.
I don't see any reason why a servlet can't be used here. You have request & response objects in servlet doPost method. You can manipulate the response object to set appropriate response code back.

Getting data from post request in GWT client

I am trying to handle POST request in GWT. I did that in server side, but, i feel its work will be fine if the POST request is handled in client side. I was able to complete GET request in client side by reading the query string. But in case of POST, a file is being sent. How can i able to get the file from POST request in GWT client.
Thanks.
handling post request in GWT client is not possible. refer :
https://groups.google.com/forum/#!topic/google-web-toolkit/LOWMIPYcd7A

Java HTTP/HTTPS post from app 1 to app 2 in different server

I have to develop a java application which has to do a http post to a different application. Both the application does not have a JSP page as a front end. (Http POST from servlet to servlet).
When I was exploring options for this I managed to find
response.sendRedirect("http://localhost:8081/appname/servletname?var1=&var2=?...);
But this is based on http get. How do I do the same using HTTP post?
Thanks
Arun Kumar
The code you found is for redirecting an incoming request to another location. The client ends up sending a new request to the new URL. This is not what you want.
You can make an HTTP request using the class java.net.URL. You can then get an OutputStream and write your POST data in the body of the request. You will have to encode all your data values with java.net.URLEncoder.
I think there is a library in Apache commons that makes this easier.

Call an external web service from a servlet

I'm developing a servlet that gets a name of a web service and could be forward the request to an external web service, for example: http://www.webservice.com/...
I have build a response wrapper that intercept response output but I can't forward request to an external web service, it works only if I redirect the request to a servlet that is on same server.
Example:
request.getRequestDispatcher("aMyServlet").forward(request, response) // WORKS
request.getRequestDispatcher("http://www.webservice.com/...").forward(request, response)
Doesn't because Tomcat searches http://www.webservice.com/... on the server as a local resource.
How can I do external request?
Thanks
forward method that you are using is used for communicating between server resources, (eg: servlet to servlet as you have found out) If you want to redirect to another location, you can use the HttpServletResponse's sendRedirect method.
Better option is to
Perform your own HTTP request and stream the results back to the
browser. This sounds harder than it is. Basically you create a
java.net.HttpURLConnection with the URL of the web site you want to
"redirect" to. This can actually contain the query parameters (as long as
they're not too large) since it will never be sent to the user's browser
either and will not appear in the browser URL bar. Open the connection, get
the content and write it to the Servlet's OutputStream.
To make any request to an outside service, you'll have to explicitly make a new HTTP request and handle its response. Take a look at the HttpUrlConnection class.
You don't mention what kind of service you want to invoke, but either way, your servlet is functioning as a service client, so you should be looking at service client technologies.
For invoking REST style services, java.net.URL or Apache Commons HttpClient can be used to send a request for a URL and fetch the reponse.
For invoking SOAP services, you can use Apache Axis, or Java WSIT.

Google protocol buffers and servlets

I am wondering how I can use google protocol buffers to accept a request and send a response back to a client? I am thinking about writing a servlet which will take a request.
Is the following trail of thought the correct way to implement this:
1. Have a .proto file which is the message definition for the incoming request.
2. Write a servlet which accepts this request, does various tasks like querying database
and then sends a response. Will this response require a separate .proto message definition with all the fields that make up the response?
3. Will the client just invoke the doGet() method of my servlet and pass the request, it should then return a response as a protobuff object?
Any suggestion or idea will be very much appreciated.
Typically you'd want a request message and a response message, yes. You'd also probably want a method name to describe the action - that's certainly how the built-in PB services work.
The client wouldn't invoke doGet() - it would make a request (probably a POST rather than a GET) and your servlet would receive it.
Now, ideally you could have a general "ProtocolBufferServlet" which could service the requests by handing them off to services implementing the appropriate interfaces.
I suggest you look at the documentation for Protocol Buffer services and the Java services generated code for more information. You could implement an RpcChannel which worked over servlets, or get the client to make the HTTP post directly. You'd probably use dependency injection of some kind at the server side to tell the servlet what was implementing the service.
HI,
I have this up and running. I ended up posting a http request as a post to my servlet. I was able to take the request protocol buffer, read the request, do some processing and then send back a response. It was actually really simple once I got it working. We used the 1 .proto file to define the request and response message structure.

Categories

Resources