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 }
时间: 2024-10-20 20:42:25

Android 开发服务类 03_ServletForGETMethod的相关文章

Android 开发服务类 05_ ApkPatchDemo

APP 增量更新服务端[https://github.com/cundong/SmartAppUpdates] 1 import com.cundong.common.Constants; 2 import com.cundong.utils.PatchUtils; 3 4 /** 5 * 类说明: 使用旧版apk包+差分包,合并新包实例 6 * 7 * @author Cundong 8 * @date 2014-9-6 9 * @version 1.2 10 */ 11 public cla

Android 开发服务类 04_ServletForPOSTMethod

ServletForPOSTMethod 业务类 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

Android 开发服务类 02_NewsListServlet

Servlet implementation class NewsListServlet 1 package com.wangjialin.server.xml; 2 3 import java.io.IOException; 4 import java.util.List; 5 6 import javax.servlet.ServletException; 7 import javax.servlet.http.HttpServlet; 8 import javax.servlet.http

Android 开发服务类 01_ServletForXML

Servlet implementation class NewsListServlet 1 package com.wangjialin.server.xml; 2 3 import java.io.IOException; 4 import java.util.List; 5 6 import javax.servlet.ServletException; 7 import javax.servlet.http.HttpServlet; 8 import javax.servlet.http

android开发——camera类拍照指定图片大小

android拍照开发 android开发实现拍照功能主要有两种方法: 直接调用系统照相机API实现拍照,拍完后,图片会保存在相册中,返回保存照片的路径,从而获取图片. 自己写SurfaceView调用camera来实现拍照,该方法触发一个回调,参数中包含一个图片字节数组,从而获取图片. 问题 当我们自定义相机时,需求需要指定拍照图片大小,然而不同手机会默认返回不同分辨率照片.所以需要对camera进行参数设置.通过设置setPictureSize,代码: // 获得相机参数 Camera.Pa

Android 开发 Camera类的拍照与录像

前言 在开发Android应用的时候,如果需要调用摄像头拍照或者录像,除了通过Intent调用系统现有相机应用进行拍照录像之外,还可以通过直接调用Camera硬件去去获取摄像头进行拍照录像的操作.本篇博客将讲解如何在Android应用中通过Camera拍照录像. 参考博客:https://www.cnblogs.com/plokmju/p/android_Camera.html Camera api 说明 Camera是Android摄像头硬件的相机类,位于硬件包"android.hardwar

android开发 - Application类

本文摘自google的android官网翻译,如果有不对的地方,请各位大神指点 网址:http://developer.android.com/reference/android/app/Application.html Application,Activity,Service,content provider都是Android框架的系统组件,当Android程序启动时系统会创建一个Application对象,用来存储系统的一些信息. 启动一个android应用程序,就等于启动了一个进程,一个进

android开发工具类总结(一)

一.日志工具类 Log.java 1 public class L 2 { 3 private L() 4 { 5 /* 不可被实例化 */ 6 throw new UnsupportedOperationException("Cannot be instantiated!"); 7 } 8 // 是否需要打印bug,可以在application的onCreate函数里面初始化 9 public static boolean isDebug = true; 10 private sta

Android 开发工具类 19_NetworkStateReceiver

检测网络状态改变类: 1.注册网络状态广播: 2.检查网络状态: 3.注销网络状态广播: 4.获取当前网络状态,true为网络连接成功,否则网络连接失败: 5.注册网络连接观察者: 6.注销网络连接观察者. 1 import java.util.ArrayList; 2 3 import android.content.BroadcastReceiver; 4 import android.content.Context; 5 import android.content.Intent; 6 i