Servlet 表单数据

很多情况下,需要传递一些信息,从浏览器到 Web 服务器,最终到后台程序。浏览器使用两种方法可将这些信息传递到 Web 服务器,分别为 GET 方法和 POST 方法。

使用 Servlet 读取表单数据

不区分GET和POST

Servlet 处理表单数据,这些数据会根据不同的情况使用不同的方法自动解析:

  • getParameter():您可以调用 request.getParameter() 方法来获取表单参数的值。
  • getParameterValues():如果参数出现一次以上,则调用该方法,并返回多个值,例如复选框。
  • getParameterNames():如果您想要得到当前请求中的所有参数的完整列表,则调用该方法。
  • 注意:如果表单提交的数据中有中文数据则需要转码:

    String name =new String(request.getParameter("name").getBytes("ISO8859-1"),"UTF-8");
  • tomcat默认用ISO8859-1解码浏览器发来的数据,用此编码重新编码,再用实际编码解码为字符串即可获得原始数据

  • 注意!POST方式获得的数据会被受上述过程影响,GET方式附加在URL上的数据似乎没有被tomcat默认解码,不需要调整编码即可使用


读取所有的表单参数

以下是通用的实例,使用 HttpServletRequest  request的 request.getParameterNames() 方法读取所有可用的表单参数name的枚举(自动去重名)。该方法返回一个枚举,其中包含未指定顺序的参数名。

一旦我们有一个枚举,我们可以以标准方式循环枚举,使用 hasMoreElements() 方法来确定何时停止,使用 nextElement() 方法来获取每个参数的name。

再用String[] request.getParameterValues(name)获得每个name可能具有的values 根据返回String[] values的values.length分单value和多value进行处理。


附简单例子:

ParseForm.java

package hentai.servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.Enumeration; 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 implementation class Main
*/
@WebServlet("/ParseForm")
public class ParseForm extends HttpServlet {
private static final long serialVersionUID = 1L; //
private String msg = ""; /**
* Default constructor.
*/
public ParseForm() {
// TODO Auto-generated constructor stub
} @Override
public void init() throws ServletException {
// TODO 自动生成的方法存根
super.init();
//
msg = "1314233";
} /**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
* response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
// TODO Auto-generated method stub
//判断提交方式以决定如何转换编码
boolean isPOST = false;
if(request.getMethod().toUpperCase().equals("POST"))
isPOST = true;
//设置响应格式与编码
response.setContentType("text/html;charset=utf-8");
//获得响应输出
PrintWriter out = response.getWriter();
//输出
out.println("<h1>" + request.getMethod() + "</h1>");
//遍历请求参数并打印回浏览器
Enumeration<String> allNames = request.getParameterNames();
while (allNames.hasMoreElements()) {
String name = allNames.nextElement();
String[] values = request.getParameterValues(name);
if (values.length == 1) {
String value = values[0];
if(isPOST)
value = new String(value.getBytes("ISO8859-1"), "utf-8");
out.println("<p>" + name + ":" + value + "</p>");
} else {
for (String value : values) {
if(isPOST)
value = new String(value.getBytes("ISO8859-1"), "utf-8");
out.println("<p>" + name + "s:" + value + "</p>");
}
} }
// out.println(request.getContextPath());
//response.sendRedirect("form.html");
} /**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
* response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
// TODO Auto-generated method stub
doGet(request, response);
} @Override
public void destroy() {
// TODO 自动生成的方法存根
super.destroy();
} }

 form.html

<!DOCTYPE html>
<html lang="en"> <head>
<meta charset="UTF-8">
<title>form</title>
</head> <body> <form method="post" action="ParseForm" id="dataForm">
<input type="text" name="uName" />
<input type="password" name="uPwd" /> <input type="radio" name="sex" value="1" />男
<input type="radio" name="sex" value="0" />女
<input type="radio" name="sex" value="-1" />保密 <input type="checkbox" name="hobby" value="game" />游戏
<input type="checkbox" name="hobby" value="music" />音乐
<input type="checkbox" name="hobby" value="write" />写作 <input type="submit" value="submit" />
<!--<input type="button" value="提交" />
<script>
var dataFormChilds = document.getElementById("dataForm").getElementsByTagName("input");
for (var i = 0; i < dataFormChilds.length; i++) {
if (dataFormChilds[i].getAttribute("type") == "button") {
dataFormChilds[i].onclick = function () {
alert(this);
this.parentElement.submit();
}
}
}
</script>-->
</form> </body> </html>

单选按钮名字需要一样,Servlet获得参数时获得是被选中的radio的value

多选框,......获得的是被选中的框的value的值的数组

其他待补充......

Servlet处理表单数据的更多相关文章

  1. 1.3(学习笔记)Servlet获取表单数据

    一.Servlet获取表单数据 表单提交数据经由Servlet处理,返回一个处理结果显示在页面上, 那么如何获取表单提交的参数进出相应的处理呢? 主要用到以下方法: String  getParame ...

  2. 用Servlet获取表单数据

    用Servlet获取表单数据 在webroot下新建userRegist2.jsp 代码如下: <%@ page contentType="text/html;charset=gb23 ...

  3. JSP简单练习-用Servlet获取表单数据

    // javaBean代码 package servlet; import java.io.*; import javax.servlet.*; import javax.servlet.http.* ...

  4. Servlet 笔记-读取表单数据

    Servlet 处理表单数据,这些数据会根据不同的情况使用不同的方法自动解析: getParameter():您可以调用 request.getParameter() 方法来获取表单参数的值. get ...

  5. Servlet学习笔记(二):表单数据

    很多情况下,需要传递一些信息,从浏览器到 Web 服务器,最终到后台程序.浏览器使用两种方法可将这些信息传递到 Web 服务器,分别为 GET 方法和 POST 方法. 1.GET 方法:GET 方法 ...

  6. Servlet 表单数据

    很多情况下,需要传递一些信息,从浏览器到 Web 服务器,最终到后台程序.浏览器使用两种方法可将这些信息传递到 Web 服务器,分别为 GET 方法和 POST 方法. GET 方法 GET 方法向页 ...

  7. IT兄弟连 JavaWeb教程 Servlet表单数据

    很多情况下,需要传递一些信息,从浏览器到Web服务器,最终到后台程序.浏览器使用两种方法可将这些信息传递到Web服务器,分别为GET方法和POST方法. 1.GET方法 GET 方法向页面请求发送已编 ...

  8. Servlet表单数据

    1.GET 方法 GET 方法向页面请求发送已编码的用户信息.页面和已编码的信息中间用 ? 字符分隔,如下所示: http://www.test.com/hello?key1=value1&k ...

  9. Servlet的5种方式实现表单提交(注册小功能),后台获取表单数据

    用servlet实现一个注册的小功能 ,后台获取数据. 注册页面: 注册页面代码 : <!DOCTYPE html> <html> <head> <meta ...

随机推荐

  1. Codeforces Round #266 (Div. 2)B(暴力枚举)

    很简单的暴力枚举,却卡了我那么长时间,可见我的基本功不够扎实. 两个数相乘等于一个数6*n,那么我枚举其中一个乘数就行了,而且枚举到sqrt(6*n)就行了,这个是暴力法解题中很常用的性质. 这道题找 ...

  2. CodeForces - 156C:Cipher (不错的DP)

    Sherlock Holmes found a mysterious correspondence of two VIPs and made up his mind to read it. But t ...

  3. Mybatis_generator自动化生成代码

    1.Run as 2.ok

  4. 【模板】【学习笔记】noip数学

    一.素数 欧拉筛 void prime(){ check[]=; ;i<=n;i++){ if(!check[i])prim[++cnt]=i;//这个if语句后面没有大括号!! ;j<= ...

  5. Yii 常用命令

    一.Yii的Active Recorder包装了很多. 特别是把SQL中 把where,order,limit,IN/not IN,like等常用短句都包含进CDbCriteria这个类中去,这样整个 ...

  6. Directx 9 VS2015环境搭建

    安装好Directx9 sdk和vs2015后 打开vs,新建项目 --> c++项目  -->win32控制台应用程序-->空项目 创建项目后,右键项目属性, 包含目录 D:\Pr ...

  7. Linux 终端 忽略大小写

    忘了在哪里看到的了,记录一下. 在-/.inputrc中加入一行 set completion-ignore-case on 搞定! 这样在终端输入.补全时就忽略大小写了.当然,Linux本身还是区分 ...

  8. OpenWrt添加启动脚本

    1.在 /etc/init.d 目录下建立文件 vi silabs #!/bin/sh /etc/rc.common # Copyright (C) 2006 OpenWrt.org START=93 ...

  9. 摘之知乎网友...PHYTIN学习

    作者:东瓜王链接:https://www.zhihu.com/question/19593179/answer/23746083来源:知乎著作权归作者所有.商业转载请联系作者获得授权,非商业转载请注明 ...

  10. java代码继承------多层继承

    总结:继承.方法的重要性, 运行结果显示: class A is callingclass B is callingclass C is calling package com.addd; //jav ...