属性
<input type="hidden"name="field_name"value="value">1隐藏域在页面中对于用户是不可见的,在表单中插入隐藏域的目的在于收集或发送信息,以利于被处理表单的程序所使用。浏览者单击发送按钮发送表单的时候,隐藏域的信息也被一起发送到服务器。
2有些时候我们要给用户一信息,让他在提交表单时提交上来以确定用户身份,如sessionkey,等等.当然这些东西也能用cookie实现,但使用隐藏域就简单的多了.而且不会有浏览器不支持,用户禁用cookie的烦恼。
3有些时候一个form里有多个提交按钮,怎样使程序能够分清楚到底用户是按那一个按钮提交上来的呢?我们就可以写一个隐藏域,然后在每一个按钮处加上οnclick="document.form.command.value=“xx”"然后我们接到数据后先检查command的值就会知道用户是按的那个按钮提交上来的。
4有时候一个网页中有多个form,我们知道多个form是不能同时提交的,但有时这些form确实相互作用,我们就可以在form中添加隐藏域来使它们联系起来。
5javascript不支持全局变量,但有时我们必须用全局变量,我们就可以把值先存在隐藏域里,它的值就不会丢失了。
##上代码
package com.zhiliaotang.servlet; import javax.servlet.ServletException; import javax.servlet.annotation.WebServlet; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import java.io.IOException; import java.io.PrintWriter; @WebServlet("/pageOne.do") public class PageServlet extends HttpServlet { @Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { doPost(req,resp); } @Override protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { resp.setContentType("text/html;charset=UTF-8"); PrintWriter out = resp.getWriter(); out.write("<h1>问卷调查--第一页</h1>"); out.write("<form action='pageTwo.do' method='post'>"); out.write("<p>"); out.write("<label for='q1'>问题一:</label>"); out.write("<input type='text'name='q1'id='q1'>"); out.write("</p>"); out.write("<p>"); out.write("<label for='q2'>问题二:</label>"); out.write("<input type='text'name='q2'id='q2'>"); out.write("</p>"); out.write("<p>"); out.write("<label></label>"); out.write("<input type='submit' value='下一页'>"); out.write("</p>"); out.write("</form>"); out.close(); } } package com.zhiliaotang.servlet; import javax.servlet.ServletException; import javax.servlet.annotation.WebServlet; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import java.io.IOException; import java.io.PrintWriter; @WebServlet("/pageTwo.do") public class PageTwoServlet extends HttpServlet { @Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { doPost(req, resp); } @Override protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { //设置请求编码 req.setCharacterEncoding("UTF-8"); //获取第一页的数据 String q1 = req.getParameter("q1"); String q2 = req.getParameter("q2"); resp.setContentType("text/html;charset=UTF-8"); PrintWriter out = resp.getWriter(); out.write("<h1>问卷调查--第二页</h1>"); out.write("<form action='pageResult.do' method='post'>"); //将第一页的数据放到隐藏域中 out.write("<input type='hidden' name='q1' value='"+q1+"'>"); out.write("<input type='hidden' name='q2' value='"+q2+"'>"); out.write("<p>"); out.write("<label for='q3'>问题三:</label>"); out.write("<input type='text' name='q3' id='q3'>"); out.write("</p>"); out.write("<p>"); out.write("<label for='q4'>问题四:</label>"); out.write("<input type='text' name='q4' id='q4'>"); out.write("</p>"); out.write("<p>"); out.write("<label ></label>"); out.write("<input type='submit' value='提交问卷'>"); out.write("</p>"); out.write("</form>"); out.close(); } } package com.zhiliaotang.servlet; import javax.servlet.ServletException; import javax.servlet.annotation.WebServlet; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import java.io.IOException; import java.io.PrintWriter; @WebServlet("/pageResult.do") public class PageResultServlet extends HttpServlet { @Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { doPost(req, resp); } @Override protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { req.setCharacterEncoding("UTF-8"); String q1 = req.getParameter("q1"); String q2 = req.getParameter("q2"); String q3 = req.getParameter("q3"); String q4 = req.getParameter("q4"); resp.setContentType("text/html;charset=UTF-8"); PrintWriter out = resp.getWriter(); out.write("<h1>问卷调查--结果</h1>"); out.write("<ol>"); out.write("<li>问题一:"+q1+"</li>"); out.write("<li>问题二:"+q2+"</li>"); out.write("<li>问题三:"+q3+"</li>"); out.write("<li>问题四:"+q4+"</li>"); out.write("</ol>"); out.close(); } }