Applet otvara konekciju sa Servletom, cita byte stream, pretvara u Object stream i iz njega dobijem StyledDocument...
Ovaj kod to radi.
Problem nastaje kada iz baze citam vise redova. Svaki red je jedan byte[] array. Kako da posaljem dva ili vise byte[] array kao HttpResponse?
Code:
public class SqlServlet extends HttpServlet {
@Override
public void doGet(HttpServletRequest req, HttpServletResponse resp){
ResultSet rs=null;
***
rs = stmt.executeQuery("select blog from tekstslika where id='1'");
***
try{
rs.next();
blob=rs.getBlob("blog");
byte[] blobData=blob.getBytes(1, (int)blob.length());
int size = blobData.length; // bytes to be sent
resp.setContentLength(size);
resp.setContentType("Content-Type");
ServletOutputStream stream = resp.getOutputStream();
stream.write(blobData);
stream.flush();
}catch(Exception e){System.out.println("3 " +e);}
}
}
public class SqlServlet extends HttpServlet {
@Override
public void doGet(HttpServletRequest req, HttpServletResponse resp){
ResultSet rs=null;
***
rs = stmt.executeQuery("select blog from tekstslika where id='1'");
***
try{
rs.next();
blob=rs.getBlob("blog");
byte[] blobData=blob.getBytes(1, (int)blob.length());
int size = blobData.length; // bytes to be sent
resp.setContentLength(size);
resp.setContentType("Content-Type");
ServletOutputStream stream = resp.getOutputStream();
stream.write(blobData);
stream.flush();
}catch(Exception e){System.out.println("3 " +e);}
}
}
Code:
// na strani klijenta
// APPLET
***
StyledDocument doc;
URLConnection con=null;
InputStream instream=null;
ObjectInputStream ois=null;
ByteArrayOutputStream byteArrayOutputStream=new ByteArrayOutputStream();
ByteArrayInputStream byteArrayInputStream = null;
try {
con = getServletConnection();
instream = con.getInputStream();
int c;
while ((c = instream.read()) != -1) {
byteArrayOutputStream.write(c);
}
byteArrayInputStream = new ByteArrayInputStream(byteArrayOutputStream.toByteArray());
ois = new ObjectInputStream(byteArrayInputStream);
doc= (StyledDocument)ois.readObject();
jTexPan.setStyledDocument(doc2);
}catch (Exception ex) {System.out.println(ex);}
// na strani klijenta
// APPLET
***
StyledDocument doc;
URLConnection con=null;
InputStream instream=null;
ObjectInputStream ois=null;
ByteArrayOutputStream byteArrayOutputStream=new ByteArrayOutputStream();
ByteArrayInputStream byteArrayInputStream = null;
try {
con = getServletConnection();
instream = con.getInputStream();
int c;
while ((c = instream.read()) != -1) {
byteArrayOutputStream.write(c);
}
byteArrayInputStream = new ByteArrayInputStream(byteArrayOutputStream.toByteArray());
ois = new ObjectInputStream(byteArrayInputStream);
doc= (StyledDocument)ois.readObject();
jTexPan.setStyledDocument(doc2);
}catch (Exception ex) {System.out.println(ex);}