Android Development Service Class 03_servletforgetmethod

Source: Internet
Author: User

Receiving and processing data uploaded by the user via GET is generally less than 2K, and the security requirements are not high.

1  PackageCom.wangjialin.internet.servlet;2 3 Importjava.io.IOException;4 Importjavax.servlet.ServletException;5 ImportJavax.servlet.annotation.WebServlet;6 ImportJavax.servlet.http.HttpServlet;7 Importjavax.servlet.http.HttpServletRequest;8 ImportJavax.servlet.http.HttpServletResponse;9 Ten  One@WebServlet ("/servletforgetmethod") A  Public classServletforgetmethodextendsHttpServlet { -      -     Private Static Final LongSerialversionuid = 1L; the     -     protected voidDoget (HttpServletRequest request, httpservletresponse response)throwsservletexception, IOException { -          -String name= request.getparameter ("name"); + //string Name= New String (Request.getparameter ("name"). GetBytes ("Iso8859-1"), "UTF-8"); -String age= request.getparameter ("Age"); +System.out.println ("Name:" +name); ASystem.out.println ("Age:" +Age ); at      -     } -  -     protected voidDoPost (HttpServletRequest request, httpservletresponse response)throwsservletexception, IOException { -         //TODO auto-generated Method Stub -     } in  -}

Httpservletrequestencodingwrapper (HttpServletRequest Business wrapper to solve the problem of Chinese characters garbled)

1  PackageCom.wangjialin.internet.filter;2 3 Importjava.io.UnsupportedEncodingException;4 5 Importjavax.servlet.http.HttpServletRequest;6 ImportJavax.servlet.http.HttpServletRequestWrapper;7 8  Public classHttpservletrequestencodingwrapperextends9 Httpservletrequestwrapper {Ten  One     Privatehttpservletrequest request; A      PublicHttpservletrequestencodingwrapper (HttpServletRequest request) { -         Super(request); -          This. Request =request; the     } -      - @Override -      Publicstring GetParameter (string name) { +          -String value =Request.getparameter (name); +         Try { A             if(Value! =NULL){ at                 return NewString (Value.getbytes ("iso8859-1"), "UTF-8"); -             } -}Catch(unsupportedencodingexception e) { -         } -         return Super. GetParameter (name); -     } in  -}

Encodingfilter

1  PackageCom.wangjialin.internet.filter;2 3 Importjava.io.IOException;4 5 ImportJavax.servlet.Filter;6 ImportJavax.servlet.FilterChain;7 ImportJavax.servlet.FilterConfig;8 Importjavax.servlet.ServletException;9 Importjavax.servlet.ServletRequest;Ten ImportJavax.servlet.ServletResponse; One ImportJavax.servlet.annotation.WebFilter; A Importjavax.servlet.http.HttpServletRequest; -  -  the@WebFilter ("/*") -  Public classEncodingfilterImplementsFilter { -  -      Public voiddestroy () { +         //TODO auto-generated Method Stub -     } +  A      Public voidDoFilter (servletrequest request, servletresponse response, Filterchain chain) at             throwsIOException, servletexception { -          -HttpServletRequest req =(httpservletrequest) request; -          -         if("GET". Equals (Req.getmethod ())) { -              inHttpservletrequestencodingwrapper Wraper =NewHttpservletrequestencodingwrapper (req); - Chain.dofilter (wraper, response); to              +}Else{ -Req.setcharacterencoding ("UTF-8"); the Chain.dofilter (request, response); *         } $     }Panax Notoginseng  -      Public voidInit (Filterconfig fconfig)throwsservletexception { the         //TODO auto-generated Method Stub +     } A}

Android Development Service Class 03_servletforgetmethod

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.