通过表单收集客户机数据

来源:互联网 发布:chantelle sydney知乎 编辑:程序博客网 时间:2024/05/17 23:13

一. 在web开发中,服务端通常要搜集客户端发送过来的数据,而这些数据通常被服务器封装在request对象中,服务端通过request对象的方法获取客户机提交过来的数据进行一系列的操作,然而客户端以什么方式提交数据 那?通常有两种方式:

1.通过超链接的方式:(如果url后面跟了中文数据,需要进行编码后提交)-->
     <a href="/day06/servlet/RequestDemo2?username=zhangsan">点击链接</a>
 
  2.通过表单的方式:
  <form action="/day06/servlet/RequestDemo2" method="post">
  用户名a:<input type="text" name="username"><br/>
  用户名b:<input type="text" name="username"><br/>
  密码:<input type="password" name="password"><br>
  <input type="submit" value="提交"> 
  </form>


eg.

客户机端提交表单:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"><html>  <head>    <title>form.html</title>    <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">    <meta http-equiv="description" content="this is my page">    <meta http-equiv="content-type" content="text/html; charset=UTF-8">        <!--<link rel="stylesheet" type="text/css" href="./styles.css">-->  </head>    <body>    <form action="/day06/servlet/RequestDemo3" method="post">  用户名:<input type="text" name="username"><br/>  密码:<input type="password" name="password"><br>  性别:  <input type="radio" name="gender" value="man">男  <input type="radio" name="gender" value="female">女  地址:  <select name="city">  <option value="beijing">北京</option>  <option valu="shanghai">上海</option>  <option value="nanjing">南京</option>  <option value="shenyang">沈阳</option>  </select><br>  爱好:  <input type="checkbox" name="likes" value="sing"/>唱歌  <input type="checkbox" name="likes" value="sing"/>唱歌  <input type="checkbox" name="likes" value="dance"/>跳舞  <input type="checkbox" name="likes" value="basketball"/>篮球  <input type="checkbox" name="likes" value="football"/>足球  <input type="checkbox" name="likes" value="draw"/>画画  <br/>  简介:  <textarea  name="discribe" rows="8" cols="80">plases introduce yourself</textarea><br>  头像:  <input type="file" name="image"><br>    <input type="hidden" name="id" value="888888">    <input type="submit" value="提交">   </form>  </body></html>

服务器端servlet

package cn.guoqing.request;import java.io.IOException;import java.io.PrintWriter;import javax.servlet.ServletException;import javax.servlet.http.HttpServlet;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;public class RequestDemo3 extends HttpServlet {public void doGet(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException {String username =request.getParameter("username");System.out.println("username="+username);String password =request.getParameter("password");System.out.println("password="+password);String gender =request.getParameter("gender");System.out.println("gender="+gender);String city =request.getParameter("city");System.out.println("city="+city);String[] values=request.getParameterValues("likes");for(int i=0;values!=null&&i<values.length;i++){System.out.println("likes="+values[i]);}String discribe =request.getParameter("discribe");System.out.println("discribe="+discribe);String id =request.getParameter("id");System.out.println("id="+id);}public void doPost(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException {doGet(request,response);}}

 

 

二.Request乱码问题:

    原因:在客户机的浏览器与服务端的Servlet之间进行数据传输时,会通过服务器,服务器会将客户机会将客户机的发送的请求信息封装到request对象中,而将servlet的响应信息封装到response对象中。在这个过程中所有的数据都需要进行转码,将其转化为二进制数。客户机到服务器的过程中,浏览器会将数据按照浏览器所使用的编码表编码后将数据发送到服务器,服务器端的servece中的request接收到数据会按照服务器的编码表查找(如TOMCAT是老美写的它的编码表就是iso8859-1)这时只需要设置request的编码表即可;从服务器端向客户机端返回数据时设置response的编码,再设置响应头的编码(告诉客户机以什么编码表打开)即可。

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"><html>  <head>    <title>中文乱码问题</title>    <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">    <meta http-equiv="description" content="this is my page">    <meta http-equiv="content-type" content="text/html; charset=UTF-8">        <!--<link rel="stylesheet" type="text/css" href="./styles.css">-->  </head>    <body>   <form action="/day06/servlet/RequestDemo4" method="post">  用户名a:<input type="text" name="username"><br/>  <input type="submit" value="提交">   </form>    <form action="/day06/servlet/RequestDemo4" method="get">  用户名b:<input type="text" name="username"><br/>  <input type="submit" value="提交">   </form>    <!--超链接提交的中文数据,服务器处理乱码只能用手工处理-->   <a href="/day06/servlet/RequestDemo4?username=张三">点击链接</a>  </body></html>


 

 

package cn.guoqing.request;import java.io.IOException;import java.io.PrintWriter;import java.io.UnsupportedEncodingException;import javax.servlet.ServletException;import javax.servlet.http.HttpServlet;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;public class RequestDemo4 extends HttpServlet {public void doGet(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException {//test1(request);test3(request, response);}//测试题private void test3(HttpServletRequest request,HttpServletResponse response)throws IOException {
//获取数据request.setCharacterEncoding("UTF-8");String value=request.getParameter("username");//返回数据response.setContentType("text/html;charset=gb2312");response.getWriter().write(value);}//解决get提交的乱码(手工处理):表单、超链接private void test2(HttpServletRequest request)throws UnsupportedEncodingException {String value=request.getParameter("username");//处理乱码String tranvalue=new String(value.getBytes("iso8859-1"),"UTF-8");System.out.println("username="+tranvalue);}//解决post提交的乱码private void test1(HttpServletRequest request)throws UnsupportedEncodingException {//只对post提交有效request.setCharacterEncoding("UTF-8");String value=request.getParameter("username");System.out.println("username="+value);}public void doPost(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException {doGet(request,response);}}



 

原创粉丝点击