Java problem set 2

xiaoxiao2021-03-05  47

Author: Small cat Source: chinaunix.net

Q: Which Java integrated development tools support Java 2? A: At present, the popular Java integrated development environment, such as Inprise's JBuilder, Symantec's Visual Cafe, Sybase's Powerj supports Java 2. Q: If you are in the Netscape or IE browser An error occurred while running a Java applet, how to determine the error range? A: When the Java Applet is running in the browser, use the browser itself's default JVM. Different browsers have the same support for JDK. Therefore, there is an error in the Netscape or IE browser, it is recommended to use the tool AppletViewer or Sun's HotJava browser provided by JDK to test the applet to determine the error of the error is related to the browser. If applet is in AppletViewer Or run everything in Hotjava, the error is caused by the browser is not fully compatible with JDK. At this point, the solution can be Java Plugin using the Hotjava browser or install Sun. If Applet is in the Hotjava browser or AppletViewer In the middle run, the applet program should be checked according to the error message. Q: When using JDBC into the database or extracts data from the database, the Chinese characters will be displayed as garbled? A: This problem is usually The implementation of each JDBC Driver is related. At present, most JDBC Driver use local coding format to transmit Chinese characters, such as Chinese characters "0x4175" will be transferred to "0x41" and "0x75" to transmit. So we need to return to JDBC Driver And send the character to the JDBC Driver to convert. When you use JDBC Driver to insert data into the database, you need to transfer Unicode to Native Code; when JDBC Driver query data from the database, you need to convert Native Code to Unicode. The implementation of these two conversions is given: 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 (); i ) {if (S.Charat (i)> = 0x100) { C = S.Charat (i); byte [] buf = (" c) .GetBytes (); buffer [j ] = (char) buf [0]; buffer [j ] = (char) buf [1] } else {buffer [j ] = S.Charat (i);}} Return New String (Buffer, 0, J);

} In addition to using two methods, some JDBC Driver If the JDBC Driver Manager sets the correct character set properties, the above 2 methods are not needed. Q: When using servlets to process HTTP requests and generate returned HTML pages How to make the Chinese characters in the HTML page you can display properly? A: javax.servlet.http.httpResponse class is used to generate return pages. Methods defined by httpResponse GetputStream () can get instances of servletOutputStream, so users can take servletoutputstream The .write method writes the contents of the return page in the output stream. But servletOutputStream uses the default encoding method. If you want to make the Chinese characters in the return page, it is best to specify the character encoding used in the specified manner. Usually need to construct a OutputStreamWriter, routine follows: 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 the test"); OW.Flush () ;OW.Close ();} Q: How do I set the ClassPath of Java WebServer to include the user's class file? A: Yes Two ways can set the ClassPath environment variable of Java WebServer to use the servlet written by the user to call the user's class file. • Place the user's Class file in the javawebser_dir / classes directory, the classes directory is automatically added when the Java Webserver starts To the classpath environment variable. · Modify the httpd.nojre file, add the path name where the user class file is added to the ClassPath environment variable. Q: Why use naming.lookup on the Windows platform to get remote R MI objects will be very slow? A: The machine's network settings are incorrectly probably caused by this problem. RMI uses Java network classes, especially Java.Net.Netaddress classes, which will query TCP / IP host name, Including the IP address to the host name mapping and host named IP address. In the Windows platform, this query function is implemented by the local Windows Socket library. So delay is in the Windows library, not RMI If your machine is set to use DNS, the problem is usually the DNS server can't find the host name, and the delay you find is the delay of the DNS query. Please try to add all hostname / IP addresses involved in the RMI communication. To the local file WinntSystem32driverstchosts or Windowshosts. The format is as follows: IP address hostname should significantly reduce the time spending the query. Q: How to set up proxy information when writing Java Application, in order to access the external website? A: If you are Access to external websites in Java Application, first set up proxy information, sample code 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); connection = (httpurconnection) u.openconnection (); ..... Q: Swing component jlist's list data modified, how to inform JList change display? A: Jlist component has a separate display mode ListModel To represent Jlist's display data. After jlist creation, the value of the JList data element and the number of data elements can be dynamically changed. Jlist Observe the changes in data in its data mode ListModel. Therefore, a ListModel's correct implementation should be at each time When the data changes, the listener of the event is notified. When using the constructor jlist (Object []) created a JList's instance, the system will automatically create an instance of the DefaultListModel to store the display data of Jlist, which can call the defaultListModel to be used. Method To dynamically modify the data of JLIST, such as RemoveElementat (INDEX), ADDELEMENT (Object), etc. DEFAULTLISTMODEL will notify Jlist for data changes while modifying data. Q: How do I implement a mode dialog in the Java Applet? Answer : The key to implementing the mode dialog box in the Java applet is to specify a correct parent window for the dialog when you create a dialog. Because the applet is the subclass of the Panel class, it is not a parent window as a dialog, so First get the window where Applet is located, the parent window of the Mode dialog. Sample code is as follows: ..... Dialog D = New Dialog (getParentWindow (Comp), title); // CoMP is any component on the applet .... Public void getParentWindow (Component Componpplet, String Title) {Container C = ComponApplet.getParent (); While (C! = NULL) {if (c = c.getParent (frame) C; c = c.getParent } Return NULL;

} Q: How to display another HTML page in the Java applet? A: You can get the appletContext () method to make the applet to browse the applet () method through the java.applet.applet.getappletcontext () method. Displays another web page. Q: Can I run with JDK implementation, can I run in Netscape or IE? A: The signature applet implemented with JDK can not be run in Netscape or IE, but can run in the Hotjava browser. Different browsers provide different signature Applet mechanisms, such as Netscape provides Zigbert tools and Capability APIs, while IE requires using a CAB file. However, whether it is the signature applet generated by the Netscape tool, or the signature APPPlet generated by IE, You cannot run in other browsers. If you want to make the JDK generated signage Applet to run in Netscape or IE, the solution is to install Java Plugin in Netscape or IE, you can use the JDK-implemented signature applet can be in these two The browser is running. Q: Use JNI technology can call the C subscription from the Java application, but how to make the C library can call another C subscription? A: If a C library C1 called by Java is still needed Call another C library C2, then connect the library C2 when compiling C1, the steps are as follows (Solaris Platform): • Write the Java file calling the C library, and compile .javac java file name · Generate C program header file Javah -JNI Java file name (without suffix .java) • Written C-program C1.c called by Java, and C2.c called by C1, and compiled .cc -g -iinclude path name c2.c -o libc2. SOCC -G-INCLUDE path name -lc2 c1.c -o libc1.so • Setting environment variable Stenv LD_Library_path libc1 • Run Java application Q: How to list all drive names in the PC file system in the Java language? A: In Java 2, the File class in the Java.IO package adds method Listroots () to implement this function. Q: Why does Runtime.exec ("ls") does not have any output? A: Call the runtime.exec method Generate a local process and return an instance of a process subclass that can be used to control the process or acquire the process. Due to the child process created by the runtime.exec method, there is no end or console, so the child The standard IO of the process IO (such as stdin, stdou, stderr) is redirected to its parent process via process.getoutputstream (), process.getinputStream (), process.GeterrorStream () method is redirected to its parent process. Users need to use these Stream to sub-process Enter the output of the sub-process. So correct the runtine of Runtime.exec ("LS") is as follows: try {process = runtime.Getruntime (); inputstreamReader Ir = newinputstreamReader (process.getinputstream) ); Linenumberreader input = new linenumberreader (IR); string line; while ((line = INPUT.READLINE ())! = Null) System.Out.println (line);} catch (java.io iexception e) {system .err.println ("ioException" E.getMessage ());

} Q: How to generate a signature applet so that the applet can access local resources? A: In JDK1.1, you can use the javakey command to generate public key, private key, certificate, and signature JAR file. For details, please refer to: Java. Sun.com/security/usingjavakey.html and Java 2 make a relatively large improvement on the signature mechanism, allowing users to set security more flexibly. Java 2 provides three tools: KeyTool, PolicyTool, and Jarsigner to implement signing Applet. For example, , Joe has written a signature applet: signedapplet.java, then generate a simple signature applet is as follows: // Generate a key, the key is not named Joe, the password is Sign12, stored in the keystore Joestore Keytool -Genkey - alias joe -keypass sign12 -keystore joestore // packaged SignedApplet.class and related files into a jar file jar cvf SignedAppletDemo.jar // use keytool to generate self-signed certificate generated signature applet (jar file) jarsigner -keystore joestore -signedjar joe .jar Signedappletdemo.jar Joe // Output Self-signed Certificate from KeyStore to file keytore-ketport -keystore joestore -alias joe -file joe.cer, you need to secure Joe securely through the following steps Written Signature Applet: // Get Joe's certificate and read it into the SusanStore in the keystore, Keytool-Ilias Joe -File Joe.Cer -KeyStore SusanStore // Run PolicyTool Produces Policy files that meets Susan requirements PolicyTool / / Run with AppletViewer or install Java Plugin in your browser. For the deployment of the signature applet in Java Plugin, please refer to the following page: java.sun.com/security/signexample12/ Note: The above example is simple , Use the self-signed certificate produced by KeyTool. In fact, users can also use Keytoo L-Certreq applies for an electronic certificate to the commercial CA Center. Q: If you write Object in an additional manner through the ObjectOutputStream, you will generate StreamCorruptedException? A: Use the default Serialize to implement When an ObjectOutputStream constructor and an ObjectInputStream constructor must correspond one by one. The constructor of ObjectOutputStream is written to the output stream, while ObjectInputStream first reads this identity head. So, multiple times to append when the writing object file, the file header will contain a plurality of identification. when it is used to deserialize the ObjectOutputStream ObjectInputStream, StreamCorruptedException generated. one solution is a class constructor ObjectOutputStream and covering the writeStreamHeader () method. The written WriteStreamHeader () method should determine whether to write Object to the file for the first time, Carbonyl?

If you write Object when you add an additional manner, you should call the ObjectOutputStream.Reset () method. Q: The Serialization class is a flow-oriented, how should you write objects to a random access file? A: Currently, there is no direct method to write objects into random access files. But you can use the ByteArray input / output stream as an intermediary to read or read or read the word from the random access file. Section, and you can use byte stream to create object input / output streams for reading and writing. Need not to note that in the byte stream, you want to include a complete object, otherwise an error will occur when reading and writing objects. For example, Java.io.byteArrayOutputStream can be used to get the byte stream of ObjectOutputStream, which can be obtained from the BYTE array and write it into a random access file. Instead, we can read the byte array from the random access file, using it ByTearRayinputStream can be constructed to read ObjectInputStream to read objects. Q: When running RMI applications, you can not manually start the name service RMIRGISTRY, but start from the program? A: Yes. Java.rmi package provides classes Java.rmi.Registry.locateREGISTRY, is used to get a name service or create a name service. Call the locateregistry.createRegistry (int port) method can create a name service at a particular port, so that users do not need to manually start RMIREGISTRY. In addition, LocateRegistry.getRegistry (String Host, INT port) method can be used to obtain a name service. Q: How should I set the printing attribute of the printer name when using the class printjob? A: Use the following method to get PrintJob instance to control the print operation: Toolkit .GetPrintJob (Frame F, String Jobtitle, Properties Pro) So settings for printing properties can be implemented by setting Prop properties, printing properties include: aff.print.destination: can be "printer" or "file" AWT.Print .printer: Printer Name AWT.PRINT.FileName: Print file name AWT.PRINT.NUMCOPIES: Costs AWT.PRINT.Options: Print command print option AWT. Print.orientation: Printing direction, can be "portrait" or "landscape" aff.print.papersize: paper size, can be "letter", "legal", "executive" or "A4"

转载请注明原文地址:https://www.9cbs.com/read-32999.html

New Post(0)