<APPLET CODE="search_book.class" WIDTH=515 HEIGHT=417></APPLET>
解决方案 »
- 知道了对象的名字,能否得到这个对象的实例?
- 一道简单的多线程题
- 机器是日文的,看不懂 ,麻烦安装中文系统的朋友 把这段代码报的错误信息贴一下。(java)
- 运行JSP要设置哪些
- 数据库中datetime型数据运算和java.util.date与java.sql.date类型转化问题 在线等!!!!
- Jrun安装问题,找不到javax.swing.Jdialog.在线等大侠(不问大侠性别\出身\年龄\身材)!
- 在JAVA中Primitive type 和 Wrapper type类型的关系是什么?
- 为什么我用这段HTML语言无法显示Applet控件?
- 问一个比较恶心的问题,关于16进制文本转换成二进制文件
- JTable显示的记录中如何写双击某一行就会弹出窗口的事件?
- jsp response 问题
- java有没有windows里按详细资料浏览文件夹的那种控件
是什么地方导致这样的错误呢??
不知道到底是什么地方不对了,我确实都放在同、一目录下了!!!!!!!!!
package book;
import java.awt.*;
import java.awt.event.*;
import java.applet.*;import java.util.*;
import java.io.*;
import java.net.*;/**
* This applet allows a user to search books and get a list of
* books. The applet communicates w/ a servlet which accesses a database.
*
* Applet and servlet communication is outlined below:
*
* <pre>
* To search the book list:
* 1. The applet sends a GET request to the servlet
* 2. The servlet replies w/ a serialized vector of Book objects
* 3. The results are displayed in the GUI list component.
* </pre>
*
* @author Chad (shod) Darby, [email protected]
* @version 0.9, 31 July 1998
*
*/
public class search_book extends Applet
{ private Panel toolBarPanel;
private Button searchButton;
private Panel statusBarPanel;
private Label statusLabel;
private TextArea statusTextArea;
private String webServerStr = null;
private String hostName = "localhost";
private int port = 8080;
private String servletPath = "myapp/Search_result";
private final int COLUMN_WIDTH = 20;
public void init()
{
this.setLayout(new BorderLayout());
toolBarPanel = new Panel();
toolBarPanel.setLayout(new FlowLayout());
this.add(BorderLayout.NORTH, toolBarPanel); searchButton = new Button("search Books");
toolBarPanel.add(searchButton); statusBarPanel = new Panel();
statusBarPanel.setLayout(new FlowLayout());
this.add(BorderLayout.SOUTH, statusBarPanel); statusLabel = new Label("Status Messages");
statusBarPanel.add(statusLabel); statusTextArea = new TextArea(3, 50);
statusBarPanel.add(statusTextArea);
// register listeners
searchButton.addActionListener(new DisplayButtonActionListener());
// get the host name and port of the applet's web server
URL hostURL = getCodeBase();
hostName = hostURL.getHost();
port = hostURL.getPort();
if (port == -1)
{
port = 8080;
}
log("Web Server host name: " + hostName);
webServerStr = "http://" + "localhost" + ":" + port + "/" + servletPath;
log("Web String full = " + webServerStr);
}
/**
* Get a list of students.
*
* This is accomplished by connecting to the servlet using a GET. The servlet
* will return a vector of student objects.
*
*/
protected Vector getBookList()
{ ObjectInputStream inputFromServlet = null;
Vector bookVector = null;
try
{
// build a GET url string w/ encoded name/value pairs
//
// Send over UserOption=AppletDisplay. The servlet will interpret
// the user option and send back the student list as a serialized vector
// of student objects.
//
String servletGET = webServerStr + "?"
+ URLEncoder.encode("UserOption") + "="
+ URLEncoder.encode("AppletDisplay");
// connect to the servlet
log("Connecting...");
URL Bookservlet = new URL( servletGET );
URLConnection servletConnection = Bookservlet.openConnection();
// Read the input from the servlet.
//
// The servlet will return a serialized vector containing
// student entries.
//
log("Getting input stream");
inputFromServlet = new ObjectInputStream(servletConnection.getInputStream()); bookVector = readBookVector(inputFromServlet);
}
catch (Exception e)
{
log(e.toString());
} return bookVector;
} /**
* Read the input from the servlet. <b>
*
* The servlet will return a serialized vector containing
* student entries.
*
*/
protected Vector readBookVector(ObjectInputStream theInputFromServlet)
{
Vector theBookVector = null;
try
{
// read the serialized vector of students objects from
// the servlet
//
log("Reading data...");
theBookVector = (Vector) theInputFromServlet.readObject();
log("Finished reading data. " + theBookVector.size() + " Books returned");
theInputFromServlet.close();
}
catch (IOException e)
{
log(e.toString());
}
catch (ClassNotFoundException e)
{
log(e.toString());
}
catch (Exception e)
{
System.out.println(e);
}
return theBookVector;
}
/**
* Display the list of books <br>
*
* Iterate over the vector of books and display
* the name, e-mail and company in the multi-column list.
*/
protected void displayBooks(Vector bookVector)
{
Enumeration enum = bookVector.elements();
SearchDialog aBook = null;
String name = null;
String author = null;
String publish = null;
String minprice = null;
String maxprice = null;
String minnum = null;
String maxnum = null; // clear out the list first
}
/**
* Create a row w/ the name, email and company. Format this row such that
* the items are equally spaced.
*/
protected String createRow(String name, String author, String publish,String minprice,String maxprice,String minnum,String maxnum)
{
StringBuffer row = new StringBuffer();
String temp = null;
temp = padString(name);
row.append(temp + " "); temp = padString(author);
row.append(temp + " "); temp = padString(publish);
row.append(temp + " ");
temp = padString(minprice);
row.append(temp + " ");
temp = padString(maxprice);
row.append(temp + " ");
temp = padString(minnum);
row.append(temp + " ");
temp = padString(maxnum);
row.append(temp + " "); return row.toString();
} /**
* Pads a string w/ extra spaces if it is shorter than the COLUMN_WIDTH
*/
他说回复内容太长,让我分开,后面还有一部分applet内容,但可能关系不大了!!!!!!!!!!!!!!
protected String padString(String text)
{
StringBuffer result = null;
if (text.length() < COLUMN_WIDTH)
{
int count = COLUMN_WIDTH - text.length();
result = new StringBuffer(text);
for (int i=0; i < count; i++)
{
result.append(" ");
}
}
else
{
result = new StringBuffer(text.substring(0, COLUMN_WIDTH));
}
return result.toString();
}
/**
* Register a new book.
*
* This is accomplished by showing a registration dialog box. The user
* enters name, e-mail, company, etc. This information is
*/
protected void registerbook()
{
PrintWriter outTest = null;
BufferedReader inTest = null;
Book theBook = null;
// show the dialog for registering books
SearchDialog searchDialog = new SearchDialog(new Frame(), true);
searchDialog.setVisible(true);
// find out if user selected the register or cancel button
if (searchDialog.isRegisterButtonPressed())
{
theBook = searchDialog.getBook();
log("Register button: " + searchDialog.isRegisterButtonPressed());
}
else
{
// the cancel button was pressed, so do nothing and return
log("Register button: " + searchDialog.isRegisterButtonPressed());
return;
}
try
{
// connect to the servlet
log("Connecting to servlet...");
URL bookDBservlet = new URL( webServerStr );
URLConnection servletConnection = bookDBservlet.openConnection();
log("Connected"); // inform the connection that we will send output and accept input
servletConnection.setDoInput(true);
servletConnection.setDoOutput(true);
// Don't used a cached version of URL connection.
servletConnection.setUseCaches (false); // Specify the content type that we will send binary data
servletConnection.setRequestProperty
("Content-Type", "application/octet-stream");
// send the book object to the servlet using serialization
sendbookToServlet(servletConnection, theBook);
// now, let's read the response from the servlet.
// this is simply a confirmation string
readServletResponse(servletConnection);
// now let's refresh the data list
log("Refreshing the list");
Vector bookVector = getBookList();
displayBooks(bookVector);
}
catch (Exception e)
{
log(e.toString());
}
} /**
* Sends a book object to a servlet. The book object is serialized over the URL connection
*/
protected void sendbookToServlet(URLConnection servletConnection, Book thebook)
{
ObjectOutputStream outputToServlet = null;
try
{
// send the book object to the servlet using serialization
log("Sending the book to the servlet...");
outputToServlet = new ObjectOutputStream(servletConnection.getOutputStream());
// serialize the object
outputToServlet.writeObject(thebook);
outputToServlet.flush();
outputToServlet.close();
log("Complete.");
}
catch (IOException e)
{
log(e.toString());
}
}
/**
* Reads a response from the servlet.
*/
protected void readServletResponse(URLConnection servletConnection)
{
BufferedReader inFromServlet = null;
try
{
// now, let's read the response from the servlet.
// this is simply a confirmation string
inFromServlet = new BufferedReader(new InputStreamReader(servletConnection.getInputStream()));
String str;
while (null != ((str = inFromServlet.readLine())))
{
log("Reading servlet response: " + str);
}
inFromServlet.close();
}
catch (IOException e)
{
log(e.toString());
}
}
/**
* Simple logging method for status message in the text area.
*/
protected void log(String msg)
{
statusTextArea.append(msg + "\n");
}
/////////////////////////////////////////////////////////
//
// INNER CLASSES for EVENT HANDLING
//
/**
* When the display button is pressed, we simply get the
* book list and display the books in the GUI.
*/
class DisplayButtonActionListener implements ActionListener
{
public void actionPerformed(ActionEvent event)
{
try
{
// Get a list of the books
Vector bookVector = getBookList(); // Display the list of books
displayBooks(bookVector);
}
catch (Exception e)
{
System.out.println("Error: " + e);
}
}
} /**
* When the "Register" button is pressed then we register
* the book in the database.
*/
class RegisterButtonActionListener implements ActionListener
{
public void actionPerformed(ActionEvent event)
{
registerbook();
}
}}
应该这样
<APPLET CODE="book.search_book.class" WIDTH=515 HEIGHT=417></APPLET>网页在book的上一层目录试试
的错误信息!!!!