JSP页面刷新重复提交数据解决方法总结

jsp页面:

<%@ page contentType="text/html;charset=gbk"%>

<%@ page language="java" %>

<%@ page import="java.util.*"%>

<%

response.setHeader("Cache-Control", "no-cache");

String username="";//session.getAttribute("username")==null?"":(String)session.getAttribute("username");

%>

<%

  Random rand = new Random();  

  Integer   flag =new   Integer(rand.nextInt());

  String str_flag = flag.toString();

  session.setAttribute("flag",str_flag); 

 %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">

<head>

<meta http-equiv="Content-Type" content="text/html; charset=gbk" />

<title>test</title>

</head>

<body>

<table>

 

<form id="form1" action="usercheck.test" name="form1">

<tr>

 <td width="25%" height="25" align="right">帐户:</td>

 <td>

 <input   type=hidden   name="flag"   value="<%=str_flag%>"/>

 <input name="username" type="text" class="text" id="account" value="<%=username %>" maxlength="10"/>

 &nbsp;&nbsp;<span id="account_span"></span>

 </td>

 <td height="25" colspan="2">

 <input id="jccf" style="margin-left:300px" type="button" name="Submit" οnclick="checkusername();" value="提交"/>

 </td>

 </tr>

 <tr>

    <td>第四行赋的值是:</td>

    <td><script>document.write(document.form1.flag.value)</script></td>

  </tr>

</table>

</form>

<script script="javascript">

function checkusername()

{

  form1.submit();

}

document.getElementById('username').focus();

</script>

</body>

</head>

Java:

package cn;

import java.io.*;

import java.io.IOException;

import javax.servlet.ServletConfig;

import javax.servlet.ServletException;

import javax.servlet.http.HttpServlet;

import javax.servlet.http.HttpServletRequest;

import javax.servlet.http.HttpServletResponse;

import javax.servlet.http.HttpSession;

import java.util.*;

import cn.hello;

public class completeservlet extends HttpServlet{

 hello h = new hello();

 String session = "";

 public void service(HttpServletRequest request, HttpServletResponse response)

 throws ServletException, IOException {

 

  HttpSession session = request.getSession();

 

 

  PrintWriter out= response.getWriter();

  String returninfo ="";

  String returnselectinfo = "";

  String username = "";

  username = request.getParameter("username").toString().trim();

  

  Integer flag1 =  new Integer(Integer.parseInt(request.getParameter("flag")));

  String l_strflag1 = flag1.toString();

  if(l_strflag1.equals(session.getAttribute("flag")))

  {

   returninfo =h.test(username);

   returnselectinfo = h.SelectTest(username);

   out.println("test is starting.....");

   out.println("<P>");

   out.println(returninfo);

   out.println("<P>");

   out.println(returnselectinfo);  

   out.println("<P>");

   out.println("username:" + username);

   session.removeAttribute("flag");

  }

  else

  {

   out.println("Don't submit repeatly.");

   out.println(session.getAttribute("flag"));

   out.println(flag1.toString());

   session.removeAttribute("flag");  

  } 

 }

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值