Hi,
I just wanted to follow up.. There are only two ways to grab an image from an
other machine whose codebase is different from where your applet resides.
1. using a signed applet
2. using a servlet to send the image to the applet.
I have discovered the following code online that does the latter, I hope it
is useful to someone eventually.. thank you for your response.
Here a servlet is handing a file to the applet.. I have not implemented
this, but it looks, at first glance, to be useful. I have noticed that
there have been a lot of APIs created to handle image
manipulation on a servlet specifically..
From: Phil Hanna ([EMAIL PROTECTED])
Subject: Re: Servlet <==> Applet
Newsgroups: comp.lang.java.programmer
View: Complete Thread (4 articles) | Original Format
Date: 1999/12/15
On Wed, 15 Dec 1999 03:10:09 GMT, "Ron H." <[EMAIL PROTECTED]> wrote:
>My question is, how then can I transfer the binary data from the servlet to
>the client applet?
>
>Let's say the servlet reads a binary file on the host disk, and loads an
>array, say, double data[]. How can I then transfer data[] to the
>client applet?
There are several ways to do this. One straightforward way is to
1. have the applet open a URL connection to the servlet passing the
file name as a parameter
2. have the servlet write the array of doubles to its output stream
(after setting the content type to application/octet-stream)
3. read the results back from the URL input stream.
Here is the servlet:
======== CUT ========
import java.io.*;
import java.net.*;
import java.util.*;
import javax.servlet.*;
import javax.servlet.http.*;
public class SendDoublesServlet extends HttpServlet
{
public void doGet(
HttpServletRequest request,
HttpServletResponse response)
throws ServletException, IOException
{
// Get the fileName parameter
String fileName = request.getParameter("fileName");
if (fileName == null)
throw new ServletException
("No fileName parameter specified");
// Verify that the file exists and is readable
File file = new File(fileName);
if (!file.exists())
throw new ServletException
(fileName + " does not exist");
if (!file.canRead())
throw new ServletException
("Cannot read " + fileName);
// Copy the file to the output data stream
response.setContentType("application/octet-stream");
DataOutputStream out =
new DataOutputStream(
response.getOutputStream());
DataInputStream in =
new DataInputStream(
new FileInputStream(file));
while (true) {
try {
double d = in.readDouble();
out.writeDouble(d);
}
catch (EOFException e) {
break;
}
}
in.close();
out.flush();
out.close();
}
}
======== CUT ========
Here is the applet:
======== CUT ========
import java.applet.*;
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import java.net.*;
public class ReadDoublesApplet
extends Applet
implements ActionListener
{
public void init()
{
setLayout(new BorderLayout());
Panel pnlTop = new Panel();
Label lblFileName = new Label("Enter file name: ");
txtFileName = new TextField(48);
btnOK = new Button("OK");
btnOK.addActionListener(this);
pnlTop.add(lblFileName);
pnlTop.add(txtFileName);
pnlTop.add(btnOK);
add(pnlTop, BorderLayout.NORTH);
txtDoubles = new TextArea(10, 32);
add(txtDoubles, BorderLayout.CENTER);
}
public void actionPerformed(ActionEvent event)
{
txtDoubles.setText("");
try {
// Get the file name from the text field
String fileName = txtFileName.getText().trim();
if (fileName.equals(""))
throw new IOException
("No file name specified");
// Create a URL passing the file name to
// the SendDoubles servlet
String urlString = "/servlet/SendDoublesServlet"
+ "?fileName=" + URLEncoder.encode(fileName);
URL url = new URL(getCodeBase(), urlString);
// Connect to the servlet and open an input
// stream for the output it generates
DataInputStream in =
new DataInputStream(url.openStream());
// Read and print the doubles received
int n = 0;
for (;;) {
try {
double d = in.readDouble();
txtDoubles.append(d + "\n");
n++;
}
catch (EOFException e) {
break;
}
}
txtDoubles.append(
"\n" + n + " doubles read from " + fileName);
}
catch (IOException e) {
StringWriter sw = new StringWriter();
PrintWriter pw = new PrintWriter(sw);
e.printStackTrace(pw);
pw.flush();
txtDoubles.append(sw.toString());
}
}
private Button btnOK;
private TextField txtFileName;
private TextArea txtDoubles;
}
======== CUT ========
-- Phil Hanna
--
Phil Hanna
--On Thursday, February 21, 2002, 5:01 PM -0600 "Jacqueline L. Spiegel - Cohen"
<[EMAIL PROTECTED]> wrote:
> Hi All,
>
> I have an applet that loads in images from a URL.
> It is perfectly happy when it is either running from my
> appletviewer, or if the applet is physically located on
> the same machine as where these images reside. The problem
> is that I need the applet to reside on a different machine from
> where the images reside.
> And yes, it needs to be an applet.
>
> So I have hit the security exception that restricts applets from accessing
> things they shouldnt.. but my question is, isnt there a workaround ?
> I know that you can access any URL, can I somehow grab the image
> which resides in the HTML page some other way (Through the url.net
> library) ? I did try (Image) myurl.getContent() but this did not work
> either.
>
> (And if the workaround is signed applets, can you provide a URL with
> ample hand holding, because it looks a bit scarey to me. )
>
>
> Thanks so very much,
> I genuinely appreciate it.
> Jackie
>
> ===========================================================================
> To unsubscribe, send email to [EMAIL PROTECTED] and include in the body
> of the message "signoff JAVA2D-INTEREST". For general help, send email to
> [EMAIL PROTECTED] and include in the body of the message "help".
===========================================================================
To unsubscribe, send email to [EMAIL PROTECTED] and include in the body
of the message "signoff JAVA2D-INTEREST". For general help, send email to
[EMAIL PROTECTED] and include in the body of the message "help".