MyException - 我的异常网
当前位置:我的异常网» J2SE » web编程的乱码有关问题

web编程的乱码有关问题

www.MyException.Cn  网友分享于:2013-11-21  浏览:102次
web编程的乱码问题
[code=HTML][/code]我重新编辑了一个东西,是页面转移的,由AddServlet.java,BookWriter.java,Book.java,hello.jsp源代码按顺序如下

Java code
package jp.bcat;

import java.io.*;

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

import java.text.SimpleDateFormat;
import java.util.Date;

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

    /**
     * @see HttpServlet#HttpServlet()
     */

    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // TODO Auto-generated method stub
        String step = request.getParameter("step");
        if ("prepare".equals(step))
            prepare(request, response);

    }
    void prepare(HttpServletRequest request, HttpServletResponse response) throws IOException {
        String errorMessage = "";
        Book book = new Book();
        String title = request.getParameter("title");
        book.setTitle(title);
        if (title == null || title.length() == 0)
            errorMessage += "<P>タイトルを入力してください。</P>";


        response.setContentType("text/html; charset=utf-8");
        PrintWriter out = response.getWriter();
        out.println("<HTML><HEAD><TITLE>図書登録確認</TITLE></HEAD>");
        if (errorMessage.length() == 0) {
            out.println("<BODY>");

            out.println("<PRE>");
            new BookWriter(out, "+ ").write(book);
            out.println("</PRE>");



            out.println("</BODY>");
        }
        out.println("</HTML>");
    }
}

Java code

package jp.bcat;

import java.io.PrintWriter;

public class BookWriter {
    protected PrintWriter out;
    protected String indent;

    public BookWriter(PrintWriter out, String indent) {
        this.out = out;
        this.indent = indent;
    }

    public void write(Book book) {
        out.println(indent + "タイトル: " + book.getTitle());

    }
}


Java code
package jp.bcat;

import java.io.Serializable;

public class Book implements Serializable {

    protected String title;

    public String getTitle() {
        return title;
    }

    public void setTitle(String value) {
        title = value;
    }
}


HTML code
<%@ page language="java" contentType="text/html; charset=utf-8"
    pageEncoding="windows-31j"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Insert title here</title>
</head>
<body>
  <FORM action="AddServlet" method="POST">
    <TABLE>
      <TR><TD>タイトル: </TD><TD><INPUT name="title"></TD></TR>

      <TR><TD><INPUT type="submit" value="登録"></TD></TR>
      <INPUT type="hidden" name="step" value="prepare">
    </TABLE>
  </FORM>
</body>
</html>


如果输入汉字,显示的是????的乱码。
我估计我好像是Filter的设置好像不对,我的Filter的代码如下,如何在程序中使用这个过滤器呢?请高手们帮助,谢谢大家了。

文章评论

软件开发程序错误异常ExceptionCopyright © 2009-2015 MyException 版权所有