l_zqlw
l_zqlw
采纳率0%
2016-12-30 02:04 阅读 2.7k

利用SERVLET跳转到JSP页面时显示乱码

单独打开SERVLET或者JSP,网页显示全部正常,在SERVLET里输出数据也显示正常,可是一转到JSP就全部乱码了。不仅是数据,只要是JSP里的中文就都成了?

 package SERVLET;

import java.io.IOException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import JDBC.S;
import JDBC.SDao;

@WebServlet("/ShowSServlet")
public class ShowSServlet extends HttpServlet {
    private static final long serialVersionUID = 1L;

    public ShowSServlet() {
        super();
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        response.getWriter().append("Served at: ").append(request.getContextPath());

        request.setCharacterEncoding("UTF-8");
        response.setCharacterEncoding("UTF-8");

        String currentpage=request.getParameter("currentpage");
        if(null==currentpage || "".equals(currentpage)){
            currentpage="1";
        }

        List<S> list=SDao.selectPage(Integer.parseInt(currentpage));
        int totalcount=SDao.selectS().size();
        int row=5;
        int totalpage=(totalcount%row==0)?(totalcount/row):(totalcount/row+1);

        for(S l:list){
            System.out.println(l.getSname());
        }

        request.setAttribute("currentpage", currentpage);
        request.setAttribute("totalcount", totalcount);
        request.setAttribute("row", row);
        request.setAttribute("totalpage", totalpage);
        request.setAttribute("list", list);

        request.getRequestDispatcher("ShowS.jsp").forward(request, response);
    }

    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request, response);
    }

}
 <%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
   <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<!DOCTYPE html>
<html>
<head>
<!-- <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> -->
<title>信息一览</title>
    <style>
        table
        {
            border:thin solid black;
            border-collapse:collapse;
            width:60%;
            text-align:center;
        }
        th
        {
            border:thin solid black;
        }
        td
        {
            border:thin solid black;    
        }
    </style>
    <script  type="text/javascript">
        function gotos(page){
            location.href="ShowSServlet?currentpage="+page;
        }

        function gotod(sno){
            location.href="DeleteSServlet?sno="+sno;
        }
    </script>
</head>
<body>
    <table align="center" >
        <tr>
            <th>学号</th>
            <th>姓名</th>
            <th>年龄</th>
            <th>院系</th>
            <th>操作</th>
        </tr>

        <c:forEach var="l" items="${list }">
            <tr>
                <td>${l.sno }</td>
                <td>${l.sname }</td>
                <td>${l.age }</td>
                <td>${l.depart }</td>
                <td><a href="javascript:gotod(${l.sno })">删除</a></td>
            </tr>
        </c:forEach>

    </table>

    <center>
        <c:choose>
            <c:when test="${currentpage==1 }">首页&nbsp&nbsp上一页</c:when>
            <c:otherwise>
                <a href="javascript:gotos(1)">首页</a>
                <a href="javascript:gotos(${currentpage-1 })">上一页</a>
            </c:otherwise>
        </c:choose>

        <c:choose>
            <c:when test="${currentpage==totalpage }">下一页&nbsp&nbsp末页</c:when>
            <c:otherwise>
                <a href="javascript:gotos(${currentpage+1 })">下一页</a>
                <a href="javascript:gotos(${totalpage })">末页</a>
            </c:otherwise>
        </c:choose></br>
        共有${totalcount }条记录,每页显示${row }条记录,当前为第${currentpage }页
    </center>
</body>
</html>

图片说明
图片说明
图片说明

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享

3条回答 默认 最新

  • u010159310 冰上蝴蝶梦蝶舞 2016-12-30 02:23

    设置编码方式,请求--request.setCharacterEncoding("utf-8");
    返回--response.setCharacterEncoding("utf-8");

    点赞 评论 复制链接分享
  • u010159310 冰上蝴蝶梦蝶舞 2016-12-30 02:24

    还有要保证jsp的编码与设置的是一样的

    点赞 评论 复制链接分享
  • zjn640322 阿理斯特 2016-12-30 09:04

    1.先检查自己
    --request.setCharacterEncoding("utf-8");
    --response.setCharacterEncoding("utf-8");
    设置的编码是否和jsp页面中的编码统一
    2.在检查您当前项目的编码是否统一,我就遇到过有些项目从别的地方导入的,原项目编码是一种,可以自己当前开发软件默认另一种,虽然显示都没错,但是在传送数据时就乱码了,如何设置呢,右键文件 > preference > 看看当前文件的编码,同样在项目上右键,查看当前项目的编码,修改一致就ok

    点赞 评论 复制链接分享

相关推荐