l_zqlw 2016-12-30 02:04 采纳率: 0%
浏览 2918

利用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条回答

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

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

    评论

报告相同问题?

悬赏问题

  • ¥15 Python时间序列如何拟合疏系数模型
  • ¥15 求学软件的前人们指明方向🥺
  • ¥50 如何增强飞上天的树莓派的热点信号强度,以使得笔记本可以在地面实现远程桌面连接
  • ¥15 MCNP里如何定义多个源?
  • ¥20 双层网络上信息-疾病传播
  • ¥50 paddlepaddle pinn
  • ¥20 idea运行测试代码报错问题
  • ¥15 网络监控:网络故障告警通知
  • ¥15 django项目运行报编码错误
  • ¥15 STM32驱动继电器