一、实验环境:
1.存在一个Java Project,工程名为JarTest.(将其导出jar文件为test.jar)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; } @Override public String process() { // TODO Auto-generated method stub return this.id+""+this.name; } }
package cn.edu.sdu; public abstract class A { public String name = ""; public abstract String process(); }
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 stub response.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 stub doGet(request,response); } }
<?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>
<%@ 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?