您好,登錄后才能下訂單哦!
使用Servlet怎么實(shí)現(xiàn)一個(gè)表單提交功能?相信很多沒(méi)有經(jīng)驗(yàn)的人對(duì)此束手無(wú)策,為此本文總結(jié)了問(wèn)題出現(xiàn)的原因和解決方法,通過(guò)這篇文章希望你能解決這個(gè)問(wèn)題。
用servlet實(shí)現(xiàn)一個(gè)注冊(cè)的小功能 ,后臺(tái)獲取數(shù)據(jù)。
注冊(cè)頁(yè)面:
注冊(cè)頁(yè)面代碼 :
<!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/> 愛(ài)好:<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>---請(qǐng)選擇---</option> <option value="bj">北京</option> <option value="sh">上海</option> <option value="sy">沈陽(yáng)</option> </select> <br/> <input type="submit" value="點(diǎn)擊注冊(cè)"> </form> </body> </html>
人員實(shí)體類(lèi): 注意:人員實(shí)體類(lèi)要與表單中的name一致,約定要優(yōu)于編碼
package com.chensi.bean; //實(shí)體類(lèi)中的字段要與表單中的字段一致,約定優(yōu)于編碼 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頁(yè)面(后臺(tái)接收數(shù)據(jù)方法一)
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 獲得填寫(xiě)的表單數(shù)據(jù) */ @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"); //獲取傳過(guò)來(lái)的表單數(shù)據(jù),根據(jù)表單中的name獲取所填寫(xiě)的值 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); } }
得到的數(shù)據(jù):
接收方法二:
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 獲得填寫(xiě)的表單數(shù)據(jù) */ @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"); //獲取傳過(guò)來(lái)的表單數(shù)據(jù),根據(jù)表單中的name獲取所填寫(xiě)的值 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); } }
得到的數(shù)據(jù):
接收方法三: 利用反射賦值給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 獲得填寫(xiě)的表單數(shù)據(jù) */ @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"); //獲取傳過(guò)來(lái)的表單數(shù)據(jù),根據(jù)表單中的name獲取所填寫(xiě)的值 try { User u = new User(); System.out.println("數(shù)據(jù)封裝之前: "+u); //獲取到表單數(shù)據(jù) Map<String, String[]> map = request.getParameterMap(); for(Map.Entry<String,String[]> m:map.entrySet()){ String name = m.getKey(); String[] value = m.getValue(); //創(chuàng)建一個(gè)屬性描述器 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("封裝數(shù)據(jù)之后: "+u); } catch (IntrospectionException | IllegalAccessException | IllegalArgumentException | InvocationTargetException e) { e.printStackTrace(); } } protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doGet(request, response); } }
得到的結(jié)果:
接收方法四:使用apache 的 BeanUtils 工具來(lái)進(jìn)行封裝數(shù)據(jù)(ps:這個(gè)Benautils工具,Struts框架就是使用這個(gè)來(lái)獲取表單數(shù)據(jù)的哦!)
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 獲得填寫(xiě)的表單數(shù)據(jù) */ @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"); //獲取傳過(guò)來(lái)的表單數(shù)據(jù),根據(jù)表單中的name獲取所填寫(xiě)的值 //方法四:使用beanUtil來(lái)封裝User類(lèi) User u = new User(); System.out.println("沒(méi)有使用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); } }
得到的結(jié)果:
接收方法 方式五: 使用inputStream流來(lái)進(jìn)行接收(一般字符串啥的不用這個(gè)方法,一般是文件上傳下載時(shí)候才會(huì)使用這種方法)因?yàn)榻邮盏降淖址鞣N亂碼,編碼問(wèn)題解決不好
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 獲得填寫(xiě)的表單數(shù)據(jù) */ @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"); //獲取傳過(guò)來(lái)的表單數(shù)據(jù),根據(jù)表單中的name獲取所填寫(xiě)的值 response.setContentType("text/html;charset=UTF-8"); //獲取表單數(shù)據(jù) 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); } }
得到的結(jié)果:(各種亂碼 。。。。)
看完上述內(nèi)容,你們掌握使用Servlet怎么實(shí)現(xiàn)一個(gè)表單提交功能的方法了嗎?如果還想學(xué)到更多技能或想了解更多相關(guān)內(nèi)容,歡迎關(guān)注億速云行業(yè)資訊頻道,感謝各位的閱讀!
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。