I'm trying to send message to online user by user's servlet response object which is already stored in map. For Example if user A is to send message to B then i will get data from A's request object and write it to B's response object(from map). I'm trying it for avoid timed ajax call. Any suggestion and help. I'm getting message when reload the jsp page.can we have object listener in jsp.
IMHO,The servlet response will be sent back to the client when the doGet or Post method terminates, it won't wait for your asynchronous call(time that getting data from A's ) to finish.
You may face the issue "response alredy has been committed"
if user A is to send message to B then i will get data from A's request object and write it to B's response object(from map).
The request object of A and the response object of B, will be in different threads running the Servlet's service() method .I think you need to store the data sent using A's request in some app context probably and push it to B using AJAX or when user B makes a request fetch the data from context and send it to response to B.
Don't do this - don't mix requests and responses from different servlet calls.
Either use Ajax or periodic refresh using javascript or something similar.
You can use Comet (server push) but really Ajax or refresh are natural for your use case.
(Unless you have other concerns which you didn't share)
Related
I am using Java servlets using Apache tomcat.
I've configured a threadpool and am dealing with each request.
My page is taking in many GET requests at the same time, I'm wondering if I can respond to the server after each get request before any of the logic happens?
So server gives me a request -> I respond with either 'good send another' or 'bad send another' before I start my queueing.
Any help would be much appreciated!
EDIT
Sorry that was terribly written :(
What I'm asking for is a way to send a Header to the client (in this case it's a server which sends me lots of requests). The response would just be 200 or error based on the information I get sent.
What my program is doing:
My servlet gets sent lots of GET requests from one client. (over 100,000) Which I am using tomcat to queue and put into a threadpool. It is then assigned to a worker thread which processes it and puts it into a database.
I've been told to do is send a request back to that server saying 'ok received it'. I think I can use a header response but I don't have the URL of that client (and the client can change for different campaigns). So was wondering what the best way would be to send that response.
After doing some more research I think what I'm looking for is ServletOutputStream.
response.setContentType("text/html");
ServletOutputStream output = response.getOutputStream();
output.flush();
output.close();
Using servlet output stream where do I set the <head><body> tag? and insert the header response afterwards.
The simple answer is "sure".
If these are get requests from a web page for a web page, include a refresh timer and send back some token that can be used to identify the difference between a first-time-request and an I-requested-earlier-are-you-done request. In this case the refresh timer can be set via a meta refresh tag.
If the get requests are part of a REST API then you can define "got it and I'm working" into the protocol. For instance, return a 202 to indicate "got it but not done" and return 200 to indicate "done". As with the html page, consider sending some token back with the 202 that identifies the pending request.
Let me explain what I am doing first:
I have a servlet that handles some GET, POST and PUT requests.
Now on my PUT request I am saving a file from the request.inputStream. Now I want to do some things like I issue another GET request that can give me the status of reading the input stream of that previous request. I can issue an PUT request that can put some binary data with range for that file I am saving previously. Or I can send a DELETE request that will cancel the upload.
How can I do that? How can I access one request from another?
You could use the HttpSession object on the request and save the bytes read on the InputStream.
The session attribute will be accessible on the following request.
I am using REST API for searching. When an ajax call is fired, REST returns json from Java code
return JResponse.ok(searchResult).build() //searchResult is List of Custom object
In javascript I would stringfy that json and parse to show relevant data on screen.
var search = jQuery.parseJSON(JSON.stringify(data));
Now I want to secure/obfuscate json response returned from REST, so that anyone who directly hits APIs won't get readable response. I tried bson but bot able to implement it successfully. Didn't find much support on how to put collection object in bson and how to retrieve it back in JS while googling.
I will suggest you to go with tokens.
Every time when request made to server request must contain a token which change for every request as well check that the request header for ajax request. If it is an ajax request then and then only return result. Also add rule for no cross browser access.
I think if you did it your data will not be accessible to anyone by direct http request.
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Servlets: doGet and doPost
I know doGet() request is appended to the request URL in a query string.But I don't know the concept of doPost() request.how does doPost request posting information to the server.
Please Guide me to get the working concept of doPost request...
Post requests are used usually for sending data to Server, and get request for reading data from server. In Post request data is sent in http request body, so data size can be very large compared to Get. If a browser fires an POST request (usually a form submit) doPost of the mapping Servlet will be called. There is another overloaded method (service()) which is called for both GET and POST
In doPost() the data is not appended in the URL.
It can handle large amount of data compared to the doGet() method.
Filling of form and submitting is done through doPost(), it's secure to use doPost() during submission of the username and password.
There is also differnce in the doGet() and doPost() header and body structure.
The main conceptual difference GET and POST is that, GET is used for getting the data from the server, and POST is used for updating the data to the server.
In general POST has the following properties:
The data is x-www-form-urlencoded . Which means, the request parameters are sent as request body. And the server has to parse the request body for parameters.
By default, when no content-length header is present, the default value for GET is 0 whereas for POST it is till end of stream.
GET is Idempotent whereas POST is Non-Idempotent. i.e, Proxies on failures for GET they retry. But, for POST they do not retry.
doGet() can be used when client request doesn't intend to change stored data.
In using Apache MINA, I'm sending a login request from the client, which is interpreted on the server via LoginRequestDecoder (implements org.apache.mina.filter.codec.demux.MessageDecoder).
I now want to send a response (LoginResponse) that includes a success/failure code. Should I be sending the response from the LoginRequestDecoder's finishDecode() method, or is there a better place for it that I'm overlooking?
What I needed to do was make my IoHandler of type DemuxingIoHandler. Within its constructuor, I had to make multiple calls to addReceivedMessageHandler and addSentMessageHandler. This allows the code behind DemuxingIoHandler to automatically respond based upon the type of message received.