Some tricks of the servlet & template code
Request related
Get some basic parameters view source print?
02 |
Request.getrequesturi (); |
04 |
Request.getquerystring (); |
05 |
Get client address (browser) |
06 |
Request.getremoteaddr (); |
07 |
Get client port (browser) |
08 |
Request.getremoteport (); |
09 |
Get the Commit method (Get,post or ..... ) |
Get Submit Content Body View Source print?
01 |
Get a Submit content parameter |
02 |
Http://xxx/xx?username=flx |
03 |
String username = request.getparameter ("username"); |
04 |
SYSTEM.OUT.PRINTLN (username); |
07 |
Get some submit content parameters with the same name |
08 |
Http://xxx/xx?username=flx&username=lhm |
09 |
String values[] = request.getparametervalues ("username"); |
10 |
for (int i=0;values!=null && i<values.length;i++) { |
11 |
System.out.println (Values[i]); |
14 |
Gets a collection of all the submit parameters (not appropriate with an argument with the same name) |
15 |
Enumeration E = Request.getparameternames (); |
16 |
while (E.hasmoreelements ()) { |
17 |
String name = (string) e.nextelement (); |
18 |
String value = request.getparameter (name); |
19 |
SYSTEM.OUT.PRINTLN (name + "=" + value); |
22 |
Get all parameter collections, suitable for parameters with the same name |
23 |
Http://xxx/xx?username=flx&password=123 |
24 |
map<string,string[]> map = Request.getparametermap (); |
25 |
Map.keyset () Set set = Map.entryset () |
26 |
For (map.entry<string, string[]> entry:map.entrySet ()) { |
27 |
String name = Entry.getkey (); |
28 |
Values = Entry.getvalue (); |
29 |
for (String value:values) { |
30 |
SYSTEM.OUT.PRINTLN (name + "=" + value); |
Garbled Question View source print?
1 |
The content obtained by default is "Iso8859-1" encoding |
2 |
String username = request.getparameter ("username"); |
3 |
First gets the original byte in the current encoding and then turns to the target encoding |
4 |
Username = new String (username.getbytes ("iso8859-1"), "UTF-8"); |
6 |
You can also set the encoding manually |
7 |
Request.setcharacterencoding ("UTF-8"); |
8 |
String username = request.getparameter ("username"); |
Use Org.apache.commons.beanutils to quickly populate the Bean view source print?
01 |
Get the submitted data collection |
02 |
Map map = Request.getparametermap (); |
03 |
This is a bean that stores the submitted data |
04 |
User user = new user (); |
06 |
Org.apache.commons.beanutils |
07 |
A conversion tool for Beanutils, where a conversion of a date type is registered |
08 |
Convertutils.register (New Converter () {//converter interface |
09 |
Implementing this method, Convertutils knows how to convert the date type |
10 |
public object Convert (Class type, Object value) { |
11 |
Decide whether to be empty first |
12 |
if (Value==null | | value.equals ("")) { |
15 |
Determines whether a string |
16 |
if (!) ( Value instanceof String)) { |
17 |
throw new Conversionexception ("only string-type conversions are supported. "); |
19 |
string s = (string) value; |
20 |
Format a date string as a date type |
21st |
SimpleDateFormat SDF = new SimpleDateFormat ("Yyyy-mm-dd"); |
24 |
catch (ParseException e) { |
25 |
throw new Conversionexception (S + "is not a valid date value"); |
30 |
Start populating the collection into the bean map |
31 |
Beanutils.populate (user, map); |
Response related
Set browser cache view Source print?
01 |
Response the parameters in the body to describe the cache settings |
02 |
Response.setheader ("Content-type", "image/jpeg"); |
04 |
Cache-control:no-cache |
07 |
Setting the browser does not cache data |
08 |
Response.setdateheader ("Expires",-1); |
09 |
Response.setheader ("Cache-control", "No-cache"); |
10 |
Response.setheader ("Pragma", "No-cache"); |
Output text view source print?
1 |
Set all methods of encoding format |