Servlet的5种方式实现表单提交(注册小功能),后台获取表单数据实例
作者:柠檬旋风腿 发布时间:2022-08-19 20:58:06
标签:servlet,提交,表单
用servlet实现一个注册的小功能 ,后台获取数据。
注册页面:
注册页面代码 :
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<form action="/RequestDemo/RequestDemo3" method="post">
用户名:<input type="text" name="userName"><br/>
密码:<input type="text" name="pwd"><br/>
性别:<input type="radio" name="sex" value="男" checked="checked">男
<input type="radio" name="sex" value="女">女<br/>
爱好:<input type="checkbox" name="hobby" value="足球">足球
<input type="checkbox" name="hobby" value="篮球">篮球
<input type="checkbox" name="hobby" value="排球">排球
<input type="checkbox" name="hobby" value="羽毛球">羽毛球<br/>
所在城市:<select name="city">
<option>---请选择---</option>
<option value="bj">北京</option>
<option value="sh">上海</option>
<option value="sy">沈阳</option>
</select>
<br/>
<input type="submit" value="点击注册">
</form>
</body>
</html>
人员实体类: 注意:人员实体类要与表单中的name一致,约定要优于编码
package com.chensi.bean;
//实体类中的字段要与表单中的字段一致,约定优于编码
public class User {
private String userName;
private String pwd;
private String sex;
private String[] hobby;
private String city;
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
public String getPwd() {
return pwd;
}
public void setPwd(String pwd) {
this.pwd = pwd;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
public String[] getHobby() {
return hobby;
}
public void setHobby(String[] hobby) {
this.hobby = hobby;
}
public String getCity() {
return city;
}
public void setCity(String city) {
this.city = city;
}
}
接收方法一: Servlet页面(后台接收数据方法一)
package com.chensi;
import java.io.IOException;
import java.util.Iterator;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
* Servlet 获得填写的表单数据
*/
@WebServlet("/RequestDemo3")
public class RequestDemo3 extends HttpServlet {
private static final long serialVersionUID = 1L;
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
request.setCharacterEncoding("UTF-8");
//获取传过来的表单数据,根据表单中的name获取所填写的值
String userName = request.getParameter("userName");
String pwd = request.getParameter("pwd");
String sex = request.getParameter("sex");
String[] hobbys = request.getParameterValues("hobby");
System.out.println(userName);
System.out.println(pwd);
System.out.println(sex);
for (int i = 0; hobbys!=null&&i < hobbys.length; i++) {
System.out.println(hobbys[i]+"\t");
}
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doGet(request, response);
}
}
得到的数据:
接收方法二:
package com.chensi;
import java.io.IOException;
import java.util.Enumeration;
import java.util.Iterator;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
* Servlet 获得填写的表单数据
*/
@WebServlet("/RequestDemo3")
public class RequestDemo3 extends HttpServlet {
private static final long serialVersionUID = 1L;
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
request.setCharacterEncoding("UTF-8");
//获取传过来的表单数据,根据表单中的name获取所填写的值
Enumeration<String> names = request.getParameterNames();
while (names.hasMoreElements()) {
String strings = (String) names.nextElement();
String[] parameterValues = request.getParameterValues(strings);
for (int i = 0;parameterValues!=null&&i < parameterValues.length; i++) {
System.out.println(strings+":"+parameterValues[i]+"\t");
}
}
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doGet(request, response);
}
}
得到的数据:
接收方法三: 利用反射赋值给User
package com.chensi;
import java.beans.IntrospectionException;
import java.beans.PropertyDescriptor;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.Enumeration;
import java.util.Iterator;
import java.util.Map;
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.chensi.bean.User;
/**
* Servlet 获得填写的表单数据
*/
@WebServlet("/RequestDemo3")
public class RequestDemo3 extends HttpServlet {
private static final long serialVersionUID = 1L;
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
request.setCharacterEncoding("UTF-8");
//获取传过来的表单数据,根据表单中的name获取所填写的值
try {
User u = new User();
System.out.println("数据封装之前: "+u);
//获取到表单数据
Map<String, String[]> map = request.getParameterMap();
for(Map.Entry<String,String[]> m:map.entrySet()){
String name = m.getKey();
String[] value = m.getValue();
//创建一个属性描述器
PropertyDescriptor pd = new PropertyDescriptor(name, User.class);
//得到setter属性
Method setter = pd.getWriteMethod();
if(value.length==1){
setter.invoke(u, value[0]);
}else{
setter.invoke(u, (Object)value);
}
}
System.out.println("封装数据之后: "+u);
} catch (IntrospectionException | IllegalAccessException | IllegalArgumentException | InvocationTargetException e) {
e.printStackTrace();
}
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doGet(request, response);
}
}
得到的结果:
接收方法四:使用apache 的 BeanUtils 工具来进行封装数据(ps:这个Benautils工具,Struts框架就是使用这个来获取表单数据的哦!)
package com.chensi;
import java.beans.IntrospectionException;
import java.beans.PropertyDescriptor;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.Enumeration;
import java.util.Iterator;
import java.util.Map;
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 org.apache.commons.beanutils.BeanUtils;
import com.chensi.bean.User;
/**
* Servlet 获得填写的表单数据
*/
@WebServlet("/RequestDemo3")
public class RequestDemo3 extends HttpServlet {
private static final long serialVersionUID = 1L;
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
request.setCharacterEncoding("UTF-8");
//获取传过来的表单数据,根据表单中的name获取所填写的值
//方法四:使用beanUtil来封装User类
User u = new User();
System.out.println("没有使用BeanUtil封装之前: "+u);
try {
BeanUtils.populate(u, request.getParameterMap());
System.out.println("使用BeanUtils封装之后: "+u);
} catch (IllegalAccessException | InvocationTargetException e) {
e.printStackTrace();
}
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doGet(request, response);
}
}
得到的结果:
接收方法 方式五: 使用inputStream流来进行接收(一般字符串啥的不用这个方法,一般是文件上传下载时候才会使用这种方法)因为接收到的字符串各种乱码,编码问题解决不好
package com.chensi;
import java.beans.IntrospectionException;
import java.beans.PropertyDescriptor;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.Enumeration;
import java.util.Iterator;
import java.util.Map;
import javax.servlet.ServletException;
import javax.servlet.ServletInputStream;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.commons.beanutils.BeanUtils;
import com.chensi.bean.User;
/**
* Servlet 获得填写的表单数据
*/
@WebServlet("/RequestDemo3")
public class RequestDemo3 extends HttpServlet {
private static final long serialVersionUID = 1L;
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
request.setCharacterEncoding("UTF-8");
//获取传过来的表单数据,根据表单中的name获取所填写的值
response.setContentType("text/html;charset=UTF-8");
//获取表单数据
ServletInputStream sis = request.getInputStream();
int len = 0;
byte[] b = new byte[1024];
while((len=sis.read(b))!=-1){
System.out.println(new String(b, 0, len, "UTF-8"));
}
sis.close();
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doGet(request, response);
}
}
得到的结果:(各种乱码 。。。。)
来源:http://www.cnblogs.com/zhanghaoliang/p/5622900.html
0
投稿
猜你喜欢
- 提示:这里可以添加本文要记录的大概内容:例如:一般系统前端调用后台相关功能接口时,需要验证此时用户的权限是否满足调用该接口的条件,因此我们需
- SpringAOP的介绍:传送门demo介绍主要通过自定义注解,使用SpringAOP的环绕通知拦截请求,判断该方法是否有自定义注解,然后判
- 新建一个表:create table abc(id int IDENTITY(1,1) NOT NULL,name nvarchar(100
- 背景SpringBoot 版本<parent> <groupId>org.springfr
- 最近项目中使用springboot+jwt实现登录权限控制,所以在这里记录一下防止以后忘记,毕竟好记性不如烂笔头嘛~。首先我们需要导入使用到
- 1.简介使用线程池可以避免线程的频繁创建以及销毁。JAVA中提供的用于实现线程池的API:Executor、ExecutorService、
- Spring Cache设置缓存条件原理从Spring3.1开始,Spring框架提供了对Cache的支持,提供了一个对缓存使用的抽象,通过
- 首先当我们将Dwr3配置好以后,我们可以在浏览器中测试一下,查看一下我们配置的Dwr有没有生效,方法是http://localhost:[你
- 本文实例为大家分享了springboot实现异步任务的具体代码,供大家参考,具体内容如下1.什么异步任务同步:一定要等任务执行完了,得到结果
- 一、代码先上代码(以下这段代码会有多种执行结果)@Testpublic void test_interrupted_thread() thr
- 什么是JavaMemoryModel(JMM)?JMM通过构建一个统一的内存模型来屏蔽掉不同硬件平台和不同操作系统之间的差异,让Java开发
- 这篇文章主要介绍了java split()使用方法解析,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋
- 最近在做一个资源共享的项目中,采用了Struts2.1.8+Spr
- 在本博客中,可以找到一篇《c#实现输出的字符靠右对齐的示例》它有教大家怎样实现字符串输出进行左齐或者是右对齐。本篇的方法,超简单,是使用st
- init和clinit区别①init和clinit方法执行时机不同init是对象构造器方法,也就是说在程序执行 new 一个对象调用该对象类
- java控制台输出图书馆管理系统(只用java代码不用数据库和GUI,java入门的新手秒懂)在个项目中,我只用数组保存数据,和只用for循
- 路径分隔符:Windows下是“\”unix|linux下是“/”考虑到程序的可移植性,创建文件时建议大家选用"/",因
- 如何解决yml没有spring小叶子标志我的idea springboot项目中有两个.yml文件,一个application.yml,一个
- 这篇文章主要介绍了Springboot整合MybatisPlus的实现过程解析,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定
- Object是所有类的父类,也就是说java中所有的类都是直接或者间接继承自Object类。比如你随便创建一个classA,虽然没有明说,但