/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的更多相关文章

  1. JAVA–利用Filter和session防止页面重复提交

    JAVA–利用Filter和session防止页面重复提交解决思路:1 用户访问表单页面,先经过过滤器,过滤器设置一个随机id作为token令牌, 并将该token放入表单隐藏域中.2 表单响应到浏览 ...

  2. 一脸懵逼学习Struts数据校验以及数据回显,模型驱动,防止表单重复提交的应用。

    1:Struts2表单数据校验: (1)前台校验,也称之为客户端校验,主要是通过Javascript编程的方式进行数据的验证. (2)后台校验,也称之为服务器校验,这里指的是使用Struts2通过xm ...

  3. Token注解防止表单的重复提交

    注解的一些基础: 参见http://blog.csdn.net/duo2005duo/article/details/50505884和 http://blog.csdn.net/duo2005duo ...

  4. Struts2第十三篇【防止表单重复提交】

    回顾防止表单重复提交 当我们学习Session的时候已经通过Session来编写了一个防止表单重复提交的小程序了,我们来回顾一下我们当时是怎么做的: 在Servlet上生成独一无二的token,保存在 ...

  5. Struts2 06--系统拦截器防止数据重复提交

    一.拦截器简要概述 拦截器,在AOP(Aspect-Oriented Programming)中用于在某个方法或字段被访问之前,进行拦截然后在之前或之后加入某些操作.拦截是AOP的一种实现策略. 在W ...

  6. MVC_防止HttpPost重复提交

    重复提交的场景很常见,可能是当时服务器延迟的原因,如购物车物品叠加,重复提交多个订单.常见的解决方法是提交后把Button在客户端Js禁用,或是用Js禁止后退键等.在ASP.NET MVC 3 Web ...

  7. 利用session防止表单重复提交

    转自:http://www.cnblogs.com/xdp-gacl/p/3859416.html 利用Session防止表单重复提交 对于[场景二]和[场景三]导致表单重复提交的问题,既然客户端无法 ...

  8. Restful api 防止重复提交

    当前很多网站是前后分离的,前端(android,iso,h5)通过restful API 调用 后端服务器,这就存在一个问题,对于创建操作,比如购买某个商品,如果由于某种原因,手抖,控件bug,网络错 ...

  9. API接口重复提交

    重复提交的几种情况1.利用JavaScript防止表单重复提交 按钮禁用2.利用Session令牌防止表单重复提交 具体的做法:在服务器端生成一个唯一的随机标识号,专业术语称为Token(令牌),同时 ...

随机推荐

  1. Net Core2-JWT

    NET Core2 http://www.cnblogs.com/wyt007/category/1130278.html JWT 设计解析及定制 前言 上一节我们讲述的书如何使用jwt token, ...

  2. CellSet 遍历

    CellSet 结构: 查询MDX: SELECT NON EMPTY {{ {{ {{ {{ {{ AddCalculatedMembers([店铺.店铺ID].[店铺ID].Members)}} ...

  3. Hadoop数据管理

    本节主要从三方面介绍Hadoop数据管理:分布式文件系统HDFS.分部式数据库HBase和数据仓库工具Hive. 1. HDFS的数据管理 HDFS是分布式计算的存储基石,Hadoop分布式文件系统和 ...

  4. 介绍我最近做的网站 Asp.Net MVC4 + BootStrap

    一.前言 最近一直在做一个多站SEO数据分析的站点(www.easyyh.com),用了一些新技术,如Asp.Net MVC4,BootStrap,EasyUI,这些都是以前没有搞过的,最近搞得差不多 ...

  5. 【转】pom.xml讲解

    <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/20 ...

  6. linux touch和vi建立的文件是什么文件类型的

    都是acsii类型的文本文档,但是也可以建立其他格式的,比如vi newFile.c(c是c语言动态链接库格式)

  7. Servlet之sendRedirect和getRequestDispatch

    Servlet的请求重定向和请求转发方法的比较分析: 1.getRequestDispatch是属于httpServletRequest对象的方法,请求转发是在同一个请求中完成的,因此整个过程只包含一 ...

  8. @vue/cli 3.x项目脚手架 webpack 配置

    @vue/cli  是一个基于 Vue.js 进行快速开发的完整系统. @vue/cli   基于node服务  需要8.9以上版本 可以使用 nvm等工具来控制node版本  构建于 webpack ...

  9. 洛谷P2062 分队问题(dp)

    题意 题目链接 给定n个选手,将他们分成若干只队伍.其中第i个选手要求自己所属的队伍的人数大等于a[i]人. 在满足所有选手的要求的前提下,最大化队伍的总数. 注:每个选手属于且仅属于一支队伍. So ...

  10. Author: Jan Odvarko, www.janodvarko.cz

    /*  * Author: Jan Odvarko, www.janodvarko.cz */ FBL.ns(function() { with (FBL) { function HelloWorld ...