Javaweb獲取表單數(shù)據(jù)的多種方式
Javaweb獲取表單數(shù)據(jù)的幾種方式
一、通過鍵值對(duì)的形式獲取表單數(shù)據(jù)
getParameter(String name):通過key,返回一個(gè)value。
getParameterValues(String name):通過key返回一個(gè)string數(shù)組(多個(gè)值)
getParameterNames():返回form表單中的所有key值。
下面介紹通過鍵值對(duì)獲取form表單數(shù)據(jù)的數(shù)據(jù)的方法:
@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<String> 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表單:
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>Insert title here</title> </head> <body> <form action="/TomcatDemo/form" method="post"> 用戶名<input type="text" name="username"/><br/> 密碼 <input type="password" name="password"/><br/> 性別 <input type="radio" name="sex" value="male" checked="checked"/>男 <input type="radio" name="sex" value="female"/>女<br/> 愛好 <input type="checkbox" name="hobby" value="basketball"/>籃球 <input type="checkbox" name="hobby" value="football"/>足球 <input type="checkbox" name="hobby" value="game"/>游戲 <input type="checkbox" name="hobby" value="media"/>電影<br/> 城市 <select name="city"> <option value="bj">北京</option> <option value="sh">上海</option> <option value="sz">深圳</option> <option value="hz">杭州</option> </select><br/> <input type="submit" value="注冊(cè)"/> <input type="submit" value="登入"/><br/> </form> </body> </html>
二、通過Map的形式獲取表單數(shù)據(jù)
getParameterMap():獲取form表單的數(shù)據(jù),以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("獲取表單數(shù)據(jù)之前:"+user.toString());
Map<String,String[]> map = request.getParameterMap();
for (Map.Entry<String, String[]> m : map.entrySet()) {
String name = m.getKey();
String[] values = m.getValue();
//屬性描述器:表示JavaBean類通過存儲(chǔ)器導(dǎo)出一個(gè)屬性
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("獲取表單數(shù)據(jù)之后:"+user.toString());
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doGet(request, response);
}
}
PropertyDescriptor的使用,點(diǎn)擊查看
User類
public class User {
private String username;//屬性名稱需要和表單數(shù)據(jù)中的name值保持一致
private String password;
private String sex;
private String[] hobby;
private String city;
...
//set get方法省略
}
三、通過第三方j(luò)ar包獲取封裝表單數(shù)據(jù)
使用第三方j(luò)ar包: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 ServletException, IOException {
response.setContentType("text/json;charset=utf-8");
PrintWriter out = response.getWriter();
User user = new User();
out.println("獲取表單數(shù)據(jù)之前:"+user.toString());
try {
//通過第三方j(luò)ar包處理
BeanUtils.populate(user, request.getParameterMap());
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (InvocationTargetException e) {
e.printStackTrace();
}
out.println("獲取表單數(shù)據(jù)之后:"+user.toString());
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doGet(request, response);
}
}
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- Javaweb中Request獲取表單數(shù)據(jù)的四種方法詳解
- JavaWeb實(shí)現(xiàn)表單提交的示例詳解
- 解決Javaweb 提交表單到servlet時(shí)出現(xiàn)空白頁面,但網(wǎng)站不報(bào)錯(cuò)問題
- Javaweb接收表單數(shù)據(jù)并處理中文亂碼
- Java后臺(tái)防止客戶端重復(fù)請(qǐng)求、提交表單實(shí)現(xiàn)原理
- JavaWeb表單注冊(cè)界面的實(shí)現(xiàn)方法
- JavaWeb表單及時(shí)驗(yàn)證功能在輸入后立即驗(yàn)證(含用戶類型,性別,愛好...的驗(yàn)證)
- Java后臺(tái)開發(fā)之表單提交之前驗(yàn)證
- java后臺(tái)防止表單重復(fù)提交方法詳解
相關(guān)文章
SrpingDruid數(shù)據(jù)源加密數(shù)據(jù)庫密碼的示例代碼
本篇文章主要介紹了SrpingDruid數(shù)據(jù)源加密數(shù)據(jù)庫密碼的示例代碼,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2017-10-10
springboot項(xiàng)目docker分層構(gòu)建的配置方式
在使用dockerfile構(gòu)建springboot項(xiàng)目時(shí),速度較慢,用時(shí)比較長(zhǎng),為了加快構(gòu)建docker鏡像的速度,采用分層構(gòu)建的方式,這篇文章主要介紹了springboot項(xiàng)目docker分層構(gòu)建,需要的朋友可以參考下2024-03-03
如何用Springboot Admin監(jiān)控你的微服務(wù)應(yīng)用
這篇文章主要介紹了如何用Springboot Admin監(jiān)控你的微服務(wù)應(yīng)用,幫助大家更好的理解和使用springboot框架,感興趣的朋友可以了解下。2021-01-01
SpringBoot中的配置文件加載優(yōu)先級(jí)詳解
這篇文章主要介紹了SpringBoot中的配置文件加載優(yōu)先級(jí)詳解,springboot啟動(dòng)會(huì)掃描以下位置的application.properties或者application.yml文件作為Spring?boot的默認(rèn)配置文件,需要的朋友可以參考下2024-01-01
MyBatis綁定錯(cuò)誤提示BindingException:Invalid bound statement (not f
這篇文章主要介紹了MyBatis綁定錯(cuò)誤提示BindingException:Invalid bound statement (not found)的解決辦法,非常不錯(cuò),具有參考借鑒價(jià)值,需要的的朋友參考下吧2017-01-01
SpringBoot+Redis Bitmap實(shí)現(xiàn)活躍用戶統(tǒng)計(jì)
Redis的Bitmap數(shù)據(jù)結(jié)構(gòu)是一種緊湊的位圖,它可以用于實(shí)現(xiàn)各種場(chǎng)景,其中統(tǒng)計(jì)活躍用戶是一種經(jīng)典的業(yè)務(wù)場(chǎng)景,下面我們就來學(xué)習(xí)一下SpringBoot如何利用Redis中的Bitmap實(shí)現(xiàn)活躍用戶統(tǒng)計(jì)吧2023-11-11

