`

j2ee13:jsp02,request对象,中文乱码处理,url传参

    博客分类:
  • j2ee
 
阅读更多

第一:基本概念

1.服务器端的执行过程:1.接收请求,2.处理请求,3.响应请求
2.jsp和servlet服务器端开发技术:
 ----1.运行在服务器端执行,执行之后给客户端返回一个静态网页(html网页)
 ----2.获取请求,处理请求,响应请求
3.jsp的内置对象:由jsp执行的时候自动创建对象(jsp页面是由服务器执行的)
 --out对象:向客户端输出数据
 --request对象:(其实也可以当成容器,用容器封装内容了)。request用来封装客户端向服务器的请求和请求的数据
       ---getparameter方法:是获取请求的参数,接收的返回值是String
       ---request.setCharacterEncoding("utf-8");:客户端数据要以utf-8的格式发送给tomcat服务器,只适用于post请求
4。编码:数据从页面发送数据的时候默认是iso-8859-1的编码格式把数据库转化成这个格式的十六进制,然后服务器端接收二进制后在、
 对这个二进制数据进行解码
  ----1.指定客户端发送时候的数据编码格式:request.setCharacterEncoding("utf-8");注意这种处理只适用于post请求。
5.提交方式:(面试)
   ---get:提交之后的地址是项目的全路径+问号传参(传参的时候中文都转化成了编码)
   ---post:提交之后的地址是项目的全路径
   区别:1.地址不一样,2.中文乱码的处理方式不一样3.安全性不一样(post提交的安全性高。因为get提交把内容都放在了地址栏上用问号链接了)4.get方式提交的数据长度有限,最大能够提交255个字符,而post可以向服务器提交大量数据

6.url传参(也叫做地址重写):url指的是项目的全路径。url传参跟get提交很相似,也就是超链接的时候使用问号传参(参数的值就是客户端要传递的数据)
7.解码:服务器端响应给客户端时候需要对数据(这个数据是客户端发送给服务器端的时候编码过的数据)进行解码。也就是把二进制还原成汉字(系统默认的解码方式是iso-8859-1)
总结:1.乱码出现的两种情况:第一是由java代码保存到数据库的时候需要先转化成十六进制的字节码,这叫编码
                          第二种情况是把由电脑显示给客户端,因此需要把十六进制的内容转化成中文,这个就是解码
      2.编码与解码其实就是字符串string和字节数组getByte之间的转化
      3.request.setCharacterEncoding("utf-8")是在发送请求的时候就要求数据是utf-8格式进行编码的。
       而new String(saddress1.getBytes("iso-8859-1"),"UTF-8");表示发送请求的时候是默认的iso-8859-1方式对中文进行十六进制编码得到字节码,然后服务器端收到客户端的数据,处理数据,响应数据,响应数据的时候
      在堆中文乱码问题进行处理,要把这个十六进制的字节码在解码成中文。
      其中:
      saddress1.getBytes("iso-8859-1")的作用:由于客户端发送请求的时候使用了iso-8859-1进行了编码,
                                            现在这句话就是对客户端发送来的字节码进行解码操作,解码之后就形成了没有编码格式的中文(字符串);
                                            getBydes得到的返回值是byte类型的数组,也就是十六进制的二进制编码
     new String(saddress1.getBytes("iso-8859-1"),"UTF-8")的作用是:对 saddress1.getBytes("iso-8859-1")得到的中文在重新用utf-8进行解码。用utf-8编码之后在从服务器端向客户端响应、
     也就是说:new String(解码,编码);

8.转发的全称是请求转发,意思是将请求向下转发。
9.乱码综合性解说:
  1.String ssex=request.getParameter("ssex");;是接收客户端请求的数据,此时接收到的数据是以iso-8859-1(服务器默认的解码方式)进行解码后的数据,因此在显示的时候就乱码了
  2.String s=new String(ssex.getBytes("iso-8859-1"),"UTF-8"):ssex.getBytes("iso-8859-1")表示我先将服务器解码后的字符串转换成字节数组,转换后就成了没解码过的字符,然后在对没编码过的字符进行重新解码
 

第二:代码

1.表单的提交页面

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <title>addStudent.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="doAddStudent.jsp" name="myform" method="get">
     <table>
      <tr>
       <td colspan="2" align="center"> 添加学生 </td>
       
      </tr>
      <tr>
       <td>姓名</td>
       <td><input type="text" name="sname" value=""/></td>
      </tr>
      <tr>
       <td>性别</td>
       <td>
       <input type="radio" name="ssex" value="男"/>男
       <input type="radio" name="ssex" value="女"/>女
       
       </td>
      </tr>
      <tr>
       <td>年龄</td>
       <td><input type="text" name="sage" value=""/></td>
      </tr>
      <tr>
       <td>地址</td>
       <td><input type="text" name="saddress" value=""/></td>
      </tr>
      <tr>
       <td>电话</td>
       <td><input type="text" name="stel" value=""/></td>
      </tr>
      <tr>
       <td><input type="submit" name="sub" value=" 提 交 "/></td>
       <td><input type="reset" name="re" value=" 重 置 "/></td>
      </tr>
     </table>
    </form>
  </body>
</html>

2.跳到这个jsp页面:添加的过程

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ page import="com.dao.*" %>
<%@ page import="com.dao.impl.*" %>
<%@ page import="com.entity.*" %>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href="<%=basePath%>">
   
    <title>My JSP 'doAddStudent.jsp' starting page</title>
   
 <meta http-equiv="pragma" content="no-cache">
 <meta http-equiv="cache-control" content="no-cache">
 <meta http-equiv="expires" content="0">   
 <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
 <meta http-equiv="description" content="This is my page">
 <!--
 <link rel="stylesheet" type="text/css" href="styles.css">
 -->

  </head>
 
  <body>
   <%
     
     //1 接收请求数据(客户端发送给服务器的数据)
     
     //中文乱码处理--将客户端发送到服务器端的数据安装UTF-8编码
     //request.setCharacterEncoding("UTF-8");
     
     String sname=request.getParameter("sname");
     
     String sage=request.getParameter("sage");
     String saddress=request.getParameter("saddress");
     String ssex=request.getParameter("ssex");
     String s=new String(ssex.getBytes("iso-8859-1"),"UTF-8");
     System.out.println("ssex="+s);
     String stel=request.getParameter("stel");
     //2 处理请求
     StudentDao dao = new StudentDaoImpl();
     Student stu = new Student();
     stu.setSname(sname);
     stu.setSage(Integer.parseInt(sage));
     stu.setSaddress(saddress);
     stu.setSsex(s);
     stu.setStel(stel);
     
    
     int row =dao.saveStudent(stu);
     //3 请求响应
     if(row>0)
     {
      out.print("<h1 align='center'>添加成功</h1>");
     }
     else
     {
      out.print("<h1 align='center'>添加失败</h1>");
     }
     
    %>
  </body>
</html>

3.删除的过程

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ page import="com.dao.*" %>
<%@ page import="com.dao.impl.*" %>
<%@ page import="com.entity.*" %>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href="<%=basePath%>">
   
    <title>My JSP 'doAddStudent.jsp' starting page</title>
   
 <meta http-equiv="pragma" content="no-cache">
 <meta http-equiv="cache-control" content="no-cache">
 <meta http-equiv="expires" content="0">   
 <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
 <meta http-equiv="description" content="This is my page">
 <!--
 <link rel="stylesheet" type="text/css" href="styles.css">
 -->

  </head>
 
  <body>
    <!-- 处理删除请求 -->
   
    <%
   
     //1 获取请求数据
     String sno=request.getParameter("sno");
     
     //2 处理请求,调用数据访问对象
     StudentDao dao = new StudentDaoImpl();
     int row=dao.deleteStudent(Integer.parseInt(sno));
         
     //3 响应
  if(row>0)
     {
      out.print("<h1 align='center'>删除成功<br/><a href='index.jsp'>首页</a></h1>");
     }
     else
     {
      out.print("<h1 align='center'>删除失败<br/><a href='index.jsp'>首页</a></h1>");
     }
     %>
    
  </body>
</html>

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics