Several common problems encountered in JSP

Source: Internet
Author: User
Tags date count current time insert integer reference
js| problem

Something that you remember before.

1, form request Chinese character processing:

Request.setcharacterencoding ("GB2312")

2, in the JSP page to get the name of the page:

Request.getrequesturi ();//File name
Request.getrequesturl ();//All URLs

3, the page does not keep the cache:

Response.setheader ("Pragma", "No-cache");
Response.setheader ("Cache-control", "No-cache");
Response.setdateheader ("Expires", 0);

4, Date time (server side)

String Datestr;
Java.text.DateFormat df = new Java.text.SimpleDateFormat ("MM month DD day hh:mm E"); Here format
Datestr = Df.format (New java.util.Date ());
Out.println (DATESTR);

Or

<% java.util.Date shijian= New Java.util.Date ();%>
<%=shijian.getyear () +1900%><%=shijian.getmonth () +1%><%=shijian.getdate ()%>
<%=shijian.gethour ()%><%=shijian.getminute ()%>

5, Java in the use of regular. JDK needs to be more than 1.4 import= "java.util.regex.*

6, point back Display page expiration

The code inside.

Same as the 3 principle.

7. Calculate the time spent on execution

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

Long T1 = System.currenttimemillis ();
Your code
Long t2 = System.currenttimemillis ();
Long time = T2-T1;

8, rounded, keep the decimal point after two decimal places?

Import java.text.*;
NumberFormat nf=numberformat.getnumberinstance ();
Nf.setmaximumfractiondigits (2);
Nf.setminimumfractiondigits (2);
Nf.format (numb);
or (+0.005-0.01) to be fetched. The latter two digits

9, the default method of form is get.

Post is the method that is used when transferring large volumes of data to the server. (Open a socket again.?) )

10, to prevent users to enter the URL directly into the page:

One is to add control to the page you want to access. This is generally used in session.

The second is from the Web server control, all access to a directory to pass validation. (Some people say to put the JSP under Web-inf)

11, the database is datetime type, insert the current time to the database:

Java.sql.Date sqldate = new Java.sql.Date ();
PreparedStatement pstmt = conn.preparestatement ("INSERT into foo (?)");
Pstmt.setdate (1,sqldate);
Pstmt.executeupdate ();

In fact, the general database has its own system time functions.

INSERT into Foo (time) VALUES (sysdate)

12, Session access variables of type int:

Session.setattribute ("int", i+ ""); Note here i+ ""
int i = Integer.parseint (Session.getattribute ("int"));

Some concepts of the session.

When users browse the Web, because the HTTP protocol is a stateless protocol, there is often a problem of data exchange between different pages, which requires sharing data between these different pages. The common implementation is to save the data to be shared into the session. For example, in the user log on the page to save some user information to the session, and then read the user's information in other pages. The shared data can be a string or an object related to the original Java data type, or it can be a Java object.

Session can only save objects and cannot save original data types, such as:

Session.setattribute ("Count", 10)

is an illegal statement, if you want to save an integer value of 10 to the session, you need to use the following methods:

Session.setattribute ("Count", new Integer (10));

And then use it on another page:

(Integer) Session.getattribute ("Count")

To read this integer.

was set to place the session in the system's database, but this affects efficiency. The session size is best not too big

13. Convert the characters into ASCII code

int a= ' a '; Out.println (a);

14, String s = new string ("XYZ"), creates two string object objects, one is "XyX", and one is a reference object s that points to "XyX".

There is also a typical equals () and = = This reference in the stack principle.

15, Swtich can not function on long and string:

Switch (EXPR1), expr1 is an integer expression. The arguments passed to the switch and case statements should be
int, short, char, or byte. Long,string can not act on Swtich.

16, Hashtable and HashMap

Hashtable inherits from the dictionary class, and HashMap is an implementation of the map interface introduced by Java1.2 HASHMAP allows NULL as a entry key or value, The method that Hashtable does not allow Hashtable is synchronize, and HashMap is not, when multiple threads access Hashtable, they do not need to synchronize their methods, and HashMap must provide an external synchronization for them.







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.