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(令牌),同时 ...
随机推荐
- 转 Oracle 11g Rman – 08317错误
在一次帮助客户解决归档满的过程中遭遇了此错误. 客户是新上线系统,11g版本.设置了归档清除脚本(脚本参考:http://www.ludatou.com/?p=766),结果发现以往没问题的脚本在此刻 ...
- 记录一个修改application.properties时遇到的坑
有一个需求是会频繁修改配置文件中的常量,为了方便就会用unzip解压war包,修改propertites中的值后重新打war 包,部署,但是发现修改的值没有起作用,,一直在纠结...后来发现其实在编译 ...
- Funsioncharts 线图 破解
在线示例:http://jsfiddle.net/henley/xnozyLa8/2/ 下载:http://files.cnblogs.com/files/ycdx2001/chart.zip
- springmvc httprequest 使用@Autowired注解
springmvc httprequest 使用@Autowired注解我一直有个疑问,就是注解后每次的httprequest 是不是都一样的了,然后会不会引发多线程问题? 代码如下: import ...
- oracle v$database 视图
Select db.dbid "数据库标识",--数据库的标识,当数据库在所有文件的头部创建和存储时计算出来的标记编号 db.Name "数据库名称" ...
- java学习笔记(2)——数组
1.创建数组: int[] a = new int[n];//数组长度n不要求为常数,一旦创建了数组,其大小不可改变 int[] a = {0,1,2,3};//也可这样定义 获得数组元素的个数:ar ...
- hystrix 给方法加断路器
添加依赖 <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>s ...
- jquery显示隐藏效果
通过 jQuery,您可以使用 hide() 和 show() 方法来隐藏和显示 HTML 元素toggle() 方法来切换 hide() 和 show() 方法. 1.hide()隐藏元素 $(se ...
- C++ string 类型提取字符串
在某些情况下需要对输入的字符串进行处理,提取其中的需要的信息. 比如在linux中输入"mkdir test",新建test文件夹,就需要提取其中的test字符. 提取的方法需要b ...
- pycharm使用秘籍 和 pip命令
python使用requirements.txt批量安装包 requirements.txt文件格式: requests==1.2.0 Flask==0.10.1 等等一系列包 cd 到requir ...