新网创想网站建设,新征程启航
为企业提供网站建设、域名注册、服务器等服务
使用Servlet怎么实现一个表单提交功能?相信很多没有经验的人对此束手无策,为此本文总结了问题出现的原因和解决方法,通过这篇文章希望你能解决这个问题。
专注于为中小企业提供网站建设、成都做网站服务,电脑端+手机端+微信端的三站合一,更高效的管理,为中小企业渭南免费做网站提供优质的服务。我们立足成都,凝聚了一批互联网行业人才,有力地推动了成百上千家企业的稳健成长,帮助中小企业通过网站建设实现规模扩充和转变。
用servlet实现一个注册的小功能 ,后台获取数据。
注册页面:
注册页面代码 :
Insert title here
人员实体类: 注意:人员实体类要与表单中的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获取所填写的值 Enumerationnames = 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); //获取到表单数据 Mapmap = request.getParameterMap(); for(Map.Entry 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); } }
得到的结果:(各种乱码 。。。。)
看完上述内容,你们掌握使用Servlet怎么实现一个表单提交功能的方法了吗?如果还想学到更多技能或想了解更多相关内容,欢迎关注创新互联行业资讯频道,感谢各位的阅读!