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

网页的乱码问题
HTML code
<!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=Shift_JIS">
<title>Insert title here</title>
</head>
<body>
  <FORM action="AddServlet" method="POST">
    <TABLE>
      <TR><TD>タイトル: </TD><TD><INPUT name="title"></TD></TR>
      <TR><TD>著者:    </TD><TD><INPUT name="author"></TD></TR>
      <TR><TD>訳者:    </TD><TD><INPUT name="translator"></TD></TR>
      <TR><TD>出版社:  </TD><TD><INPUT name="publisher"></TD></TR>
      <TR><TD>出版日:  </TD><TD><INPUT name="publicationDate"></TD></TR>
      <TR><TD>ISBN:   </TD><TD><INPUT name="code"></TD></TR>
      <TR><TD>メモ:    </TD><TD><INPUT name="memo"></TD></TR>
      <TR><TD>キーワード: </TD><TD><INPUT name="keyword"></TD></TR>
      <TR><TD>登録者:  </TD><TD><INPUT name="dataCreator"></TD></TR>
      <TR><TD><INPUT type="submit" value="登録"></TD></TR>
      <INPUT type="hidden" name="step" value="prepare">
    </TABLE>
  </FORM>
</body>
</html>


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()
     */
    public AddServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

    /**
     * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
     */
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // TODO Auto-generated method stub
    }

    /**
     * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
     */
    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);
        else
            confirm(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>";
        String author = request.getParameter("author");
        book.setAuthor(author);
        if (author == null || author.length() == 0)
            errorMessage += "<P>著者を入力してください。</P>";
        String translator = request.getParameter("translator");
        book.setTranslator(translator);
        String publisher = request.getParameter("publisher");
        book.setPublisher(publisher);
        if (publisher == null || publisher.length() == 0)
            errorMessage += "<P>出版社を入力してください。</P>";
        String publicationDate = request.getParameter("publicationDate");
        book.setPublicationDate(publicationDate);
        if (publicationDate == null || publicationDate.length() == 0)
            errorMessage += "<P>出版年月日を入力してください。</P>";
        String code = request.getParameter("code");
        book.setCode(code);
        String memo = request.getParameter("memo");
        book.setMemo(memo);
        String keyword = request.getParameter("keyword");
        book.setKeyword(keyword);
        String dataCreator = request.getParameter("dataCreator");
        book.setDataCreator(dataCreator);
        if (dataCreator == null || dataCreator.length() == 0)
            errorMessage += "<P>登録者名を入力してください。</P>";
        String now = new SimpleDateFormat("yyyy-MM-dd")
            .format(new Date());
        book.setDataCreatedDate(now);

        response.setContentType("text/html; charset=Shift_JIS");
        PrintWriter out = response.getWriter();
        out.println("<HTML><HEAD><TITLE>図書登録確認</TITLE></HEAD>");
        if (errorMessage.length() == 0) {
            out.println("<BODY>");
            out.println("この内容で登録しますか?");
            out.println("<PRE>");
            new BookWriter(out, "+ ").write(book);
            out.println("</PRE>");
            out.println("<FORM method='POST'>");
            out.println("<INPUT type='hidden' name='step' value='confirm'>");
            out.println("<INPUT type='submit' value='登録'>");
            out.println("</FORM>");
            out.println("<A href='./'>メニューに戻る</A>");
            out.println("</BODY>");
            request.getSession().setAttribute("book", book);
        } else {
            out.println("<BODY>");
            out.println(errorMessage);
            out.println("<A href='./'>メニューに戻る</A>");
            out.println("</BODY>");
        }
        out.println("</HTML>");
    }

    void confirm(HttpServletRequest request, HttpServletResponse response) throws IOException {
        Book book = (Book)request.getSession().getAttribute("book");
        BookCatalog catalog = BookCatalog.getInstance();
        catalog.addBook(book);

        response.setContentType("text/html; charset=Shift_JIS");
        PrintWriter out = response.getWriter();
        out.println("<HTML><HEAD><TITLE>図書登録結果</TITLE></HEAD>");
        out.println("<BODY>");
        out.println("<P>登録しました。</P>");
        out.println("<A href='./'>メニューに戻る</A>");
        out.println("</BODY>");
        out.println("</HTML>");
    }
}