首页 > 代码库 > JSP_tomcat_mysql_注册验证用户;
JSP_tomcat_mysql_注册验证用户;
本文出自:http://blog.csdn.net/svitter
资源下载:
github:
git clone https://github.com/Svtter/JSP-tomcat-mysql
使用了servlet:
web.xml:
<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" version="3.1"> <display-name>JSP_servlet_javabean</display-name> <servlet> <description>配置用於用戶登錄的Servlet</description> <display-name>LoginServlet</display-name> <servlet-name>LoginServlet</servlet-name> <servlet-class>LoginServlet</servlet-class> </servlet> <servlet-mapping> <servlet-name>LoginServlet</servlet-name> <url-pattern>/LoginServlet</url-pattern> </servlet-mapping> <welcome-file-list> <welcome-file>login.html</welcome-file> </welcome-file-list> </web-app>
四个java文件:
LoginServlet.java://用于验证:
import java.io.*; import javax.servlet.*; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import bean.ConnectionBean; import bean.userBean; /** * @author Svitter * */ @SuppressWarnings("serial") public class LoginServlet extends HttpServlet { userBean user = new userBean(); ConnectionBean connBean = new ConnectionBean(); public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { String checkLogin; String username = request.getParameter("login"); String password = request.getParameter("password"); checkLogin = connBean.checkUser(username, password); if(checkLogin.equals("Success")) { user.setUserName(username); request.setAttribute("user", user); getServletConfig().getServletContext().getRequestDispatcher("/loginsuccess.jsp").forward(request, response); } else { request.setAttribute("checkLogin", checkLogin); getServletConfig().getServletContext().getRequestDispatcher("/loginCheck.jsp").forward(request, response); } } public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException{ String username = request.getParameter("login"); String password = request.getParameter("password"); connBean.addUser(username, password); getServletConfig().getServletContext().getRequestDispatcher("/login.html").forward(request, response); } }
bean.GetConnection.java://用于连接数据库,数据库配置文件property.conf
package bean; import java.io.*; import java.util.Properties; import java.sql.*; public class GetConnection { private static Properties p; static{ try{ p = new Properties(); InputStream is = GetConnection.class.getResourceAsStream("property.conf"); p.load(is); is.close(); } catch(Exception e) { e.printStackTrace(); } } public static String getProperty(String key){ return p.getProperty(key); } static String driver = GetConnection.getProperty("driver"); static String url = GetConnection.getProperty("url"); static String name = GetConnection.getProperty("user"); static String pass = GetConnection.getProperty("password"); static{ try{ Class.forName(driver); }catch(ClassNotFoundException e){ e.printStackTrace(); } } public static Connection getConnection() throws SQLException { return DriverManager.getConnection(url, name, pass); } }
property.conf:
driver=com.mysql.jdbc.Driver url=jdbc:mysql://localhost:3306/users user=root password=root
bean.ConnectionBean.java://用于数据库操作
package bean; import java.sql.*; public class ConnectionBean { private Connection con; public ConnectionBean() { try{ con = GetConnection.getConnection(); } catch (Exception e){ e.printStackTrace(); } } //向数据库添加信息 public boolean addUser(String name, String password){ try{ PreparedStatement pstmt = con.prepareStatement("insert into ur(name, passwd) values(?,?)"); pstmt.setString(1, name); pstmt.setString(2, password); pstmt.execute(); return true; } catch (Exception e){ e.printStackTrace(); return false; } } public ResultSet getUser(String name){ try { Statement stm = con.createStatement(); ResultSet result = stm.executeQuery("select * from ur where name='"+name+"'"); return result; } catch (Exception e){ e.printStackTrace(); return null; } } public String checkUser(String name, String passwd){ String checkUser=null; try{ Statement stm = con.createStatement(); ResultSet result = stm.executeQuery("select * from ur where name='"+name+"'"); if(result.next()==false){ checkUser = "No user"; } else{ if(result.getString("passwd").equals(passwd)){ checkUser = "Success"; } else{ checkUser = "Wrong passwd"; } } }catch (Exception e){ e.printStackTrace(); } return checkUser; } }
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。