日期:2014-05-20  浏览次数:20758 次

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的代码如下,如何在程序中使用这个过滤器呢?请高手们帮助,谢谢大家了。