04.request获取表单数据二 doPost方式

 

复制代码
package com.gyf.web.servlet.lesson04;

import java.beans.IntrospectionException;
import java.beans.PropertyDescriptor;
import java.io.IOException;
import java.lang.reflect.Method;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Properties;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.gyf.model.User;



@WebServlet("/Lesson04Servlet")
public class Lesson04Servlet extends HttpServlet{

    
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse resp) throws ServletException, IOException {
        //post请求时,中文会乱码,解决方法setCharacterEncoding,只针对post请求有效
        request.setCharacterEncoding("UTF-8");
        
        //创建用户对象
        User user = new User();
        
        //获取请求参数
        /**
         * key            value
         * username        {"gyf"};
         * password     {"123"};
         * hobby        {"studying","money"}
         */
        Map<String, String[]> map = request.getParameterMap();
        try {
            for(Entry<String, String[]> entry : map.entrySet()){
                String pname = entry.getKey();
                /*if(pname.equals("username")){
                    user.setUsername(entry.getValue()[0]);
                }else if(pname.equals("hobby")){
                    user.setHobby(entry.getValue());
                }*/
                
                //反射
                //创建属性描述器
                
                PropertyDescriptor pd = new PropertyDescriptor(entry.getKey(), User.class);
                
                //获取写入方法对象
                
                Method method = pd.getWriteMethod();
                
                //通过反射调用方法【怎么获取方法的参数类型】
                Class[] clzs = method.getParameterTypes();
                if(clzs.length == 0){
                    return;
                }
                
                String clzName = clzs[0].toString();
                String[] values = entry.getValue();
                if(clzName.contains("[Ljava.lang.String")){
                    System.out.println("数组...");
                    method.invoke(user, (Object)values);
                }else{
                    System.out.println("非数组...");
                    method.invoke(user, values[0]);
                }
                
                /*String[] values = entry.getValue(); 这种方式有bug
                if(values.length == 1){
                    method.invoke(user, values[0]);
                }else{
                    method.invoke(user, (Object)values);
                }
                */
            }
        
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        
        System.out.println(user);
    }
}
复制代码

 

posted @   expworld  阅读(384)  评论(0编辑  收藏  举报
编辑推荐:
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
阅读排行:
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· Manus的开源复刻OpenManus初探
· AI 智能体引爆开源社区「GitHub 热点速览」
· 三行代码完成国际化适配,妙~啊~
· .NET Core 中如何实现缓存的预热?
点击右上角即可分享
微信分享提示