• Post Reply Bookmark Topic Watch Topic
  • New Topic

ServletOutputStream

 
Mukunthan Shanmuganathan
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Guys!

Can anybody please help me to write and test a small ServletOutputStream example.

Thank You. :roll:
 
Ben Souther
Sheriff
Posts: 13411
Firefox Browser Redhat VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Mukunthan Shanmuganathan,

Welcome to the ranch.

What type of help are you looking for?
It's a little odd to want to write a ServletOutputStream program without first having a problem to solve, unless it's a homework assignment.

Is this a homework assignment?
 
Mukunthan Shanmuganathan
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Thank you!

This what I did.

package com.mypack;

import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;

public class MyFirstServlet extends HttpServlet {

public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doPost(request, response);
}

public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

byte[] bytearr = new byte[6];

for(int i=0 ; i < bytearr.length ; i++) {
bytearr[i] =(byte) i;
}

ServletOutputStream out = response.getOutputStream();

for(int j=0 ; j < bytearr.length ; j++) {
out.write(bytearr[j]);
}

}

}

When I deploy and run I get a blank screen. Why?

Since I'm new to WCD, kind enough to bear me.

Thank You.
 
David O'Meara
Rancher
Posts: 13459
Android Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There are a couple of things that you are missing.

Firstly, when you send something to the browser, it needs to be something that the browse is able to display. You're code responds with 6 bytes of data, but there is a good chance you just managed to confuse the browser

The second thing is called a 'mime type', which is like a hint about what the content is. Browsers can use other tricks to find the data type, but it is better if you pass that information yourself.

 
Mukunthan Shanmuganathan
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dear Ben Souther, David O'Meara!

I've got it.

Thank you.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!