日期:2014-05-20 浏览次数:20758 次
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>"); } }
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()); } }
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; } }
<%@ 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>