Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to realize the answers to Java frequently asked questions

2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article analyzes "how to implement answers to Java frequently asked questions". The content is detailed and easy to understand. Friends who are interested in "how to achieve answers to Java FAQ" can follow the editor's idea to read it slowly and deeply. I hope it will be helpful to you after reading. Let's follow the editor to learn more about "how to achieve answers to Java FAQ".

Java FAQ Collection (transferred) [@ more@] Q:

How do I set the environment variable for Java 2 (JDK1.2)?

A:

After Java 2 is installed, you need to set the PATH and JAVA_HOME environment variables. Unlike JDK1.1, after setting the JAVA_HOME environment variable, JVM will automatically search the system class library and the user's current path.

The Java 2 environment variable is set as shown in the following example:

Solaris platform: the installation path of setenv JAVA_HOME Java2

Setenv PATH $JAVA_HOME/bin:$ {PATH}

Windows platform: the installation path of set JAVA_HOME=Java2

Set PATH=$JAVA_HOMEbin;%PATH%

Q: which Java integrated development tools support Java 2?

A:

Popular Java integrated development environments, such as Visual Cafe of Inprise's JBuilder,Symantec and PowerJ of Sybase, all support Java 2. 0.

Q:

If there is an error running Java applet in a Netscape or IE browser, how do I determine the error range?

A:

When java applet runs in a browser, it uses the default JVM of the browser itself. And different browsers have different levels of support for JDK. Therefore, there is an error in running Java applet in Netscape or IE browsers. It is recommended to use appletviewer, a tool provided by JDK, or Hotjava browser of Sun to test the applet to make sure that the error is related to the browser.

If applet runs well in appletviewer or Hotjava, the error occurs because the browser is not fully compatible with JDK. At this point, the solution can be to use a Hotjava browser or install Sun's Java Plugin.

If an error occurs when applet is running in a Hotjava browser or appletviewer, you should check the applet program according to the error prompt.

Q:

Why do Chinese characters sometimes appear garbled when inserting data into or extracting data from a database with JDBC?

A:

The implementation of this problem is usually related to the implementation of each JDBC driver. At present, most JDBC driver use local coding format to transmit Chinese characters. For example, Chinese characters "0x4175" will be converted to "0x41" and "0x75" for transmission. So we need to convert the characters returned by JDBC driver and the characters to be sent to JDBC driver.

When inserting data into the database with JDBC driver, you need to convert Unicode to native code; first. When JDBC driver queries data from the database, you need to convert native code into Unicode. The following is the implementation of these two transformations:

String native2Unicode (String s) {

If (s = = null | | s.length () = = 0) {

Return null

}

Byte [] buffer = new byte [s.length ()]

For (int I = 0; I s.length (); iTunes +) {if (s.charAt (I) > = 0x100) {

C = s.charAt (I)

Byte [] buf = ("" + c) .getBytes ()

Buffer [jacks +] = (char) buf [0]

Buffer [jacks +] = (char) buf [1]

}

Else {

Buffer [jacks +] = s.charAt (I)

}

}

Return new String (buffer, 0, j)

}

In addition to using the above two methods, some JDBC driver will not need the above two methods if they set the correct character set properties on jdbc driver Manager.

Q:

When using Servlet to process the http request and generate the returned HTML page, how to make the Chinese characters in the HTML page display properly?

A:

The javax.servlet.http.HttpResponse class is used to generate a return page. An instance of ServletOutputStream can be obtained through the method getOutputStream () defined by HttpResponse, so that the user can use the ServletOutputStream.write method to write the content of the returned page to the output stream. But ServletOutputStream uses the default encoding. If you want to make the Chinese characters in the returned page display normally, it is best to specify the character encoding used. You usually need to construct an OutputStreamWriter, as shown in the following routine:

Public void doGet (HttpServletRequest req, HttpServletResponse res)

Throws ServletException, IOException

{

Res.setContentType ("text/html")

ServletOutputStream out = res.getOutputStream ()

OutputStreamWriter ow = new OutputStreamWriter (out, "GB2312")

Ow.write ("this is a test")

Ow.flush ()

Ow.close ()

}

Q:

How do I set up the CLASSPATH of Java WebServer to include the user's class file?

A:

There are two ways to set the CLASSPATH environment variable of Java WebServer so that the user-written Servlet can call the user's class file.

Put the user's class file in the JavaWebServer_Dir/classes directory, and when JavaWebServer starts, the classes directory is automatically added to the CLASSPATH environment variable.

Modify the httpd.nojre file to add the path name of the user class file to the CLASSPATH environment variable.

Q:

Why is it so slow to use Naming.lookup to get remote RMI objects on Windows platforms?

A:

The incorrect network setting of the machine is likely to cause this problem.

RMI uses the Java network class, especially the java.net.InetAddress class, which will query the host name of TCP/IP, including the mapping of IP address to host name and the mapping of host name to IP address. On the Windows platform, this query function is realized by the local Windows Socket library. Therefore, the delay occurs in the Windows library, not in RMI.

If your machine is set to use DNS, the problem is usually that the DNS server cannot find the hostname, and the delay you find is the delay of the DNS query. Try adding all hostnames / IP addresses involved in RMI communication to the local file winntsystem32driversetchosts or windowshosts. The format is as follows:

IP address hostname

This setting should significantly reduce the time spent on the query.

Q: when writing Java application, how do I set up proxy information so that I can access external websites?

A:

If you visit an external website in java application, you should first set the proxy information. The sample code is as follows:

Import java.util.properties

.

Properties sys = System.getProperties ()

Sys.put ("proxySet", "true")

Sys.put ("proxyHost", "myHTTP.proxyserver.com")

Sys.put ("proxyPort", "80")

System.setProperties (sys)

U = new URL (website)

Connect = (HttpURLConnection) u.openConnection ()

.

Q: the list data of the Swing component JList has been modified. How can I tell JList to change the display?

A:

The JList component has a separate display mode ListModel to represent the display data of JList.

After JList is created, the value of JList data elements and the number of data elements can be changed dynamically.

JList observes changes in data in its data schema, ListModel. Therefore, the correct implementation of a ListModel should notify the listener of the event each time the data changes.

When using the constructor JList (Object []) to create an instance of JList, the system will automatically create an instance of DefaultListModel to store the display data of JList. You can call the simple methods defined in DefaultListModel to dynamically modify the data of JList, such as removeElementAt (index), addElement (Object) and so on. When DefaultListModel modifies the data, it will notify JList of the change.

Q:

How to implement a modal dialog box in Java applet?

A:

The key to implementing a modal dialog in Java applet is to specify a correct parent window for the dialog when creating a dialog. Because Applet is a subclass of the Panel class and cannot be used as the parent window of the dialog box, we should first obtain the window where applet is located as the parent window of the modal dialog box. The sample code is as follows:

.

Dialog d = new Dialog (getParentWindow (comp), title)

/ / comp is any component on applet

....

Public void getParentWindow (Component compOnApplet,String title) {

Container c = compOnApplet.getParent ()

While (c! = null) {

If (c instanceof Frame)

Return (Frame) c

C = c.getParent ()

}

Return null

}

Q: how do I display another HTML page in Java applet?

A:

The AppletContext related to the applet can be obtained by the java.applet.Applet.getAppletContext () method, and the AppletContext.showDocument (URL) method can make the browser where the applet is located display another web page.

Q:

Can the signed applet implemented in JDK run in Netscape or IE?

A:

Signature applet implemented in JDK cannot be run in Netscape or IE, but can be run in Hotjava browsers.

Different browsers provide different signature applet mechanisms, such as Netscape provides zigbert tools and Capability API, while IE requires the use of CAB files. However, signature applet generated by Netscape tool or signature applet generated by IE cannot be run in other browsers.

If you want to make the signature applet generated by JDK run in Netscape or IE, the solution is to install Java Plugin in Netscape or IE, then the signature applet implemented in JDK can run in both browsers.

Q:

C library can be called from Java application with JNI technology, but how to make this C library call another C library?

A:

If a C library C1 called by Java still needs to call another C library C2, then the library C2 should be linked when compiling C1, as follows (Solaris platform):

Write the Java file that calls the C library and compile it.

Javac java file name

Generate C program header file

Javah-jni java file name (without suffix .java)

Write the C program C 1.c called by Java and C 2.c called by C 1, and compile it.

Cc-G-Iinclude path name C2.c-o libC2.so

Cc-G-Iinclude pathname-lC2 C1.c-o libC1.so

Set environment variabl

Path where setenv LD_LIBRARY_PATH libC1.so,libC2.so is located

: ${LD_LIBRARY_PATH}

Run the java application

Q:

In the Java language, how do I list all the drive names in the PC file system?

A:

In Java 2, the method listRoots () is added to the File class in the java.io package to achieve this function.

The answers to frequently asked questions about how to implement Java are shared here. I hope the above content can improve everyone. If you want to learn more knowledge, please pay more attention to the editor's updates. Thank you for following the website!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report