Some tricks of the servlet & template code

Source: Internet
Author: User
Tags set set browser cache

Some tricks of the servlet & template code

Request related

Get some basic parameters view source print?

01 Get Submit Address
02 Request.getrequesturi ();
03 Get Submit Content
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 ..... )
10 Request.getmethod ();

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);
05
06
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]);
12 }
13
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);
20 }
21st
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);
31 }
32 }

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");
5
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 ();
05
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 ("")) {
13 return null;
14 }
15 Determines whether a string
16 if (!) ( Value instanceof String)) {
17 throw new Conversionexception ("only string-type conversions are supported. ");
18 }
19 string s = (string) value;
20 Format a date string as a date type
21st SimpleDateFormat SDF = new SimpleDateFormat ("Yyyy-mm-dd");
22 try {
23 return Sdf.parse (s);
24 catch (ParseException e) {
25 throw new Conversionexception (S + "is not a valid date value");
26 }
27 }
28 }, Date.class);
29
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");
03 Expires:-1
04 Cache-control:no-cache
05 Pragma:no-cache
06
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

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.