最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Servlet的5種方式實(shí)現(xiàn)表單提交(注冊小功能),后臺獲取表單數(shù)據(jù)實(shí)例

 更新時(shí)間:2017年05月22日 16:21:06   作者:檸檬旋風(fēng)腿  
這篇文章主要介紹了Servlet的5種方式實(shí)現(xiàn)表單提交(注冊小功能),后臺獲取表單數(shù)據(jù)實(shí)例,非常具有實(shí)用價(jià)值,需要的朋友可以參考下

用servlet實(shí)現(xiàn)一個(gè)注冊的小功能 ,后臺獲取數(shù)據(jù)。

注冊頁面:

  

注冊頁面代碼 :

<!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="點(diǎn)擊注冊">
  </form>
</body>
</html>

人員實(shí)體類: 注意:人員實(shí)體類要與表單中的name一致,約定要優(yōu)于編碼

package com.chensi.bean;

//實(shí)體類中的字段要與表單中的字段一致,約定優(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頁面(后臺接收數(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 獲得填寫的表單數(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");
    //獲取傳過來的表單數(shù)據(jù),根據(jù)表單中的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);
  }

}

得到的數(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 獲得填寫的表單數(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");
    //獲取傳過來的表單數(shù)據(jù),根據(jù)表單中的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);
  }
  
  

}

得到的數(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 獲得填寫的表單數(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");
    //獲取傳過來的表單數(shù)據(jù),根據(jù)表單中的name獲取所填寫的值
    
      
      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 工具來進(jìn)行封裝數(shù)據(jù)(ps:這個(gè)Benautils工具,Struts框架就是使用這個(gè)來獲取表單數(shù)據(jù)的哦?。?/p>

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 獲得填寫的表單數(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");
    //獲取傳過來的表單數(shù)據(jù),根據(jù)表單中的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);
  }
  
  
}

得到的結(jié)果:

   

 接收方法 方式五: 使用inputStream流來進(jìn)行接收(一般字符串啥的不用這個(gè)方法,一般是文件上傳下載時(shí)候才會使用這種方法)因?yàn)榻邮盏降淖址鞣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 獲得填寫的表單數(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");
    //獲取傳過來的表單數(shù)據(jù),根據(jù)表單中的name獲取所填寫的值
    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)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • 創(chuàng)建并運(yùn)行一個(gè)java線程方法介紹

    創(chuàng)建并運(yùn)行一個(gè)java線程方法介紹

    這篇文章主要介紹了創(chuàng)建并運(yùn)行一個(gè)java線程,涉及線程代碼示例,具有一定參考價(jià)值,需要的朋友可以了解下。
    2017-11-11
  • Java中接口和抽象類的區(qū)別詳解

    Java中接口和抽象類的區(qū)別詳解

    這篇文章主要介紹了Java中接口和抽象類的區(qū)別詳解,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2020-07-07
  • java使用@Scheduled注解執(zhí)行定時(shí)任務(wù)

    java使用@Scheduled注解執(zhí)行定時(shí)任務(wù)

    這篇文章主要給大家介紹了關(guān)于java使用@Scheduled注解執(zhí)行定時(shí)任務(wù)的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2021-01-01
  • Java中臨時(shí)文件目錄的使用

    Java中臨時(shí)文件目錄的使用

    :Java提供了系統(tǒng)屬性java.io.tmpdir來獲取默認(rèn)臨時(shí)文件目錄,適用于不同操作系統(tǒng),使用Files.createTempFile方法創(chuàng)建臨時(shí)文件,并在不需要時(shí)應(yīng)及時(shí)刪除,下面就來介紹一下
    2024-10-10
  • Java中LinkedHashSet的源碼剖析

    Java中LinkedHashSet的源碼剖析

    這篇文章主要介紹了Java中LinkedHashSet的源碼剖析,LinkedHashSet是HashSet的子類,LinkedHashSet底層是一個(gè)LinkedHashMap,底層維護(hù)了一個(gè)數(shù)組+雙向鏈表,需要的朋友可以參考下
    2023-09-09
  • Java?8中的Collectors?API介紹

    Java?8中的Collectors?API介紹

    這篇文章主要介紹了Java?8中的Collectors?API,Stream.collect()是Java?8的流API的終端方法之一。它允許我們對流實(shí)例中保存的數(shù)據(jù)元素執(zhí)行可變折疊操作,下文相關(guān)內(nèi)容需要的小伙伴可以參考一下
    2022-04-04
  • Java中的PowerMock使用實(shí)踐

    Java中的PowerMock使用實(shí)踐

    這篇文章主要介紹了Java中的PowerMock使用實(shí)踐,@PrepareForTest和@RunWith是成對出現(xiàn)的,一般@RunWith(PowerMockRunner.class),@PrepareForTest的值是引用的靜態(tài)方法或私有方法的類,需要的朋友可以參考下
    2023-12-12
  • Spring Cloud-Feign服務(wù)調(diào)用的問題及處理方法

    Spring Cloud-Feign服務(wù)調(diào)用的問題及處理方法

    Feign 是一個(gè)聲明式的 REST 客戶端,它用了基于接口的注解方式,很方便實(shí)現(xiàn)客戶端配置。接下來通過本文給大家介紹Spring Cloud-Feign服務(wù)調(diào)用,需要的朋友可以參考下
    2021-10-10
  • SpringBoot 入門教程之引入數(shù)據(jù)傳輸層的方法

    SpringBoot 入門教程之引入數(shù)據(jù)傳輸層的方法

    這篇文章主要介紹了SpringBoot 入門教程之引入數(shù)據(jù)傳輸層的方法,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2020-07-07
  • 分析Java中的類加載問題

    分析Java中的類加載問題

    很多時(shí)候提到類加載,大家總是沒法馬上回憶起順序,這篇文章會用一個(gè)例子為你把類加載的諸多問題一次性澄清
    2021-06-06

最新評論

梁河县| 屏东县| 正定县| 佛冈县| 甘洛县| 仪征市| 故城县| 台中县| 祥云县| 高邮市| 铜梁县| 荔波县| 乐安县| 余姚市| 山阳县| 阳西县| 正蓝旗| 涿州市| 鄄城县| 兰州市| 康平县| 博野县| 桓台县| 金乡县| 丹寨县| 兴安县| 新平| 邵阳市| 寻乌县| 余姚市| 石泉县| 泗水县| 仪陇县| 修武县| 舒兰市| 泸州市| 东丽区| 万州区| 长岭县| 南宁市| 临安市|