Common Java code segments

Source: Internet
Author: User

1. Chinese garbled characters in JSP web pages:
A problem occurs when user input is transmitted from one webpage to another through a request.
If the Chinese characters obtained by request. getparameter () cannot be correctly displayed, for example:
String username = request. getparameter ("username ");
If username is not correctly displayed, add the following sentence:
Username = new string (username. getbytes ("iso-8859-1"), "gb2312 ");

2. Get the current time
Public static string gettime (){
String format = "mm-dd-yyyy KK: mm a"; // format the date and time
Simpledateformat formatter = new simpledateformat (format );
Date nowdate = new date ();
Return formatter. Format (nowdate );
}

3. Format and display the time
Originaltime indicates the input time, And desttime indicates the time when the format is converted.
Public static string timeformat (string originaltime ){
Date = NULL;
String desttime = NULL;
// Set the time format first
Simpledateformat bartdateformat = new simpledateformat ("yyyy-mm-dd KK: mm ");
Try {
// Obtain the string time in this format and save it as a date object
Date = bartdateformat. parse (originaltime );
// To convert the string to the desired time type, format is also required.
Desttime = bartdateformat. Format (date );
}
Catch (exception ex ){
System. Out. println (ex. getmessage ());
}
Return desttime;
}

4. traverse all elements in the request
Java. util. Enumeration E = request. getparameternames ();
While (E. hasmoreelements ()){
String Param = (string) E. nextelement ();
// Perform subsequent operations on the element
}

5. automatically submit values using the hidden fields of the form
<Input type = "hidden" name = "ID" value = "XXXXX">
In this way, when a user submits a form, this value is filled in the relevant position of the request.

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.