Link to home
Create AccountLog in
Avatar of taiping
taiping

asked on

JAVA + HTTP Response

Hi, i need to prepare a URL for a user to come in and request something.

I come across some steps by steps to create the HTTP Responde using java technology and i couldn't find it anymore.

Please suggest me or show me some sample how can i prepare a URL, i mean in return back some response back to the caller.

thanks.
ASKER CERTIFIED SOLUTION
Avatar of cmalakar
cmalakar
Flag of India image

Link to home
membership
Create an account to see this answer
Signing up is free. No credit card required.
Create Account
SOLUTION
Avatar of mrcoffee365
mrcoffee365
Flag of United States of America image

Link to home
membership
Create an account to see this answer
Signing up is free. No credit card required.
Create Account
Avatar of taiping
taiping

ASKER

thanks.. and sorry to confuse you both.

In fact, i have written the below code to send back my response back to anyone that requested with a specific URL.


  public void doPost(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException {
       
            String strResponse = "Hello World";      
            byte[] outputbyte  = strResponse.getBytes();
            httpResponse.setStatus (HttpServletResponse.SC_OK);
            BufferedOutputStream bos =  new BufferedOutputStream(httpResponse.getOutputStream());
            httpResponse.setContentLength(outputbyte.length);
            bos.write(outputbyte, 0, outputbyte.length);
            bos.flush();
            bos.close();
            bos = null;
            return true;
        } catch(Exception e) {
            Log.Log(Log.ERROR,SERVLET_NAME,e);
            return false;
        }

But i what i need most is the in depth sample of HTTP kind of coding. For example, let say if i wish to send blob type over HTTP, how can  I do that? Or what is i want to use HTTP(s), what shoud i do?


  public void doPost(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException {
        
		String strResponse = "Hello World";	
            byte[] outputbyte  = strResponse.getBytes();
            httpResponse.setStatus (HttpServletResponse.SC_OK);
            BufferedOutputStream bos =  new BufferedOutputStream(httpResponse.getOutputStream());
            httpResponse.setContentLength(outputbyte.length);
            bos.write(outputbyte, 0, outputbyte.length);
            bos.flush();
            bos.close();
            bos = null;
            return true;
        } catch(Exception e) {
            Log.Log(Log.ERROR,SERVLET_NAME,e);
            return false;
        }

Open in new window

>> let say if i wish to send blob type over HTTP, how can  I do that?
Blob --> Binary large objects..

What kind of binary data it is .. "exe, pdf, or what"?
You need to set the content-type header of the response

For example, to send a pdf file over http response..
see the following example..

https://www.experts-exchange.com/questions/23159240/Download-a-file-from-server-and-open-it.html

To send the binary stream.. the content-type should be application/octet-stream

http://kb.iu.edu/data/agtj.html


>> what is i want to use HTTP(s), what shoud i do?
Basically when request is https, your server should send the response in https..
There is nothing special to do in doPost method..