How to access external jar in Java Web and convert the Object type after the instance

Source: Internet
Author: User

To clarify the problem, we assume the following experiment environment, test process, and problems.

I. experiment environment:

1. There is a Java Project named JarTest. (export the jar file as test. jar)
There are two classes. One is abstract parent class A and the other is subclass B. The Code is as follows.

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. There is a Dynamic Web Project in Eclipse named Test. Copy the exported test. jar file to the WebContent directory of the Test project. In addition, this project also includes the following class, Servlet, xml and index. jsp. The Code is as follows.


1) abstract class
package cn.edu.sdu;public abstract class A {public String name = "";public abstract String process();}

2) The Servlet File is responsible for accessing the jar file, creating Class B objects, and converting them into Class.
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 Se RvletException, IOException {// TODO Auto-generated method stubresponse. setContentType ("text/html; charset = gb2312"); PrintWriter out = response. getWriter (); out. println ("

3) configure the servlet web. xml file in the root directory of the 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 file, which is accessed by default on the 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"> 

Ii. Execution Process

1. After running the web, access index. jsp, as shown in the error message.



Click the link to access the servlet, as shown in. If there is an error and the operation fails, B cannot be converted to.



A prompt is displayed on the console interface.



2. If a Test class is added to the project, the Code is as follows.

The execution result is that B can be converted to.

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 = null; uload = new URLClassLoader (new URL [] {new URL (jarPath)}); a = () uload. loadClass (classPath ). newInstance (); System. out. println ("Access jar under Web" ); System. out. println (. getClass (); System. out. println ("successfully created an Object of A");} catch (Exception e) {e. printStackTrace (); System. out. println ("An error occurred while accessing jar to create an Object on the Web! "+ E. toString ());}}}




Iii. Problems

1. Why can I directly execute Java Application and access jar and create B's Object and convert it into A's Object?

2. Why cannot I convert the Object of B Created by jar into an Object of A in A Java Web project?

3. How to Implement the second problem: access the external Jar on the Web and convert the Object created by B to the Object created by?

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.