Java programming FAQ (2)

Source: Internet
Author: User

36. Q: Why can't sendredirect be transferred to the address of the MMS protocol? Response. sendredirect ("MMS: // missiah.adsldns.org: 9394 ");
A: currently, the Java platform does not implement MMs in protocol. You can use the system attribute Java. protocol. handler. pkgs to see if its value contains MMS, so if you want to redirect to MMS: // host and URL, you only need to generate the client's JavaScript to redirect it.

37. Q: How does one define columns and width in jtable, and how to set table content to align to the right or center?
A:
Tablecolumn Tc = table. getcolumn ("name"); // retrieves the handle column named "name ".
Int currentwidth = tc. getpreferredwidth (); // get the current width of the column
TC. setpreferredwidth (200); // you can specify the current column width.
TC. setmaxwidth (200); // you can specify the maximum width of a column.
TC. setminwidth (50); // you can specify the minimum width of a column.

38. Q: Can batch operations be used in select statements?
A: Batch operations refer to batch update operations, which cannot be used for select operations.

39. Q: Why can't I read files when the JSP path is too deep and the file name is too long?
A: The path length cannot exceed 255. Otherwise, the path cannot be found. This is a task of the job system.

40. Q: How can I prevent the page from being cached?
A:
<%
Response. setheader ("Pragma", "No-Cache ");
Response. setheader ("cache-control", "No-Cache ");
Response. setdateheader ("expires", 0 );
%>

41. Q: Is there an error when jbutton is used in my applet code? Is it because IE does not support swing package? What should I do?
A: jbutton is a basic swing package. You only need to put JDK/JRE/lib/RT. jar in classpath. Do not load any other libraries.

42. Q: I wonder if JAVA supports the MIDI format. If so, how can I convert the wave format to the MIDI format?
A: currently, it does not work. You can check that the MIDI format supported by JMF is read only, while the wave is read/write. MIDI can only be played and cannot be generated.

43. Q: in JSP, how should I prevent users from entering the URL directly into the page?
A:
First, the web server controls all access to a directory through verification.
The second is to add control to the page to be accessed. This is generally implemented using session or request status code.

44. Q:
For example, there is a computing application in the backgroundProgram(This program is very slow in operation and can last for several minutes to several hours, regardless of whether it can be activated.) After the client submits the task, after the server detects that the task is correct, it will send information to the server background program and activate it. The requirements are as follows:
1) First activate the background program to execute this task (for example, the foreground will calculate the CCodeAfter submission, the background program can be called immediately and run)
2) how to display the running process information on the front-end JSP page (due to the long running time, you want to see the information generated during the running process?

A:
It is okay to run a shell to run it in the background, but it is impossible to retrieve the running information, because the HTTP timeout limit cannot always wait for you to run in the background, in addition, if the information needs to be dynamically released in real time, the server push technology is used.

45. Q: the database is of the datetime type. Is the current time inserted to the database?
A:
Java. SQL. Date sqldate = new java. SQL. Date ();
Preparedstatement pstmt = conn. preparestatement ("insert into Foo (time) values (?) ");
Pstmt. setdate (1, sqldate );
Pstmt.exe cuteupdate ();

46. Q: How to remove spaces before and after the string.
Answer: String. Trim ()

47. Q: How does a session access int-type variables?
A:
Session. setattribute ("int", I + "");
Int I = integer. parseint (session. getattribute ("int "));

48. Q: How can I keep two decimal places for the output float data in JavaScript.
Answer: Math. Round (AAAAA * 100)/100.

49. Q: How to call session in bean Mode
A:
You can pass the session object as a parameter to bean.
Define httpservletrequest request and httpsession session in bean;
Then
Session = request. getsession (false );
False: if the session is null, no new session is created.
Pass the session as a parameter. In fact, you only need to pass the request.

50. Q: How can I display a TXT or Word file on a JSP page or servlet in the original format?
A:
In fact, a very simple solution is to explain the text and word in the server's mime, and then generate it with JSP or servlet, the client automatically calls the corresponding program to open your document.
If you want to display it on the page in the original format, instead of calling other programs to open it, you can try the webdev protocol. This is a highlight of Ms. it opens a document in the Web mode, the same as sharing. fully compliant.

51. Q: Why cannot I directly call the object clone method?
A:
This method is protected in the object.
The purpose of defining this method as protected is to know that your method is only a tag in the object, not an implementation. For example

Public Class Object
{

.............
Protected object clone ()
{}
}

Therefore, the clone () method inherited directly cannot be used. To use this method, you need to reload this method and relax the access permission to public, or implement the cloneable interface. but it cannot tell you that it is not actually implemented, so you have to use the protected method to warn you.

52. Q: How to refresh the other page on one page?
A:
The requirement is that these pages must be associated. First, they all have a common top-level frame, that is, a hierarchical page within a frame. Of course, they can be at any level, it is also possible to split frames within the frame. Another window may pop up in the current window. If there is no contact, it is impossible to refresh another page with one page. only a level-1 Reference is required within the frame. for example, write top in a page in the left frame. right. location. reload (); then the page in the right frame named "right" will be refreshed. the dialog box appears. Refresh the subwindow with the name of open and the subwindow uses opener to refresh the main window.

53. Q: How do I write cookies to clients in JSP?
A:
Cookie coo = new cookie (name, value );
Httpservletresponse. addcookie (name );

54. Q: Why does jtextfield1.settext ("aaabbb"); jtextfield2.settext ("aaabbb"); have different font widths?
A: If it is not specified as an equal-width font, the width of each font is different. Therefore, the fontmetrics class is used in Java to retrieve the character width.

55. Q: String KK = application/octet-stream; name = "G:/smbcrack.exe" why does it reach smbcrack.exe?
A:
This should be because the format in the line obtained by parsing the binary stream during the upload is fixed, get the string after name = ", and then remove. Then, all the characters after the last/are taken to form a new string.

56. Q: How to pass the value without refreshing the page?
A:
A page is displayed to select or enter values. After OK, pass the values to the original window and close the window with javascript:
Window. Close (); opener. Focus ();

57. q: There is a string: "ef0c114ea4 ", how to change it to a [0] = 0xef A [1] = 0x0c A [2] = 0x11 A [3] = 0x4e A [4] = 0xa4?
A:
String STR = "ef0c114ea4f ";
Out. Print (STR + "<br> ");
Int L = Str. Length ()/2 + Str. Length () % 2, j = 0, K = 0;
String [] A = new string [l];
For (INT I = 0; I <L; I ++ ){
If (Str. Length ()-j = 1)
K = Str. Length ();
Else
K = J + 2;
A [I] = "0x" + Str. substring (j, k );
Out. Print ("A [" + integer. tostring (I) + "] =" + A [I] + "<br> ");
J + = 2;
}

58. Q: How can I convert an int into a four-byte array?
A:
Int x = 1234567;
Byte [] B = new byte [4];
For (INT I = 0; I <B. length; I ++)
{
B [I] = (x> (I * 8) & 0xff;
}

59. Q: What should I pay attention to when using indexof?
A: The parameter refers to the number (1, 2, 3 ,...) start searching, and the return value refers to the position (0, 1, 2, 3 .......) note that it is counted from zero.

60. Q: How to dynamically Add a button in a Java application?
A:
This involves a component re-painting problem. The component must exist before the display of the Panel. If a panel has already been displayed, can you see it above? However, if button a is added to the same Panel first, if you press it to add butt on B, then if you re-give the entire panel, then a must be re-painted, its event listening will be gone, and of course B will not be added. So if you want to have another panel, add B to this Panel when you press a and redraw the paenl, in fact, a better way is to add B to the Panel first, and the same one will work. set it to true when setvisiable (flase) is pressed as.

61. Q: Book mybook = New Book (bookid); book is a Servlet and an error occurs.
A:
Book is a Servlet and can book mybook = New Book (bookid );
What is the servlet container implemented by myself? Otherwise, can servlet be called by yourself? If the servlet call is actually no different from the EJB 1%, they all inherit or implement some interfaces by themselves. In these parent classes or interfaces, the method of "dealing" with containers is implemented, then the container calls these methods to manage it, so that it can generate instances, pooling, passivation, destruction, and regeneration. therefore, writing in this way is incorrect.

62. Q: How to calculate the result of a given string of 5*(5 + 9)/7?
A:
There are two methods
1. Complete with Stack
2. The simplest method is not programming. If any database is made available, select (5*(5 + 9)/7) from onetable

63. Q: How can I encrypt and decrypt the content of the submitted form?
A:
If you use IE, you can only use the SSL protocol at present, this layer should not be considered, otherwise you only use your own tools to encrypt the transmission, receive and then close friends, as to how to add solutions, if you want to combine it with a recognized system, make it public by using common MD5, Ras, etc.AlgorithmIf you just upload your own solutions, you can add some data as you want, and retrieve the data and delete the data according to the rules, I am sure no one except you can know the decryption method.

64. Q: Why does integer. parseint ("+ 1"); throw a numberformatexception?
A: Because the "+" operation is overloaded in Java, the system cannot determine whether you are using arithmetic addition or character +.
This can be better understood in javascript:
<Form name = "T"> <input name = s value = 1234> </form>
VaR A = Document. T. S. Value + 1;
At this time, a = 12345, because document. T. S. value is used as the string. But var A = Document. T. S. Value-1;
A Is 1233, because the system knows that the-operation must be an arithmetic operation. Therefore, the document. T. S. value is converted to a number.

65. Q: What is the use of hashcode ()? Why do I sometimes need to overwrite the hashcode () method in an object?
A: This is the ID card of the object. How can we identify the object.

66. Q: How to implement a scheduled execution in Tomcat?
A:
It runs automatically when the application starts. Servlet2.3 defines servletlistener, which listens to the start of servlet con text or closes it (which can be configured in the configuration file), and triggers the running of a daemon (Java can be implemented. util. timer or javax. swing. timer ).

67. Q: Can a program output itself?
A: The well-known law of the Confucius paradox means that no program can output itself.

68. Q: Can I convert a character into an ascii code? For example, convert a to 65?
A:
Int A = 'a ';
Out. println ();

69. Q: How can I differentiate the full and half corners of input text?
A: Because I cannot tell the differences between the full-width and half-width characters, I have to cite the full-width symbols.

70. Q: What should I do with the automatic mail program after user registration?
answer:
this mail program does not consider performance, because it is impossible for one person to register within one second, the performance-oriented mail program refers to the one that keeps sending millions of messages in the queue. You can write a program as you like without using javamail. you only need to specify a sending server and use the cocket to connect to its 25 ports. I used a socket to send a letter to the 25 ports of SMTP, as if I had sent something between two neighbors and handed it over directly. I used javamail, the message mechanism is that you sent this item from the post office to your neighbor.

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.