為了把問題說清楚,假設了如下的實驗環境、測試過程和問題。
一、實驗環境:
1.存在一個Java Project,工程名為JarTest.(將其匯出jar檔案為test.jar)
內有兩個類,一個為抽象父類A,一個為子類B,代碼如下。
package cn.edu.sdu;public abstract class A {public String name = "";public abstract String process();}
package cn.edu.sdu;public class B extends A {public int id;public String getName(){return name;}public void setName(String name){this.name = name;}public int getId() {return id;}public void setId(int id) {this.id = id;}@Overridepublic String process() {// TODO Auto-generated method stubreturn this.id+""+this.name;}}
2.在Eclipse中存在一個Dynamic Web Project,工程名為Test。把上面匯出的test.jar檔案拷貝到Test工程的WebContent目錄下。另本工程還有如下class、Servlet,xml和index.jsp。代碼如下。
1)抽象類別A
package cn.edu.sdu;public abstract class A {public String name = "";public abstract String process();}
2)Servlet檔案,負責訪問jar檔案,建立B類的對象,並轉化成A類。
import java.io.IOException;import java.io.PrintWriter;import java.net.URL;import java.net.URLClassLoader;import javax.servlet.ServletException;import javax.servlet.annotation.WebServlet;import javax.servlet.http.HttpServlet;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;import cn.edu.sdu.A;/** * Servlet implementation class HelloWorldServlet */@WebServlet("/HelloWorldServlet")public class HelloWorldServlet extends HttpServlet {private static final long serialVersionUID = 1L; /** * Default constructor. */ public HelloWorldServlet() { // TODO Auto-generated constructor stub }/** * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response) */protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {// TODO Auto-generated method stubresponse.setContentType("text/html;charset=gb2312");PrintWriter out = response.getWriter();out.println("<html>");out.println("<body>");out.println("動態使用jar");String jarPath = "http://localhost:8080/Test/test.jar";String classPath = "cn.edu.sdu.B";URLClassLoader uload;Object pya;try {A a = null;uload = new URLClassLoader(new URL[]{new URL(jarPath)});a = (A)uload.loadClass(classPath).newInstance();out.println("Web 下訪問jar<br>");out.println(a.getClass());out.println("<br>成功建立了A的Object");} catch (Exception e) {e.printStackTrace();out.println("<font color = \"red\">Web下訪問jar建立Object出現了錯誤!<br>"+e.toString()+"</font>");}out.println("</body>");out.println("</html>");}/** * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response) */protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {// TODO Auto-generated method stubdoGet(request,response);}}
3)配置servlet的web.xml檔案,在WEB-INF的根目錄下。
<?xml version="1.0" encoding="ISO-8859-1"?><web-app xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" version="3.0" metadata-complete="true"> <servlet> <servlet-name>Hello</servlet-name> <servlet-class>HelloWorldServlet</servlet-class> </servlet> <!-- Define the Manager Servlet Mapping --> <servlet-mapping> <servlet-name>Hello</servlet-name> <url-pattern>/hello</url-pattern> </servlet-mapping> </web-app>
4)index.jsp檔案,web下預設訪問的頁面。
<%@ page language="java" contentType="text/html; charset=GB18030" pageEncoding="GB18030"%><!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"><html><head><meta http-equiv="Content-Type" content="text/html; charset=GB18030"><title>Insert title here</title></head><body><a href="hello">連結Servlet</a></body></html>
二、執行過程
1.運行web 後,訪問index.jsp後,如的錯誤提示。
點選連結後,訪問servlet,如。有錯誤,沒有成功,不能將B轉化成A。
console介面的提示如。
2、如果在工程內添加一個Test類,代碼如。
執行結果是可以將B轉化成A。
import java.net.URL;import java.net.URLClassLoader;import cn.edu.sdu.A;public class Test {public static void main(String[] args){String jarPath = "http://localhost:8080/Test/test.jar";String classPath = "cn.edu.sdu.B";URLClassLoader uload;Object pya;try {A a = null;uload = new URLClassLoader(new URL[]{new URL(jarPath)});a = (A)uload.loadClass(classPath).newInstance();System.out.println("Web 下訪問jar");System.out.println(a.getClass());System.out.println("成功建立了A的Object");} catch (Exception e) {e.printStackTrace();System.out.println("Web下訪問jar建立Object出現了錯誤!"+e.toString());}}}
三、問題
1、為什麼Java Application 直接執行,可以訪問jar並建立B的Object後,並轉化成A的Object?
2、為什麼Java Web的項目下,不能將jar建立的B的Object後不能轉化成A的Object?
3、如何?第二個問題,在Web下訪問外部Jar,建立的B的Object轉化成A的Object?