1: Applet中访问Servlet
ArrayList<MagcaInfo> dbMagcaInfo = (ArrayList<MagcaInfo>) Applet2Servlet.OpenServlet("http://localhost:7001/MediatorServlet");
2:从ObjectInputStream中读取Servlet返回的对象信息
public class Applet2Servlet {
public static Object OpenServlet(String sServlet) throws MalformedURLException {
try {
URL url = new URL(sServlet);
URLConnection urlConnection = url.openConnection();
ObjectInputStream inputStream = new ObjectInputStream(url.openStream());
return inputStream.readObject();
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
}
3:Servlet中从数据库读取信息,写入ObjectOutputStream中。
public class MediatorServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
doPost(req, resp);
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
MagcaInfoService magServices = (MagcaInfoService) Framework.getEngine().getContainer().getComponent("magcaInfoService");
List<MagcaInfo> list = magServices.findAll(MagcaInfo.class);
//输出JAVA对象
ObjectOutputStream os = new ObjectOutputStream(resp.getOutputStream());
os.writeObject(list);
// getServletConfig().getServletContext().getRequestDispatcher("topview.jsp").forward(req, resp);
}
}