A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© 张翼 黑马帝   /  2011-11-12 11:55  /  1449 人查看  /  3 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

当用Servlet来处理Http请求,产生返回的HTML页面时,如何能使HTML中的中文字符能够正常显示

评分

参与人数 1技术分 +1 收起 理由
宁超 + 1

查看全部评分

3 个回复

正序浏览
呼呼,只看doGet方法里面的内容就行了!
回复 使用道具 举报
fso918 黑马帝 2011-11-13 12:05:32
藤椅
解决中文乱码问题分2步:
1 将返回消息(即中文字符)以支持中文的码表发送:
   response.setCharacterEncoding("UTF-8");
   一般就用UTF-8码。
2 通知浏览器以你发送的消息的编码方式读取消息内容即中文字符。
  response.setContentType("text/html;charset=UTF-8");
  值一步设置的码表必须和上一步一样。
  然后,如果还有乱码,就是你本地浏览器设置了以某一固定码表显示文档内容了,即在浏览器的设置里把显示页面的码表设置为 UTF-8,

评分

参与人数 1技术分 +1 收起 理由
宁超 + 1 赞一个!

查看全部评分

回复 使用道具 举报
因为你用的是网页显示的,所以可能会出现中文乱码!我下面写了一个网页访问人数统计的,可以供你参考一下!
package servletBao;

import java.io.*;
import java.util.*;
import java.text.*;


import javax.servlet.ServletException;
import javax.servlet.http.*;


public class HelloServlet extends HttpServlet {

        /**
         * The doGet method of the servlet. <br>
         *
         * This method is called when a form has its tag value method equals to get.
         *
         * @param request the request send by the client to the server
         * @param response the response send by the server to the client
         * @throws ServletException if an error occurred
         * @throws IOException if an error occurred
         */
        public void doGet(HttpServletRequest request, HttpServletResponse response)
                        throws ServletException, IOException {
                System.out.println("调用doGet");
                Date now=new Date();
                SimpleDateFormat format=new SimpleDateFormat("yyyy年MM月dd日");
                //解决中文乱码
                response.setContentType("text/html;charset=GBK");
                //会话跟踪     创建Session
                HttpSession session=request.getSession(true);
               
//                session.setAttribute("counter",new Integer(counter));
                Object count=session.getAttribute("counter");
                int counter=0;
                if(count==null){
                        System.out.println("这也运行了");
                        counter=1;
                        session.setAttribute("counter", new Integer(1));
                }else{
                        System.out.println((Integer)count);
                        System.out.println(counter);
                        counter=((Integer)count).intValue();

                        counter++;
                        session.setAttribute("counter", new Integer(counter));
                        System.out.println(counter);
                       
                }
                System.out.println(counter);
                PrintWriter out=response.getWriter();
                out.println("欢迎你第"+counter+"次访问Accp网页");
                out.flush();
                out.close();
               
               
               
               
//                String username=request.getParameter("username");
//                String password=request.getParameter("password");
//                if(username.equals("accp") && password.equals("123")){
//                        response.sendRedirect("success.jsp");
//                }else{
//                        response.sendRedirect("error.jsp");
//                }
        }

        /**
         * The doPost method of the servlet. <br>
         *
         * This method is called when a form has its tag value method equals to post.
         *
         * @param request the request send by the client to the server
         * @param response the response send by the server to the client
         * @throws ServletException if an error occurred
         * @throws IOException if an error occurred
         */
        public void doPost(HttpServletRequest request, HttpServletResponse response)
                        throws ServletException, IOException {
                System.out.println("调用doPost()");
                doGet(request, response);
        }
        /**
         * 调用父类的构造方法
         */
       
        public HelloServlet(){
                super();
        }
        /**
         * servlet初始化
         */
//        public void inint() throws ServletException(){
//                System.out.println("初始化时,init()方法被调用");
//        }
        public void init() throws ServletException{
               
                        System.out.println("init()方法被调用");
               
        }
        /**
         * 释放资源
         */
        public void destroy(){
                super.destroy();
                System.out.println("释放系统资源时destroy()方法被调用");
        }
       

}
这是我以前写的一个!也许会对你有帮助!

评分

参与人数 1技术分 +1 收起 理由
宁超 + 1

查看全部评分

回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马