/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. Win10专业版系统下添加其他国家语言

    Win10专业版系统下如何添加其他国家语言?国内的win10专业版系统默认情况下是安装简体中文,但是有的用户出于工作原因需要使用其它字体.比如外国友人就需要使用英语,西班牙等.其实win10专业版是支 ...

  2. selenum autoit上传图片

    目前,一般实现文件图片上传的方式都是有一个按钮,点击之后直接调用操作系统自身的弹框,选择文件后,实现上传.因为Selenium不支持调用操作系统的操作,所以这种情况下,利用Selenium无法完成图片 ...

  3. 策略模式和php实现

    策略模式: 策略模式(Strategy Pattern):定义一系列算法,将每一个算法封装起来,并让它们可以相互替换.策略模式让算法独立于使用它的客户而变化,也称为政策模式(Policy). 策略模式 ...

  4. swift基础-2

    一.基本运算符 let a = 5 var b = 10 b = a if a = b{ swift 中赋值运算符,并不将自身作为一个值进行返回,所以编译不合法,帮开发者避免错误,很人性化的语言 } ...

  5. Appium基础三:Appium实现原理

    1.web自动化测试用的selenium webdriver 是c/s模式,server端和client端是通过webdriver protocol实现的,而Appium是参考selenium开发的, ...

  6. php分页代码及总结

    代码部分: <?PHPheader("Content-type:text/html;charset=utf-8");$pageSize = 10;//接收传入的分页码$pag ...

  7. 织梦channel标签内调用子栏目内容

    文件:include\taglib\channel.lib.php 把代码 SELECT id,typename,typedir,isdefault,ispart,defaultname,nameru ...

  8. python爬虫之路——无头浏览器初识及简单例子

    from selenium import webdriver url='https://www.jianshu.com/p/a64529b4ccf3' def get_info(url): inclu ...

  9. SAP Cloud for Customer Price-计价简介

    SAP Cloud for Customer(本文以下简称C4C)作为SAP新一代的CRM云产品,其Price功能实现虽不如以前的SAP ERP那么复杂,但是也能满足企业运作中各种Price需求. C ...

  10. HTML之元素分类

    一.元素展示类型 在HTML本身定义了很多元素,这些元素在网页上展示的时候都会有自己的默认状态,例如有些元素在默认状态下对高宽的属性设置不起作用,有些元素都默认情况下都独立一行显示,这种现象我们称之为 ...