SQL防止重复提交和Filter
/class User
package com.neuedu.bean;
import java.io.Serializable;
public class User implements Serializable{
private static final long serialVersionUID = 1L;
private int password;
private String name;
public User() {
super();
}
public User(int password, String name) {
super();
this.password = password;
this.name = name;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getPassword() {
return password;
}
public void setPassword(int password) {
this.password = password;
}
@Override
public String toString() {
return "password=" + password + ", name=" + name;
}
}
/class LoginDao
package com.neusoft.dao; import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException; import com.neuedu.bean.User;
import com.neusoft.utils.JDBCUtil; public class LoginDao {
public User getUser(String name,String password){
User user=null;
PreparedStatement ps=null;
ResultSet rs =null;
Connection conn=JDBCUtil.getConnection();
String sql="select * from t_user where password= ? and name =?";
try {
ps = conn.prepareStatement(sql);
ps.setString(, password);
ps.setString(, name);
rs = ps.executeQuery();
while (rs.next()) {
String username = rs.getString("name");
int password2 = rs.getInt("password");
user=new User(password2,username);
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally {
JDBCUtil.close(ps, rs, conn);
}
return user; }
public void Add(String name,String password,String email){ PreparedStatement ps=null;
Connection conn=JDBCUtil.getConnection();
String sql="insert into t_user values(?,?,?,?)";
try {
ps = conn.prepareStatement(sql);
ps.setString(, null);
ps.setString(, name);
ps.setString(, password);
ps.setString(, email);
ps.executeUpdate();
System.out.println(ps.toString());
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally { if (ps !=null) {
try {
ps.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
if (conn !=null) {
try {
conn.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
public void Update(String name,String password,String email){ PreparedStatement ps=null;
Connection conn=JDBCUtil.getConnection();
String sql="update t_user set name=?,pasword=?,mail=? where id=?";
try {
ps = conn.prepareStatement(sql);
ps.setString(, null);
ps.setString(, name);
ps.setString(, password);
ps.setString(, email);
ps.executeUpdate();
System.out.println(ps.toString());
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally { if (ps !=null) {
try {
ps.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
if (conn !=null) {
try {
conn.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
}
/class AFilter
package com.neusoft.servlet; import java.io.IOException;
import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.annotation.WebFilter; @WebFilter( "/LoginServlet" )
public class AFilter implements Filter { public void destroy() {
// TODO Auto-generated method stub
} public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
System.out.println("AFilter此路是我开,此树是我栽!");
String name = request.getParameter("username");
if (name.equals("qwe")) {
chain.doFilter(request, response);
System.out.println("AFilter要想从此过,留下买路财!");
}else {
request.getRequestDispatcher("/Login.jsp").forward(request, response);//转发
} } public void init(FilterConfig fConfig) throws ServletException {
// TODO Auto-generated method stub
} }
/class BFilter
package com.neusoft.servlet; import java.io.IOException;
import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.annotation.WebFilter; @WebFilter("/LoginServlet")
public class BFilter implements Filter { public void destroy() {
// TODO Auto-generated method stub
} public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
System.out.println("BFilter此路是我开,此树是我栽!");
String psd = request.getParameter("pwd");
if (psd.equals("")) {
chain.doFilter(request, response);
System.out.println("BFilter要想从此过,留下买路财!");
}else {
request.getRequestDispatcher("/Login.jsp").forward(request, response);//转发
} } public void init(FilterConfig fConfig) throws ServletException {
// TODO Auto-generated method stub
} }
/class LoginServlet
package com.neusoft.servlet; import java.io.IOException;
import java.util.ArrayList;
import java.util.List; import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession; import com.neuedu.bean.User;
import com.neusoft.dao.LoginDao; @WebServlet("/LoginServlet")
public class LoginServlet extends HttpServlet {
private static final long serialVersionUID = 1L; protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String reqUUID = request.getParameter("uuid");
HttpSession session = request.getSession();
String sessUUID =(String)session.getAttribute("uuid");
session.removeAttribute("uuid");//防止重复提交
try {
Thread.sleep(*);//休眠3秒 String name = request.getParameter("username");
String psd = request.getParameter("pwd");
System.out.println(name);
User user=new LoginDao().getUser(name, psd);
if (user !=null&&reqUUID.equals(sessUUID)) {
// List<User>List=new ArrayList<User>();
// List.add(user);
request.setAttribute("user", user);
System.out.println(user);
// response.sendRedirect(request.getContextPath()+"/login-success.jsp");//重定向
request.getRequestDispatcher("/login-success.jsp").forward(request, response);//转发
}else {
request.setAttribute("errorMsg", "不要重复提交!");
request.getRequestDispatcher("/Login.jsp").forward(request, response);//转发
}
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
request.setCharacterEncoding("utf-8");
doGet(request, response);
} }
/class OUTServlet
package com.neusoft.servlet; import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import javax.swing.JOptionPane; @WebServlet("/OUTServlet")
public class OUTServlet extends HttpServlet {
private static final long serialVersionUID = 1L; protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
HttpSession session = request.getSession();
session.invalidate();//清除会话,也就是清除参数
JOptionPane.showMessageDialog(null,"您已退出,请重新登录");
response.sendRedirect(request.getContextPath()+"/Login.jsp");//重定向
} protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doGet(request, response);
} }
/class JDBCUtil
package com.neusoft.utils; import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement; public class JDBCUtil {
private static String driver="com.mysql.jdbc.Driver";
private static String url="jdbc:mysql://localhost:3306/demo";
private static String username="root";
private static String password="";
static{ try {
Class.forName(driver);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
} }
public static Connection getConnection(){
try {
return DriverManager.getConnection(url, username, password);
} catch (SQLException e) {
// TODO Auto-generated catch block
return null;
}
} public static void close(Statement st,ResultSet rs,Connection conn){
if (conn !=null) {
try {
conn.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
if (rs !=null) {
try {
rs.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
if (st !=null) {
try {
st.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
/Login.jsp
<%@page import="java.util.UUID"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<%
String uuid=UUID.randomUUID().toString();
session.setAttribute("uuid", uuid);
%>
${errorMsg}
<form action="${pageContext.request.contextPath}/LoginServlet" method="post">
<input type="hidden" name="uuid" value="<%=uuid%>"/>
用户名:<input type="text" name="username"/>
密码:<input type="password" name="pwd"/>
<input type="submit" value="提交"/>
</form>
<a href="Regist.html">去注册</a>
</body>
</html>
<%@page import= "com.neuedu.bean.User"%>
<%@page import="java.util.ArrayList"%>
<%@page import="java.util.List"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<table border="1" align="center" width="50%">
<tr>
<th>姓名 </th>
<th>密码 </th>
<th colspan="2"> </th>
</tr>
<%-- <%
List<User>stuList=new ArrayList<User>();
stuList=(List<User>)request.getAttribute("stuList");
for(int i=0;i<stuList.size();i++){
User user=stuList.get(i);
%> --%>
<tr>
<td><%-- <%=user.getName() %> --%>${user.name}</td>
<td><%-- <%=user.getPassword() %> --%>${user.password}</td>
<td><a href="#">修改</a></td>
<td><a href="#">删除</a></td>
</tr>
<%-- <%
}
%> --%> </table>
<form action="${pageContext.request.contextPath}/OUTServlet" >
<input type="submit"value="退出"/>
</form>
</body>
</html>
写之前导包
SQL防止重复提交和Filter的更多相关文章
- JAVA–利用Filter和session防止页面重复提交
JAVA–利用Filter和session防止页面重复提交解决思路:1 用户访问表单页面,先经过过滤器,过滤器设置一个随机id作为token令牌, 并将该token放入表单隐藏域中.2 表单响应到浏览 ...
- 一脸懵逼学习Struts数据校验以及数据回显,模型驱动,防止表单重复提交的应用。
1:Struts2表单数据校验: (1)前台校验,也称之为客户端校验,主要是通过Javascript编程的方式进行数据的验证. (2)后台校验,也称之为服务器校验,这里指的是使用Struts2通过xm ...
- Token注解防止表单的重复提交
注解的一些基础: 参见http://blog.csdn.net/duo2005duo/article/details/50505884和 http://blog.csdn.net/duo2005duo ...
- Struts2第十三篇【防止表单重复提交】
回顾防止表单重复提交 当我们学习Session的时候已经通过Session来编写了一个防止表单重复提交的小程序了,我们来回顾一下我们当时是怎么做的: 在Servlet上生成独一无二的token,保存在 ...
- Struts2 06--系统拦截器防止数据重复提交
一.拦截器简要概述 拦截器,在AOP(Aspect-Oriented Programming)中用于在某个方法或字段被访问之前,进行拦截然后在之前或之后加入某些操作.拦截是AOP的一种实现策略. 在W ...
- MVC_防止HttpPost重复提交
重复提交的场景很常见,可能是当时服务器延迟的原因,如购物车物品叠加,重复提交多个订单.常见的解决方法是提交后把Button在客户端Js禁用,或是用Js禁止后退键等.在ASP.NET MVC 3 Web ...
- 利用session防止表单重复提交
转自:http://www.cnblogs.com/xdp-gacl/p/3859416.html 利用Session防止表单重复提交 对于[场景二]和[场景三]导致表单重复提交的问题,既然客户端无法 ...
- Restful api 防止重复提交
当前很多网站是前后分离的,前端(android,iso,h5)通过restful API 调用 后端服务器,这就存在一个问题,对于创建操作,比如购买某个商品,如果由于某种原因,手抖,控件bug,网络错 ...
- API接口重复提交
重复提交的几种情况1.利用JavaScript防止表单重复提交 按钮禁用2.利用Session令牌防止表单重复提交 具体的做法:在服务器端生成一个唯一的随机标识号,专业术语称为Token(令牌),同时 ...
随机推荐
- Spring中的注入方式 和使用的注解 详解
注解:http://www.cnblogs.com/liangxiaofeng/p/6390868.html 注入方式:http://www.cnblogs.com/java-class/p/4727 ...
- org.apache.ibatis.binding.BindingException【原因汇总】
这个问题整整纠结了我四个多小时,心好累啊...不废话... 背景:Spring整合Mybatis 报错:org.apache.ibatis.binding.BindingException: Inva ...
- 关于AQS——独占锁的相关方法(一)
一.序言 Lock接口是juc包下一个非常好用的锁,其方便和强大的功能让他成为synchronized的一个很好的替代品. 我们常用的一个Lock的实现类(好像也是唯一一个只实现了Lock接口的类) ...
- Spark Mllib里使用贝氏二元分类时如何将数值特征字段用StandardScaler进行标准化(图文详解)
不多说,直接上干货! NaiveBayes数值特征字段一定要大于0,所以加入下述命令将负数转换为0. 朴素贝叶斯分类算法在进行数据标准化时,参数withMean必须设置为false. 具体,见 Had ...
- Java命令行实用工具jps和jstat 专题
在Linux或其他UNIX和类UNIX环境下,ps命令想必大家都不陌生,我相信也有不少同学写过 ps aux | grep java | grep -v grep | awk '{print $2}' ...
- Linq Enumerable.Distinct方法去重
Enumerable.Distinct 方法 是常用的LINQ扩展方法,属于System.Linq的Enumerable方法,可用于去除数组.集合中的重复元素,还可以自定义去重的规则. 有两个重载方法 ...
- 4.词法结构-JavaScript权威指南笔记
今天是第二章.所谓词法结构(lexical structure),就是写代码中最基本的东西,变量命名,注释,语句分隔等,这是抄书抄的... 1.字符集,必须是Unicode,反正Unicode是ASC ...
- 序列化流与反序列化流,打印流,工具类commons-IO
1序列化流与反序列化流 用于从流中读取对象的操作流 ObjectInputStream 称为 反序列化流 用于向流中写入对象的操作流 ObjectOutputStream 称为 序列化流 特 ...
- HDU 2899Strange fuction(模拟退火)
题意 题目链接 求 $F(x) = 6 * x^7+8*x^6+7*x^3+5*x^2-y*x (0 <= x <=100)$的最小值 Sol 强上模拟退火,注意eps要开大! /* */ ...
- BZOJ4939: [Ynoi2016]掉进兔子洞(莫队 bitset)
题意 题目链接 一个长为 n 的序列 a. 有 m 个询问,每次询问三个区间,把三个区间中同时出现的数一个一个删掉,问最后三个区间剩下的数的个数和,询问独立. 注意这里删掉指的是一个一个删,不是把等于 ...