(1) 数据列表 all.jsp <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <title>My JSP 'index.jsp' starting page</title> <meta http-equiv="pragma" content="no-cache"> <meta http-equiv="cache-control" content="no-cache"> <meta http-equiv="expires" content="0"> <meta http-equiv="keywords" content="keyword1,keyword2,keyword3"> <meta http-equiv="description" content="This is my page"> <script type="text/javascript" src="${pageContext.request.contextPath }/js/jquery-1.7.1.min.js"></script> </head> <body> 数据列表<br/> <a href="${pageContext.request.contextPath }/test/add.jsp">添加</a> </body> </html>
(2) 点击“添加”----> 添加数据页面 add.jsp
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <title>My JSP 'index.jsp' starting page</title> <meta http-equiv="pragma" content="no-cache"> <meta http-equiv="cache-control" content="no-cache"> <meta http-equiv="expires" content="0"> <meta http-equiv="keywords" content="keyword1,keyword2,keyword3"> <meta http-equiv="description" content="This is my page"> </head> <body> <a href="${pageContext.request.contextPath }/MyServlet?method=add">确定添加</a> </body> </html>
(3) 确定添加----->Servlet处理添加,并保存提示信息
package com.test.servlet; import java.io.IOException; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; public class MyServlet extends HttpServlet { public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { this.doPost(request, response); } public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { String type = request.getParameter("method"); if ("add".equals(type)) { this.add(request, response); } else if ("all".equals(type)) { this.all(request, response); } } //添加 public void add(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { System.out.println("执行添加"); request.getSession().setAttribute("userMessage", "操作成功!");//用session保存提示信息 response.sendRedirect("./test/tip.jsp");//重定向到全查询 } //全查询 public void all(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { System.out.println("全查询"); request.getRequestDispatcher("./test/all.jsp").forward(request,response); } }
(4)执行添加后,到操作提示页面 tip.jsp
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <title>My JSP 'index.jsp' starting page</title> <meta http-equiv="pragma" content="no-cache"> <meta http-equiv="cache-control" content="no-cache"> <meta http-equiv="expires" content="0"> <meta http-equiv="keywords" content="keyword1,keyword2,keyword3"> <meta http-equiv="description" content="This is my page"> <script type="text/javascript" src="${pageContext.request.contextPath }/js/jquery-1.7.1.min.js"></script> <script type="text/javascript"> var t="${userMessage}"; $(function(){ if(t!=null&&t!=""&&t!='null'&&t.length>1){ $("#mydiv").fadeIn(1000); $("#mydiv").fadeOut(1000); } }); window.setTimeout(back,2000); function back(){ window.location.href="${pageContext.request.contextPath}/MyServlet?method=all"; } </script> </head> <body> <div id="mydiv" style="position:absolute;top:50%;left:45%;margin:-100px 0 0 -100px; border:1 solid black;height:200;width:300;display:none;">${userMessage }</div> <%session.removeAttribute("userMessage"); %> </body> </html>
注:结合jquery淡入淡出效果,先让提示信息淡入,再淡出,总共2000毫秒时间,也就是2秒,设置定时器2秒后跳到全查询。
(5)回到all.jsp,由于到all.jsp是重定向过来的,所以无论怎么刷新,数据是不会重复提交的。当执行完添加后回到全查询,此时不再手动刷新当前页面,点击添加,不做任何操作,再点浏览器的后退按钮,之前的提示信息也不会再显示出来,这样就避免了把提示信息设置在all.jsp上后退重复提示的问题。