Android 开发服务类 03_ServletForGETMethod

接收并处理用户通过 GET 方式上传的数据,一般小于 2K,并且安全性要求不高。

 1 package com.wangjialin.internet.servlet;
 2 
 3 import java.io.IOException;
 4 import javax.servlet.ServletException;
 5 import javax.servlet.annotation.WebServlet;
 6 import javax.servlet.http.HttpServlet;
 7 import javax.servlet.http.HttpServletRequest;
 8 import javax.servlet.http.HttpServletResponse;
 9 
10 
11 @WebServlet("/ServletForGETMethod")
12 public class ServletForGETMethod extends HttpServlet {
13     
14     private static final long serialVersionUID = 1L;
15    
16     protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
17         
18             String name= request.getParameter("name");
19 //            String name= new String(request.getParameter("name").getBytes("ISO8859-1"),"UTF-8");
20             String age= request.getParameter("age");
21             System.out.println("name: " + name );
22             System.out.println("age: " + age );
23     
24     }
25 
26     protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
27         // TODO Auto-generated method stub
28     }
29 
30 }

HttpServletRequestEncodingWrapper (HttpServletRequest 业务包装器,解决汉字乱码的问题)

 1 package com.wangjialin.internet.filter;
 2 
 3 import java.io.UnsupportedEncodingException;
 4 
 5 import javax.servlet.http.HttpServletRequest;
 6 import javax.servlet.http.HttpServletRequestWrapper;
 7 
 8 public class HttpServletRequestEncodingWrapper extends
 9         HttpServletRequestWrapper {
10 
11     private HttpServletRequest request;
12     public HttpServletRequestEncodingWrapper(HttpServletRequest request) {
13         super(request);
14         this.request = request;
15     }
16     
17     @Override
18     public String getParameter(String name) {
19         
20         String value = request.getParameter(name);
21         try {
22             if(value != null){
23                 return new String(value.getBytes("ISO8859-1"), "UTF-8");
24             }
25         } catch (UnsupportedEncodingException e) {
26         }
27         return super.getParameter(name);
28     }
29 
30 }

EncodingFilter

 1 package com.wangjialin.internet.filter;
 2 
 3 import java.io.IOException;
 4 
 5 import javax.servlet.Filter;
 6 import javax.servlet.FilterChain;
 7 import javax.servlet.FilterConfig;
 8 import javax.servlet.ServletException;
 9 import javax.servlet.ServletRequest;
10 import javax.servlet.ServletResponse;
11 import javax.servlet.annotation.WebFilter;
12 import javax.servlet.http.HttpServletRequest;
13 
14 
15 @WebFilter("/*")
16 public class EncodingFilter implements Filter {
17 
18     public void destroy() {
19         // TODO Auto-generated method stub
20     }
21 
22     public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) 
23             throws IOException, ServletException {
24         
25         HttpServletRequest req = (HttpServletRequest) request;
26         
27         if("GET".equals(req.getMethod())){
28             
29             HttpServletRequestEncodingWrapper wraper = new HttpServletRequestEncodingWrapper(req);
30             chain.doFilter(wraper, response);
31             
32         }else{
33             req.setCharacterEncoding("UTF-8");
34             chain.doFilter(request, response);
35         }
36     }
37 
38     public void init(FilterConfig fConfig) throws ServletException {
39         // TODO Auto-generated method stub
40     }
41 }

 

郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。