Servlet+JSP下重复提交问题的解决

一、编写表单提交的JSP页面login.jsp

<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
  
    <base href="<%=basePath%>">
        <!-- 设置网页不缓存 -->
	<META HTTP-EQUIV="pragma" CONTENT="no-cache"> 
	<META HTTP-EQUIV="Cache-Control" CONTENT="no-cache, must-revalidate"> 
	<META HTTP-EQUIV="expires" CONTENT="Wed, 26 Feb 1997 08:21:57 GMT">
    <title>filterTest</title>
<script type="text/javascript">
var checkSubmitFlg = false;
function submitValidate(){
	
	if(checkSubmitFlg == true){
		return false;
	}
	checkSubmitFlg = true;
	return true;
}
//设置点击之后,浏览器不能再对点击做出事件反应
//document.onclick = function doconclick(){
//	window.event.returnValue=false;
//} 
</script>
  </head>
  <body>
    <form action="servlet/LoginServlet" method="post" >
    username:<input type="text" id="username" name="username"/><br>
    password:<input type="password" id="password" name="password"/><br>
 <!--    请输入验证码:<input type="text" name="code" id="code" /><img src=""/> -->
    <input type="hidden" name="key" value="key"/>
      <!-- 设置一个主题topic,用于记录session中是否还有该值 -->
    <input type="button" value="提交" οnclick="<%session.setAttribute("topic_add","topic_add"); %>;this.form.submit()" />
    </form>
 <!--    <h1>欢迎:<span style="color=red"><%=session.getAttribute("username")%></span></h1> -->
  </body>
</html>

二、编写Servlet,LoginServlet.java

package com.test.servlet;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;


public class LoginServlet extends HttpServlet {

	public LoginServlet() {
		super();
	}

	public void destroy() {
		super.destroy(); // Just puts "destroy" string in log
		// Put your code here
	}
	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 key = request.getParameter("key");

		if(isRedo(request,key)){
			request.getSession().setAttribute("key", key);
			this.getServletContext().getRequestDispatcher("/resultLogin.jsp").forward(request, response);
		}else{
//			System.out.println("resubmit..");
			response.sendRedirect("../resubmit.jsp");
		}
		
	}

	public void init() throws ServletException {
		// Put your code here
	}
	public boolean isRedo(HttpServletRequest request,String key){
		String value = (String)request.getSession().getAttribute(key);
		if(value == null){
			return true;
		}else{
			request.getSession().removeAttribute(key);
			return false;
		}
		
	}

}

三、登录成功页面resultLogin.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 'resultLogin.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">
	<!--
	<link rel="stylesheet" type="text/css" href="styles.css">
	-->

  </head>
    <body>
    <h1>登录结果页面</h1>
 	username::<%=request.getParameter("username") %><br>
 	password::<%=request.getParameter("password") %>
  </body>
</html>

四、重复提交提示页面

<%@ page language="java" import="java.util.*" pageEncoding="ISO-8859-1"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href="<%=basePath%>">
    
    <title>My JSP 'resubmit.jsp' starting page</title>

  </head>
  
  <body>
  <h1>Re Submt!!!!!!!!!</h1>
  </body>
</html>

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值