• 解决JSP调用JavaBean出现乱码问题


    解决JSP调用JavaBean出现乱码问题

    话不多说放代码

      1 StudentFrom.java
      2 01 /**
      3 02  * 
      4 03  */
      5 04 /**
      6 05  * @author admin
      7 06  *
      8 07  */
      9 08 package school.stu;
     10 09 public class StudentForm{
     11 10     private String number;//ѧڅ
     12 11     private String name;
     13 12     private String gender;//ДѰ
     14 13     private int age;//Īd
     15 14     private String phone;//է۰
     16 15     private String email;//էדӊܾ
     17 16     private String address;//סַ
     18 17     
     19 18     public String getNumber()
     20 19     {
     21 20         return number;
     22 21     }
     23 22     public String getName()
     24 23     {
     25 24         return name;
     26 25     }
     27 26     public String getGender()
     28 27     {
     29 28         return gender;
     30 29     }
     31 30     public int getAge()
     32 31     {
     33 32         return age;
     34 33     }
     35 34     public String getPhone()
     36 35     {
     37 36         return phone;
     38 37     }
     39 38     public String getEmail()
     40 39     {
     41 40         return email;
     42 41     }
     43 42     public String getAddress()
     44 43     {
     45 44         return address;
     46 45     }
     47 46     
     48 47     public void setNumber(String newNumber)
     49 48     {
     50 49         number = newNumber;
     51 50     }
     52 51     public void setName(String newName)
     53 52     {
     54 53         name= newName;
     55 54     }
     56 55     public void setGender(String newGender)
     57 56     {
     58 57         gender = newGender;
     59 58     }
     60 59     public void setAge(int newAge)
     61 60     {
     62 61         age = newAge;
     63 62     }
     64 63     public void setPhone(String newPhone)
     65 64     {
     66 65         phone = newPhone;
     67 66     }
     68 67     public void setEmail(String newEmail)
     69 68     {
     70 69         email = newEmail;
     71 70     }
     72 71     public void setAddress(String newAddress)
     73 72     {
     74 73         address = newAddress;
     75 74     }
     76 75     
     77 76     
     78 77 }
     79 
     80 Index.jsp
     81 01 <%@ page import="school.stu.StudentForm" language="java" contentType="text/html; charset=UTF-8"
     82 02     pageEncoding="UTF-8" %>
     83 03     <!-- page import="包名.类名" 引入包 -->
     84 04 <!DOCTYPE html>
     85 05 <html>
     86 06 <head>
     87 07 
     88 08 <meta charset="UTF-8">
     89 09 <title>学生信息</title>
     90 10 
     91 11 </head>
     92 12 <body>
     93 13 <center>
     94 14 <form action="" method="post" name="addform">
     95 15 学        号:<input type="text" name="number"><br>
     96 16 姓        名:<input type="text" name="name"><br>
     97 17 性        别:<input type="text" name="gender"><br>
     98 18 年        龄:<input type="text" name="age"><br>
     99 19 电        话:<input type="text" name="phone"><br>
    100 20 电子邮件:<input type="text" name="email"><br>
    101 21 住        址:<input type="text" name="address"><br>
    102 22 <input type="submit" value="提交" name="submit">
    103 23 <input type="reset" value="重置">
    104 24 
    105 25 </form>
    106 26 </center>
    107 27 <jsp:useBean id="student" class="school.stu.StudentForm" scope="request"></jsp:useBean>
    108 28 <!-- id给bean起的名字 | class bean类 |scope 指定对象的范围 -->
    109 29 
    110 30 <!-- <jsp:setProperty name="student" property="number" 
    111 31                                     value="201711010111"/>
    112 32  -->
    113 33 
    114 34 <jsp:setProperty name="student" property="number" 
    115 35                                     param="number"/>
    116 36 <jsp:setProperty name="student" property="name" 
    117 37                                     param="name"/>
    118 38 <jsp:setProperty name="student" property="gender" 
    119 39                                     param="gender"/>
    120 40 <jsp:setProperty name="student" property="age" 
    121 41                                     param="age"/>
    122 42 <jsp:setProperty name="student" property="phone" 
    123 43                                     param="phone"/>
    124 44 <jsp:setProperty name="student" property="email" 
    125 45                                     param="email"/>
    126 46 <jsp:setProperty name="student" property="address" 
    127 47                                     param="address"/>
    128 48 <!-- 通过HTTP表单的参数的值来设置bean的相应属性的值 -->    
    129 49                                 
    130 50 <jsp:getProperty name="student" property="name"/>
    131 51 <!-- 
    132 52 <jsp:getProperty name="student" property="name"/>
    133 53 <jsp:getProperty name="student" property="name"/>
    134 54 <jsp:getProperty name="student" property="name"/>
    135 55 <jsp:getProperty name="student" property="name"/>
    136 56 <jsp:getProperty name="student" property="name"/>
    137 57 <jsp:getProperty name="student" property="name"/> -->
    138 58 
    139 59 <!-- 获得bean的属性值,并将这个值以字符串的形式显示给用户 -->
    140 60 
    141 61 </body>
    142 62 </html>

    Javabean文件没啥问题。可是Index.jsp黄色高亮部分都是utf-8了也还是会乱码

    只需在index.jsp中插入

    <meta http-equiv="content-type" content="text/html; charset=GB2312">

    并把上述高亮部分utf-8全部改为GB2312即可

    或者

    在index.jsp中插入

    <meta http-equiv="content-type" content="text/html; charset=utf-8">

    高亮部分全写utf-8

    更改后的index.jsp如下:

     1 <%@ page import="school.stu.StudentForm" language="java" contentType="text/html; charset=GB2312"//或utf-8(原谅这个c++风格的注释)
     2     pageEncoding="GB2312" %>//或utf-8
     3     <!-- page import="包名.类名" 引入包 -->
     4 <% request.setCharacterEncoding("GB2312");%>//或utf-8
     5 <!DOCTYPE html>
     6 <html>
     7 <head>
     8 <meta http-equiv="content-type" content="text/html; charset=GB2312">//或utf-8
     9 <meta charset="GB2312">//或utf-8
    10 <title>学生信息</title>
    11 
    12 </head>
    13 <body>
    14 <center>
    15 <form action="" method="post" name="addform">
    16 学        号:<input type="text" name="number"><br>
    17 姓        名:<input type="text" name="name"><br>
    18 性        别:<input type="text" name="gender"><br>
    19 年        龄:<input type="text" name="age"><br>
    20 电        话:<input type="text" name="phone"><br>
    21 电子邮件:<input type="text" name="email"><br>
    22 住        址:<input type="text" name="address"><br>
    23 <input type="submit" value="提交" name="submit">
    24 <input type="reset" value="重置">
    25 
    26 </form>
    27 </center>
    28 <jsp:useBean id="student" class="school.stu.StudentForm" scope="request"></jsp:useBean>
    29 <!-- id给bean起的名字 | class bean类 |scope 指定对象的范围 -->
    30 
    31 <!-- <jsp:setProperty name="student" property="number" 
    32                                     value="201711010111"/>
    33  -->
    34 
    35 <jsp:setProperty name="student" property="number" 
    36                                     param="number"/>
    37 <jsp:setProperty name="student" property="name" 
    38                                     param="name"/>
    39 <jsp:setProperty name="student" property="gender" 
    40                                     param="gender"/>
    41 <jsp:setProperty name="student" property="age" 
    42                                     param="age"/>
    43 <jsp:setProperty name="student" property="phone" 
    44                                     param="phone"/>
    45 <jsp:setProperty name="student" property="email" 
    46                                     param="email"/>
    47 <jsp:setProperty name="student" property="address" 
    48                                     param="address"/>
    49 <!-- 通过HTTP表单的参数的值来设置bean的相应属性的值 -->    
    50                                 
    51 <jsp:getProperty name="student" property="number"/><br/>
    52 <jsp:getProperty name="student" property="name"/><br/>
    53 <jsp:getProperty name="student" property="gender"/><br/>
    54 <jsp:getProperty name="student" property="age"/><br/>
    55 <jsp:getProperty name="student" property="phone"/><br/>
    56 <jsp:getProperty name="student" property="email"/><br/>
    57 <jsp:getProperty name="student" property="address"/><br/>
    58 <!-- 
    59 <jsp:getProperty name="student" property="name"/>
    60 <jsp:getProperty name="student" property="name"/>
    61 <jsp:getProperty name="student" property="name"/>
    62 <jsp:getProperty name="student" property="name"/>
    63 <jsp:getProperty name="student" property="name"/>
    64 <jsp:getProperty name="student" property="name"/> -->
    65 
    66 <!-- 获得bean的属性值,并将这个值以字符串的形式显示给用户 -->
    67 
    68 </body>
    69 </html>

    完美!!

  • 相关阅读:
    Zabbix监控MySQL免密码设置
    NFS文件服务器搭建
    Glufster挂载失败Mount failed. Please check the log file for more details解决办法
    CentOS和Redhat救援模式
    CentOS和Redhat单用户模式
    EXSI中Linux安装tools
    Redhat7.5安装glusterfs4
    思科交换机根据mac地址限制主机
    怎么关闭win10防火墙
    [0] WCF开发下,提示HTTP 无法注册 URL 进程不具有此命名空间的访问权限
  • 原文地址:https://www.cnblogs.com/zhanjiahui/p/10787666.html
Copyright © 2020-2023  润新知