一、编写表单提交的JSP页面login.jsp
二、编写Servlet,LoginServlet.java
三、登录成功页面resultLogin.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>