| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 1106 人关注过本帖
标题:jsp 乱码 怎么解决?
只看楼主 加入收藏
PStudyJava
Rank: 1
来 自:郑州
等 级:新手上路
帖 子:1
专家分:0
注 册:2012-11-8
结帖率:0
收藏
已结贴  问题点数:20 回复次数:10 
jsp 乱码 怎么解决?
<%@ page language="java" import="java.sql.*" pageEncoding="utf-8"%>
<%!
    String Spname = "请输入商品名称";
    String pname1;
//转码代码
    String changeString(String str){
        try{
        byte[]b = str.getBytes("ISO-8859-1");
        str = new String(b,"utf-8");
        }catch(Exception e){
            
        }
        return str;
    }
%>
<html>
       <body>
        <form action="" method="post">
            <%
                request.setCharacterEncoding("utf-8");
                String pname = changeString(request.getParameter("pname"));
                pname1 = pname;
                System.out.println(pname);
                session.setAttribute("Rpname", pname);
                Spname = (String) session.getAttribute("Rpname");
                if (Spname == null) {
                    Spname = "请输入商品名称";
                }
            %>
            <center>
                您要检索商品为:
                <input type="text" name="pname" value="<%=Spname%>" />
                <input type="submit" value="检索" />
                <br />
                <br />
                <br />
                <%
                    String url = "jdbc:oracle:thin:@localhost:1521:orcl";
                    String name = "system";
                    String paw = "Sa123456";
                    Connection con = null;
                    PreparedStatement ps = null;
                    ResultSet rs = null;

                    try {
                        Class.forName("oracle.jdbc.driver.OracleDriver");
                        con = DriverManager.getConnection(url, name, paw);
                        //String sql1 = "select p_image,p_name,p_type,p_price,p_quantity,p_time from shop_product where p_name like '%"+pname+"%'";
                        String sql = "select p_image,p_name,p_type,p_price,p_quantity,p_time from shop_product where p_name like ?";
                        ps = con.prepareStatement(sql,ResultSet.TYPE_SCROLL_INSENSITIVE,ResultSet.CONCUR_READ_ONLY);
                        pname = "%" + pname + "%";
                        ps.setString(1, pname);
                        rs = ps.executeQuery();
                        }
                        catch (Exception e)
                        
                        {
                            System.out.print("异常");
                            e.printStackTrace();
                        }
                %>
                <table border="1" cellpadding="10" cellspacing="0">
                    <tr>
                        <th>
                            商品图片
                        </th>
                        <th>
                            商品类型
                        </th>
                        <th>
                            商品价格
                        </th>
                        <th>
                            商品数量
                        </th>
                        <th>
                            上架日期
                        </th>
                    </tr>
                    <%
                        String sPage = request.getParameter("page");
                        if(sPage==null||sPage.equals(""))
                        {
                            sPage = "1";
                        }
                        int currentPage = Integer.parseInt(sPage.trim());//当前页
                        int pageSize = 10;//页面显示行数
                        int maxPage =0;//页面数变量
                        int recordCount = 0;//总行变量
                        rs.last();//移到最后一行
                        recordCount = rs.getRow();//获取总行数
                        maxPage = (recordCount%pageSize==0)?(recordCount/pageSize):((recordCount/pageSize)+1);//获取页面总数
                        if(rs.absolute((currentPage-1)*pageSize+1))//指针移动到这一行
                        {
                            for(int i=0;i<pageSize;i++)
                            {
                        %>
                        <tr>
                            <td>
                                <img src="<%=rs.getString("p_image")%>" width="50" height="50" />
                            </td>
                            <td><%=rs.getString("p_name")%></td>
                            <td><%=rs.getString("p_price")%></td>
                            <td><%=rs.getInt("p_quantity")%></td>
                            <td><%=rs.getString("p_time")%></td>
                        </tr>
                        <%
                                if(!rs.next()){break;}   
                            }
                        }
                        if(currentPage>1)
                        {
                        %>
                            <a href="index.jsp?pname=<%= pname1%>&page=<%= "1"%>">首页</a>
                            <a href="index.jsp?pname=<%= pname1%>&page=<%= currentPage-1+""%>">上一页</a>
                        <%
                        }
                        if(currentPage<maxPage)
                        {
                         %>   
                            <a href="index.jsp?pname=<%= pname1%>&page=<%= currentPage+1+""%>">下一页</a>
                            <a href="index.jsp?pname=<%= pname1%>&page=<%= maxPage%>">尾页</a>
                        <%
                        }
                            rs.close();
                            ps.close();
                            con.close();
                        %>
                </table>
            </center>
        </form>
    </body>
</html>
这是我写的分页代码  当我汉字查询时  出现“??”  可是我已经转码了 为什么还乱码啊  我有点抓狂  水平没到啊!  求求各位大神能帮帮小弟一把  可能有点浪费您的一点时间  但也许是您的一句话 拯救了我的未来 拜谢  以前都是在百度 可是百度允许的字数太少 没法说清楚 所以就20分 全拿出来了 刚申请的 刚申请的 别嫌少 再次谢谢
搜索更多相关主题的帖子: html java return action import 
2012-11-08 18:18
mengqingguo
Rank: 2
等 级:论坛游民
帖 子:25
专家分:36
注 册:2012-11-1
收藏
得分:3 
是不是没加矢量字体库 啊?
2012-11-09 15:56
星112
Rank: 5Rank: 5
来 自:山西朔州
等 级:职业侠客
威 望:2
帖 子:94
专家分:342
注 册:2012-6-22
收藏
得分:3 
在页面上显示的就是乱码?是不是可能工程编码的设置不对啊
2012-11-09 21:24
北方凌云
Rank: 1
等 级:新手上路
威 望:1
帖 子:7
专家分:3
注 册:2012-11-3
收藏
得分:3 
你先看看jsp页面的编码设置过来了没,就是右键你的jsp看看它的编码是不是utf-8,或者把页面的utf-8改成gbk试试
2012-11-11 11:18
这一刻的呼吸
Rank: 1
等 级:新手上路
帖 子:4
专家分:5
注 册:2012-11-11
收藏
得分:3 
回复 楼主 PStudyJava
java.sql.*" pageEncoding="utf-8"%>
将utf-8改成gb2312
2012-11-11 21:34
xiaxiayige
Rank: 1
等 级:新手上路
帖 子:16
专家分:7
注 册:2011-9-8
收藏
得分:3 
lz你这个代码看着好乱,java代码还是专门写一个serlvet吧!
在serlvet中可以这样转编码
String shopName=new String(request.getParameter("shopName").getByte("iso-8859-1"),"utf-8")
jsp页面中设置顶上页面编码应该就可以了吧!
2012-11-13 18:03
逍遥茕狼
Rank: 1
等 级:新手上路
帖 子:5
专家分:8
注 册:2012-11-13
收藏
得分:3 
改成gbk就可以啦
2012-11-13 19:56
逍遥茕狼
Rank: 1
等 级:新手上路
帖 子:5
专家分:8
注 册:2012-11-13
收藏
得分:0 
iso-8859-1这个不行  只能是gbk才能解决乱码
2012-11-13 19:57
fvv
Rank: 1
等 级:新手上路
帖 子:3
专家分:3
注 册:2012-11-15
收藏
得分:3 
在项目属性中,在resource把text file encoding 中的other改成 utf-8就ok了
2012-11-15 13:43
尸体一只
Rank: 1
等 级:新手上路
帖 子:4
专家分:0
注 册:2012-11-22
收藏
得分:0 
把浏览器的编码方式改成utf-8,或者把pageEncoding="utf-8"改成pageEncoding="gb2312"试试
2012-11-22 14:59
快速回复:jsp 乱码 怎么解决?
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.022052 second(s), 9 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved