隐藏域
作用:隐藏域是用来收集或发送信息的不可见元素,对于网页的访问者来说,隐藏域是看不见的。当表单被提交时,隐藏域就会将信息用你设置时定义的名称和值发送到服务器上
关键字:hidden(隐藏的; 神秘的; 秘密的)
package demo;
import java.io.IOException;
import java.io.PrintWriter;
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 Questionnaire
*/
@WebServlet("/index.jsp")
public class Questionnaire extends HttpServlet {
private static final long serialVersionUID = 1L;
/**
* @see HttpServlet#HttpServlet()
*/
public Questionnaire() {
super();
// TODO Auto-generated constructor stub
}
/**
* @see HttpServlet#service(HttpServletRequest request, HttpServletResponse response)
*/
protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
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");//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>");
out.close();
}