了解其中的一些字符设置,PrintWriter输出等。。
form.html:
<!DOCTYPE html> <html> <head> <title>宠物类型大调查</title> <meta http-equiv="content-type" content="text/html ;charset=UTF-8"> </head> <body> <form method="post" action="pet" > 姓名:<input type="text" name="user" value=""><br> 邮件:<input type="text" name="email" value=""><br> 你喜爱的宠物代表: <br> <select name="type" size="6" multiple="true"> <option value="猫">猫</option> <option value="狗">狗</option> <option value="鱼">鱼</option> <option value="鸟">鸟</option> </select> <br> <input type="submit" value="send" /> </form> </body> </html>
Pet.java:
package cc.openhome; import java.io.IOException; import java.io.*; import javax.servlet.ServletException; import javax.servlet.annotation.WebServlet; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; /** * Servlet implementation class Pet */ @WebServlet("/pet") public class Pet extends HttpServlet { private static final long serialVersionUID = 1L; /** * @see HttpServlet#HttpServlet() */ public Pet() { super(); // TODO Auto-generated constructor stub } /** * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response) */ @Override protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // TODO Auto-generated method stub request.setCharacterEncoding("UTF-8"); response.setContentType("text/html; charset=UTF-8"); PrintWriter out = response.getWriter(); out.println("<html>"); out.println("<head>"); out.println("<title>感谢填写</title>"); out.println("</head>"); out.println("<body>"); out.println("联系人:<a herf='mailto:" + request.getParameter("email") + "'>" + request.getParameter("user") + "</a>"); out.println("<br>喜爱的宠物类型"); for (String type : request.getParameterValues("type")) { out.println("<li>" + type + "</li>"); } out.println("<ul>"); out.println("</ul>"); out.println("</body>"); out.println("</html>"); out.close(); } }
时间: 2024-11-16 16:30:07