JSP interview Problem set

Source: Internet
Author: User
Tags call back comparable

1. Q: How do I write a script on Linux, or a script program on Windows, in Java and JSP?

Answer: System.getruntime (). EXEC ("Bash < aaa.sh");

2. Q: What is used in Java to denote double quotes

Answer: "\" "

3. Q: How do I start another thread in my JSP program?

A: The JSP itself is a standalone thread rather than a CGI-independent process.

General: Thread T = new Thread ("your object \ \");

T.start (); it'll be all right.

This object requires you to implement the Runnable interface or inherit the thread.

4. Q: How does JSP get the IP address of the client?


Take a look at the API documentation for each webserver, usually with its own, resin and tomcat

5. Q: Program termination and output termination

Answer: Program abort: return;

Output abort: Out.close (); This sentence is equivalent to ASP Response.End

6. Q: How do I get the URL of the previous page in JSP?

Answer: Request.getheader ("Referer");

7. Q: How does the page expiration feature of the Web page be submitted?

Answer: Response.sethader ("Expires", "0″");

8. Q: How to know the name of a page that has been opened in a JSP Web page

Answer: Request.getrequesturi ();//File name

Request.getrequesturl ();//All URLs

9. Q: After submitting the form, the validation does not pass, return to the submission page, how to keep the data in the original submission page?

A: JavaScript's Go (-1) can re-display the contents of the previous page's form, but the password domain does not

10. Q: How do I get header information for HTTP?

Answer: Request.gethader (headername);

11. Q: What is the difference between:&& and &?

A:& is a bitwise operator that indicates that bitwise AND operation,&& are logical operators, representing logic and (and).

12. Q: Show * in a period of sinusoidal curve


public void Paint (Graphics g)


for (int i=0;i<200;i++)

g.DrawString ("*", I, (int) (Math.sin (i) *20) +50);



13. Q: After multiplying the floating-point number, the result is not accurate, such as 100.0 * 0.6 result equals 60.0004

A: This is not called an error, and float and double are implemented in this way. If you want to calculate accurately, Java provides a strictfp, which is calculated in accordance with the IEEE 754 standard. The normal float and double are the additional precision or range provided by the ground platform floating-point format or hardware.

14. Q: How do I get the position of the current cursors?

A: int row = Rs.getrow () is the current number of pointers, there are isfrist (); Isbeforefist (); Islast (); Isafterlast (); You can test for where the method name says

15. Q: The form was successfully submitted, point back to show the page expires

Answer: In the inside to code

Or in the form page, add


Response.setheader ("Pragma", "No-cache");

Response.setheader ("Cache-control", "No-cache");

Response.setdateheader ("Expires", 0);


16. Q: Simple understanding of interfaces

A: interface for specification, for example I have defined a method in the interface:

GetData ()

This is used to fetch data from different databases, that is, JDBC implementation for the user, I do not know how each database is done, but I know how they want to implement this interface there must be a way for me to invoke. So Sun put this interface to each database developer, let them do it themselves. But why not use the interface for inheritance, because inheritance can only inherit from one class, and the interface can implement multiple, that is, I implement the subclass has a number of well-defined interface functions. This is just a simple understanding, so when you dig deeper into the abstraction, you know why it is abstracted to the interface when it is abstracted to an abstract class.

17. Q: How do I write a Cancel button (how do I go back to the previous page, like the Back button on the toolbar)?

A: JavaScript puts each browse location to a stack, which is the history, and if you want to go back to the first few pages it will do a few pop operations, the last pop out of the location to you. This is the principle that JavaScript implements History.go (-X).

18. Q: What is a callback?

A: Simply put, call back is not to let you listen to who has done something, but who has done something on the report to you. This is the idea of calling back. There are too many examples, the AWT event, the swing event model, and so on. There are also multiple threads, if you want to control the number of threads, you cannot always query whether each thread ends, and let the thread tell the main threads themselves at the end of each thread that I am finished, and you can open new threads.

19. Q: A brief introduction to the CompareTo method

A: The CompareTo method is the comparable interface must implement the method, as long as the implementation comparable can be used Arrays.srot () to sort the runnable interface as a run can be the same as thread ().

20. Q: How can I retrieve a page from another Web server and then store the HTML code of the retrieved Web page in a variable and return it.

A: This is a simple web robot implementation, using the URL class to capture the content from the Web page, and then write a parser to find out the new URL, and continue to recursively go on the line.

21. Q: How to get keyboard input in an applet

A: application's system.in is the standard input for the current system, and the applet cannot read the standard input of the current system (client) for security reasons, and can only get key values from its root component's events, such as keyboard events.

22. Q: How to calculate the time spent in code execution?

A: The code starts to take the time, after the end takes the time, subtracts

Long T1 = System.currenttimemillis ();

Your code

Long t2 = System.currenttimemillis ();

Long time = T2-T1;

23. Q: How can I get a contenttype of a file in a program?

Answer: url u = new URL ("File:///aaa.txt");

URLConnection UC = U.openconnection ();

String s = uc.getcontenttype ();

24. Q: Is the connection pool used to establish many connection pools, or is it a connection pool with multiple connections?

A: Multiple pooling occurs only when the object source is different, and if you only connect one data source, never use more than one pool of connections. Therefore, the initialization of the connection pool must be static, and should be before the construction of the object, that is, only when the class load, there should be no other time to generate a new pool of connections.

25. Q: How do I install JavaMail?

A: Download two packages, one is JavaMail package, the other is JAF package. After downloading the two packages directly to add to the classpath.

26. Q: How to lock the address in the address bar?

A: Turn off the accessible directory indexing option for your server, and any server has a conf file that has this option.

27. Q: How do I get environment variables in Java? For example: TEMP = C:\TEMP?

A: String sss = System.getproperty (key)

28. Q: How do I round up and keep two decimal places after the decimal point?

Answer: Import java.text.*;


NumberFormat nf=numberformat.getnumberinstance ();

Nf.setmaximumfractiondigits (2);

Nf.setminimumfractiondigits (2);

Nf.format (numb);

29. Q: How do applets and form communicate?

Answer: The obtained parameters are passed to Param


String xxx = request.getparameter ("xxx");



30. Q: What is java-plug-in?

Answer: Java Runtime Environment Plug-in. Used to run Java programs. No special settings are required. is equal to the JVM inside your machine.

31. Q: How does the Web above connect to the previous Excel table?

A: Define the page to be contenttype= "application/vnd.ms-excel", let the page open in Excel form. It can also be opened in Word form: Application/msword.

32. Q: How can I avoid textarea word limit?

Answer: Is the default method of using the form of the reason, if nothing is written by default is get to use post can, in the form defined mothod= "POST".

33. Q: Why is the inserted database of Chinese, still garbled?

A: This should be seen from the environment, can show that your JSP engine is not a problem, but when writing to the database your JDBC can handle Chinese, different versions of the same company JDBC Support Chinese and do not support the case of Chinese, resin's own MySQL JDBC does not support, MM support, And do you have a database type that supports Chinese? General support for Char, but whether to store double-byte code in binary

34. Q: What is the difference between show () and Setvisibel () for Jframe,hide ()?

Answer: SetVisible () inherits from component, while Hide (), show () inherits from the window.

Makes the Window visible. If the Window and/or its owner is not yet Displa yable, both is made displayable. The Window is validated prior to being made visible. If t he Window is already visible, this would bring the Window to the front. The difference is in this.

36. Q: Why can sendredirect not go to the address of the MMS protocol? Response.sendredirect ("Mms://missiah.adsldns.org:9394″);

A: The Java platform is currently implemented in the Protocol does not have MMS, you can take the system Properties java.protocol.handler.pkgs see if there is no MMS in its value, so if you want to redirect to mms://host such and URL, Only the JavaScript that generates the client lets it redirect

37. Q: How to define each columns and width in jtable and how to set the contents of the table by doing right or center?

Answer: TableColumn TC = table.getcolumn ("name");//Get column named "name" handle

int currentwidth = Tc.getpreferredwidth (); Gets the current width of the column

Tc.setpreferredwidth (200); Set the current column width

Tc.setmaxwidth (200); Set the maximum width of the column

Tc.setminwidth (50); Set the minimum width of the column

38. Q: Are batch operations available for SELECT statements?

A: Batch operations actually refer to a batch update operation, which is absolutely impossible for a select operation.

39. Q: Why is the JSP path too deep for the file to be read if the filename is too long?

A: path cannot exceed 255 length, otherwise it will not be found. This is the operating system.

40. Q: How do I make the page not keep the cache?



Response.setheader ("Pragma", "No-cache");

Response.setheader ("Cache-control", "No-cache");

Response.setdateheader ("Expires", 0);


41. Q: What should I do if I use JButton in my applet code because IE does not support swing package?

A: JButton is swing basic package ah, just put Jdk/jre/lib/rt.jar on classpath on the line. Do not load any other libraries.

42. Q: Do not know whether Java supports MIDI format, if supported, how should wave format to be converted to MIDI format?

A: Not at the moment, you can see the JMF three version of the MIDI format support is read only, and wave is Read/write,midi can only play, not generated.

43. Q: In the JSP to prevent users to enter the URL directly into the page, what should be done?

A: First, from the Web server control, all access to a directory to pass validation.

The second is to add control to the page you want to visit. This general session, you can also use the request status Code implementation

44. Q:

For example, there is a computing application in the background (this program is slow to operate, it can last a few minutes to a few hours, regardless, mainly to activate it), the client after the task is submitted, the server will send the message to the server daemon and activate it. Requirements are as follows:

1) First activate the daemon, let it perform this task (for example, the foreground will calculate the C code on the post, the daemon program can be called immediately and run it)

2) How to display the running process information in the foreground JSP page (because of the long running time, want to let the customer see the information generated during the operation) how to complete?

A: Live is OK, run a shell Let it run the background of the line, but it is not possible to remove the running information, because the HTTP timeout limit is not always waiting for you to run in the background, and the information should be dynamically launched in real time to use the server push technology.

45. Q: Is the database a datetime type, inserting the current time into the database?

Answer: java.sql.Date sqldate = new Java.sql.Date ();

PreparedStatement pstmt = conn.preparestatement ("INSERT into Foo (time) VALUES (?)");

Pstmt.setdate (1,sqldate);

Pstmt.executeupdate ();

46. Q: How to remove the space before and after the string.

Answer: String.Trim ()

47. Q: How does the session access variables of type int?

Answer: Session.setattribute ("int", i+ "");

int i = Integer.parseint (Session.getattribute ("int"));

48. Q: How to keep two decimal places for the output float type data in JavaScript.

Answer: Math.Round (aaaaa*100)/100.

49. Q: How to invoke session in bean species

Answer: You can pass the session object as a parameter to the bean

Define the HttpServletRequest request in the bean; HttpSession session;

Then session = Request.getsession (false);

False to not create a new session if the session is empty

Pass the session as a parameter. In fact, just pass in the request to

50. Q: How can i display txt or Word files in the original format on a JSP page or a servlet?

A: In fact, a very simple solution is to point to the mime of the server to the text and word interpretation, and then build it with a JSP or a servlet, the client will automatically call the corresponding program to open your document.

If you want to display in the original format on the page, instead of calling other programs open then you can try the WebDev protocol, it is a highlight of Ms. It is a web-based way to open documents, and share the same. Fully compliant with the requirements.

51. Q: Why can't the Clone method of object be called directly?

Answer: This method is protected in object

Why this method is defined as protected, which is a compromise, it is intended to know that your method in object is just a token, not an implementation, such as

public class Object



Protected Object Clone ()



So the direct inheritance of the Clone () method does not do anything when you want to use this method to overload this method and relax access to public, or implement the Cloneable interface. But it's not going to tell you that it didn't really come true, it had to be warned by the protected method.

52. Q: How do I refresh another page in one page?

A: The requirements are these pages must have a correlation, one is that they have a common top-level frame, that is, a frame of the hierarchical page, of course, can be any level, frame and then sub-frame can also, another may be the current window pop-up window, if there is no contact, it is impossible to use a page refresh another page. Within the frame, as long as a first-level reference is OK. For example, Top.right.location.reload () is written on a page in the left frame, and the page in the right frame is refreshed. pops up, refreshes the subwindow with the name of the open, and the child window refreshes the main window with opener

53. Q: How do I write cookies to the client in JSP?

A: the cookie COO = new Cookie (name, value);

Httpservletresponse.addcookie (name);

54. Q: Why Jtextfield1.settext ("aaabbb"); Jtextfield2.settext ("aaabbb"); Do you get a different font width?

A: That is, if you do not specify equal-width fonts, the width of each font is different. Therefore, the FontMetrics class is used in Java to take the character width.

55. Q: String kk=application/octet-stream; Name= "G:/smbcrack.exe"; How to get SMBCrack.exe?

A: This should be the parse upload when the binary stream gets the format is fixed, take to Name= "after the string, and then"; Then take all the characters from the last/later to make up a new string.

56. Q: How do I pass a value and not refresh the page?

A: Pop up a page to select the value or input, OK after use to pass the value to the original window, using JavaScript to close the open window:

Window.close (); Opener.focus ();

57. Q: There is a string: "Ef0c114ea4″, how to change to a[0] = 0xEF a[1] = 0x0C a[2] = 0x11 a[3] = 0x4E a[4] = 0xa4?

Answer: String str= "ef0c114ea4f";

Out.print (str+ "");

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 ();



a[i]= "0x" +str.substring (j,k);

Out.print ("a[" +integer.tostring (i) + "]=" +a[i]+ "");



58. Q: How do I convert an int to a four-byte byte array?


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 ()?

Answer: The parameter refers to the number of Start the search, and the return value refers to the location of the search (0,1,2,3 ...). Note that is calculated from zero.

60. Q: How do I add a button dynamically in a Java application?

A: This involves a component redraw problem, the component should exist before the panel is displayed, if a panel has been shown, then add to it can you see? But if you have button A on the same panel, if you press it with butt on B, Then if you make the whole panel heavy, then a itself to redraw, its event monitoring will not, of course, will not add B, so if you want to have another panel, when pressing A to add B to this panel and redraw the paenl, in fact, the better way is to first put B in the panel, The same one is OK. Setvisiable it (flase) and set it to true when a is pressed.

61. Q: Book Mybook=new book (BookID), the book is a servlet, error.

Answer: Book is a servlet, can book Mybook=new book (BookID);

Explain that you have implemented a servlet container? Otherwise, the servlet will allow you to invoke it yourself? Servlet if the call is actually not the same as the EJB 1%, they are inherited or implemented some interface, in these parent classes or interfaces implemented if and container \ "deal with" method, then the container calls these methods to manage it, let it generate instances, pooling, passivation, destruction, Regeneration and so on. So it's wrong to write.

62. Q: Given a string 5* (5+9)/7 How to calculate the result?

Answer: There are two ways

1. Complete with stack

2. The simplest method, without programming, if there is any database, with select (5* (5+9)/7) from onetable

63. Q: How to implement the encryption and decryption of the contents of the submission form?

A: If you use IE can only use SSL protocol, this layer do not you consider, otherwise only you use your own tools to encrypt the transmission, receive and then decrypt the friend, as to how to add solution, if you want to and recognized system combination, use general Md5,ras and other public algorithms, if you just preach their own solution, You do whatever you want to do with the data and get it back to the rules, and I'm sure no one else can know the decryption method except you.

64. Q: Why Integer.parseint ("+1″"), throw NumberFormatException exception?

A: Because the "+" run count is overloaded in Java. The system cannot determine whether you are using arithmetic plus or character +.

This can be better understood in javascript:

var a = document.t.s.value+1;

At this point a = 12345, because Document.t.s.value as a string. but var a = document.t.s.value-1;

A is 1233 because the system knows-the operation is definitely arithmetic run. So convert the Document.t.s.value into numbers.

65. Q: What is the use of hashcode () Why do I sometimes need to overwrite the Hashcode () method in object?

Answer: This is the ID of this object, or how to distinguish which object.

66. Q: How do I implement a timed execution in Tomcat?

Answer: Run automatically when the application starts. The servletlistener is defined in the servlet2.3, and the boot or shutdown (configurable in the configuration file) of the servlet Con text is triggered, triggering the operation of a daemon (which can be implemented Java.util.Timer or Javax.swing.Timer).

67. Q: Can the program output itself?

A: The well-known law of the paradox of the hole. That means no program can output itself.

68. Q: Is it possible to convert characters to ASCII code, such as converting A to 65?

Answer: Int a= ' a ';

Out.println (a);

69. Q: How can I distinguish between full-width and half-width in the input text?

A: Because you can not distinguish between the full-width and half-width of the value of the law, had to put the full-width symbol of the pastoral lifting out.

70. Q: What should I do when the user registers the automatic sending procedure?

A: This sending process does not consider performance, because it is not possible to have a person to register in 1 seconds, we say that the performance of the letter program refers to millions of letters in the queue to continue to send the kind, like you this casually how to write a program is OK, There is no need to use JavaMail. Just specify a server and then use Cocket to connect its 25 ports. I send a letter with the socket of the SMTP 25, as if there were two neighbors to send one thing, directly handed over, with JavaMail, The message mechanism is that you sent this thing from the post office to your neighbor.

Copyright notice: I feel like I'm doing a good job. I hope you can move your mouse and keyboard for me to order a praise or give me a comment, under the Grateful!_____________________________________________________ __ Welcome reprint, in the hope that you reprint at the same time, add the original address, thank you with

JSP interview Problem set

Related Article

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.