This article describes a few frequently asked questions articles JSP topics, Hashtable and HashMap, the characters into ASCII code, Session, datetime type, to prevent the user directly input url into the page, form the default method is get, point back display page expired, java in Regular use of the page cache does not retain the form request handling Chinese characters, in the JSP page to get the name of the page and so on.
Previously recorded things.
1, character processing request form:
2, in the JSP page to get the name of the page:
request.getRequestURI (); / / file name
request.getRequestURL (); / / full url
3, the page cache is not retained:
response.setHeader ("Pragma", "No-cache");
response.setHeader ("Cache-Control", "no-cache");
response.setDateHeader ("Expires", 0);
4, date and time (server)
java.text.DateFormat df = new java.text.SimpleDateFormat ("MM dd month day HH: mm E"); / / here format
datestr = df.format (new java.util.Date ());
<% Java.util.Date shijian = new java.util.Date ();%>
<% = Shijian.getYear () +1900 %><%= shijian.getMonth () +1 %><%= shijian.getDate ()%>
<% = Shijian.getHour ()%><%= shijian.getMinute ()%>
5, java in regular use. jdk 1.4 or above is required to import = "java.util.regex .*
6, point back display expired pages
Add the following code in it
<META Http-equiv=Pragma content=no-cache>
<META Http-equiv=Cache-Control content=no-cache>
<META Http-equiv=Expires content=0>
And 3, the same principle.
7, calculating the time spent implementing
Code began to take time, after taking time subtraction
long t1 = System.currentTimeMillis ();
///////////////// Your code
long t2 = System.currentTimeMillis ();
long time = t2-t1;
8, rounded to retain two decimal places after the decimal point?
import java.text .*;
NumberFormat nf = NumberFormat.getNumberInstance ();
Or (0.005 - 0.01) and then take. After the two
9, form the default method is get.
way is to post high-capacity data transmission server, the method used. (Re-open a socket.?)
10, to prevent users from url directly into the page:
First, add the page to access control. The general use session.
Second, from a web server control and all access to a directory to be verified. (Some say the next jsp into web-inf)
11, the database is a datetime type, insert the current time to the database:
java.sql.Date sqlDate = new java.sql.Date ();
PreparedStatement pstmt = conn.prepareStatement ("insert into foo (time) values (?)");
pstmt.setDate (1, sqlDate);
In fact, the general database system has its own function of time.
insert into foo (time) values (sysdate)
12, session access the int type of variable:
session.setAttribute ("int", i +""); / / Note that i + ""
int i = Integer.parseInt (session.getAttribute ("int"));
session some of the concepts.
When users browse the Web, as Http protocol is a stateless protocol, often between different pages of data exchange issues, which requires them to share data between different pages. Common implementation is to save the data to be shared in Session. For example, in the user login page, some users of the information being saved to the Session, and then read the other pages in the user's information. The shared data can be string or primitive data types and Java-related objects, it can be a Java object.
Session can only save the object, can not save the original data type, such as:
session.setAttribute ("count", 10)
Statement is illegal, if we want to save an integer value of 10 Session, the need to use the following method:
session.setAttribute ("count", new Integer (10));
Then in another page using:
(Integer) session.getAttribute ("count")
To read out of this integer.
was set to the session on the system database, but this affected efficiency. session size is best not too
13, the characters into ASCII code
int a = 'A'; out.println (a);
14, String s = new String ("xyz"); created two String Object object, one is "xyx", one is pointing to "xyx" a reference to an object s.
There are typically equals () == this reference with the principles of the stack.
15, swtich no effect on the long on and String:
switch (expr1) in, expr1 is an integer expression. Switch and case statement passed to the parameters should be
int, short, char or byte. long, string can not act on the swtich.
16, Hashtable and HashMap
Dictionary class inherited from Hashtable, the HashMap is the introduction of the Map interface Java1.2 an implementation of HashMap allows null as an entry in the key or value, while Hashtable does not allow the method is Synchronize Hashtable, while HashMap is not, in multiple threads visit Hashtable, the need for its own method of achieving synchronization, and HashMap must be synchronized to provide them things.