当前位置: 代码迷 >> Java Web开发 >> jsp 乱码 怎么解决?
  详细解决方案

jsp 乱码 怎么解决?

热度:415   发布时间:2012-11-08 18:18:32.0
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  

----------------解决方案--------------------------------------------------------
是不是没加矢量字体库 啊?
----------------解决方案--------------------------------------------------------
在页面上显示的就是乱码?是不是可能工程编码的设置不对啊
----------------解决方案--------------------------------------------------------
你先看看jsp页面的编码设置过来了没,就是右键你的jsp看看它的编码是不是utf-8,或者把页面的utf-8改成gbk试试
----------------解决方案--------------------------------------------------------
回复 楼主 PStudyJava
java.sql.*" pageEncoding="utf-8"%>
将utf-8改成gb2312
----------------解决方案--------------------------------------------------------
lz你这个代码看着好乱,java代码还是专门写一个serlvet吧!
在serlvet中可以这样转编码
String shopName=new String(request.getParameter("shopName").getByte("iso-8859-1"),"utf-8")
jsp页面中设置顶上页面编码应该就可以了吧!
----------------解决方案--------------------------------------------------------
改成gbk就可以啦
----------------解决方案--------------------------------------------------------
iso-8859-1这个不行  只能是gbk才能解决乱码
----------------解决方案--------------------------------------------------------
在项目属性中,在resource把text file encoding 中的other改成 utf-8就ok了
----------------解决方案--------------------------------------------------------
把浏览器的编码方式改成utf-8,或者把pageEncoding="utf-8"改成pageEncoding="gb2312"试试
----------------解决方案--------------------------------------------------------
  相关解决方案