当前位置: 代码迷 >> JavaScript >> JSP页面刷新重复提交数据解决办法总结
  详细解决方案

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

热度:449   发布时间:2012-10-25 10:58:58.0
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" onclick="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");??

??}?

?}

}

  相关解决方案