protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // TODO Auto-generated method stub //response.getWriter().append("Served at: ").append(request.getContextPath()); 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("<h1>问卷</h1>"); String page = request.getParameter("page");//接收name为page的参数并赋值给字符串page out.println("<form action='index.jsp' method='post'>"); if(page==null) { out.println("问题一: <input name='p1q1' type='text' ><br>"); out.println("问题二: <input name='p1q2' type='text' ><br>"); out.println("<input name='page' type='submit' value='下一页'><br> ");//按钮,把页面切换到下一页 }else if("下一页".equals(page)) { String p1q1 = request.getParameter("p1q1"); String p1q2 = request.getParameter("p1q2"); out.println("<input name='p1q1' type='hidden' value='"+p1q1+"' >");//隐藏域,用来发送参数到完成页 out.println("<input name='p1q2' type='hidden' value='"+p1q2+"' >");//隐藏域,用来发送参数到完成页 out.println("问题三: <input name='p2q1' type='text' ><br>"); out.println("<input name='page' type='submit' value='完成' ><br>");//按钮,把页面切换到完成页 }else if("完成".equals(page)) {//完成页,显示所有页面输入的参数 out.println(request.getParameter("p1q1")+"<br>"); out.println(request.getParameter("p1q2")+"<br>"); out.println(request.getParameter("p2q1")+"<br>"); } out.println("</form>"); out.println("</body>"); out.println("</html>"); }