Javaweb获取表单数据的多种方式

网友投稿 247 2023-06-06


Javaweb获取表单数据的多种方式

javaweb获取表单数据的几种方式

一、通过键值对的形式获取表单数据

getParameter(String name):通过key,返回一个value。

getParameterValues(String name):通过key返回一个string数组(多个值)

getParameterNames():返回form表单中的所有key值。

下面介绍通过键值对获取form表单数据的数据的方法:

@WebServlet({ "/FormServlet", "/form" })

public class FormServlet extends HttpServlet {

private static final long serialVersionUID = 1L;

public FormServlet() {

super();

}

protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

response.setContentType("text/json;charset=utf-8");

PrintWriter out = response.getWriter();

Enumeration paramNames = request.getParameterNames();

while (paramNames.hasMoreElements()) {

String name = paramNames.nextElement();

String[] values = request.getParameterValues(name);

if(values!=null && values.length>0){

StringBuilder builder = new StringBuilder();

for (int i = 0; i < values.length; i++) {

builder.append(values[i]+" ");

}

out.println(name+" : "+builder.toString());

}

}

}

protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

doGet(request, response);

}

}

form表单:

用户名

密码

性别


爱好 篮球

足球

游戏

<input type="checkbox" name="hobby" value="media"/>电影

城市



二、通过Map的形式获取表单数据

getParameterMap():http://获取form表单的数据,以map的格式封装起来

示例:

@WebServlet({ "/FormServlet", "/form" })

public class FormServlet extends HttpServlet {

private static final long serialVersionUID = 1L;

public FormServlet() {

super();

}

protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

response.setContentType("text/json;charset=utf-8");

PrintWriter out = response.getWriter();

User user = new User();

out.println("获取表单数据之前:"+user.toString());

Map map = request.getParameterMap();

for (Map.Entry m : map.entrySet()) {

String name = m.getKey();

String[] values = m.getValue();

//属性描述器:表示JavaBean类通过存储器导出一个属性

PropertyDescriptor pd=null;

try {

pd = new PropertyDescriptor(name, User.class);

} catch (IntrospectionException e) {

e.printStackTrace();

}

if (values!=null&& pd !=null) {

Method setter = pd.getWriteMethod();

try {

if (values.length==1) {

setter.invoke(user, values[0]);

}else {

setter.invoke(user, (Object)values);

}

} catch (IllegalAccessException e) {

e.printStackTrace();

} catch (IllegalArgumentException e) {

e.printStackTrace();

} catch (InvocationTargetException e) {

e.printStackTrace();

}

}

}

out.println("获取表单数据之后:"+user.toString());

}

protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

doGet(request, response);

}

}

PropertyDescriptor的使用,点击查看

User类

public class User {

private String username;//属性名称需要和表单数据中的name值保持一致

private String password;

private String sex;

private String[] hobby;

private String city;

...

//set get方法省略

}

三、通过第三方jar包获取封装表单数据

使用第三方jar包:commons-beanutils-1.8.3.jar

@WebServlet({ "/FormServlet", "/form" })

public class FormServlet extends HttpServlet {

private static final long serialVersionUID = 1L;

public FormServlet() {

super();

}

protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletExcephttp://tion, IOException {

response.setContentType("text/json;charset=utf-8");

PrintWriter out = response.getWriter();

User user = new User();

out.println("获取表单数据之前:"+user.toString());

try {

//通过第三方jar包处理

BeanUtils.populate(user, request.getParameterMap());

} catch (IllegalAccessException e) {

e.printStackTrace();

} catch (InvocationTargetException e) {

e.printStackTrace();

}

out.println("获取表单数据之后:"+user.toString());

}

protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

doGet(request, response);

}

}


版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:javaBean的基础知识及常见乱码解决方法
下一篇:利用imgareaselect辅助后台实现图片上传裁剪
相关文章

 发表评论

暂时没有评论,来抢沙发吧~