1.首先配置Tomcat服务

2.创建web项目文件

包如下图所示:

com.account.dao.UserDao

package com.account.dao;import com.account.modle.User;public interface UserDao {public void add(User user)throws Exception;public User getByName(String name) throws Exception;
}

com.account.dao.imp.UserDaoImp

package com.account.dao.imp;import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;import com.account.dao.UserDao;
import com.account.modle.Account;
import com.account.modle.User;
import com.account.util.JDBCUtil;public class UserDaoImp implements UserDao{@Overridepublic void add(User user) throws Exception {Connection conn = JDBCUtil.getConn();String sql = "insert into admin values(null,?,?)";PreparedStatement ps = conn.prepareStatement(sql);ps.setString(1, user.getUsername());ps.setString(2, user.getPassword());ps.executeUpdate();JDBCUtil.close(null, ps, conn);}@Overridepublic User getByName(String name) throws Exception {Connection conn = JDBCUtil.getConn();String sql = "select * from admin where user=?";PreparedStatement ps = conn.prepareStatement(sql);ps.setString(1, name);User user = null;ResultSet rs = ps.executeQuery();while(rs.next()) {user = new User();user.setUsername(rs.getString("user"));user.setPassword(rs.getString("password"));}JDBCUtil.close(rs, ps, conn);return user;}}

com.account.modle.User

package com.account.modle;public class User {private int id;private String username;private String password;@Overridepublic String toString() {return "User [username=" + username + ", password=" + password + "]";}public User(String username, String password) {super();this.username = username;this.password = password;}public int getId() {return id;}public void setId(int id) {this.id = id;}public String getUsername() {return username;}public void setUsername(String username) {this.username = username;}public String getPassword() {return password;}public void setPassword(String password) {this.password = password;}public User() {}public User(int id, String username, String password) {this.id = id;this.username = username;this.password = password;}}

com.account.service.UserService

package com.account.service;import com.account.modle.User;public interface UserService {public boolean add(User user)throws Exception;public User getByUserName(String name) throws Exception;
}

com.account.service.imp.UserServiceImp

package com.account.service.imp;import com.account.dao.UserDao;
import com.account.dao.imp.UserDaoImp;
import com.account.modle.User;
import com.account.service.UserService;public class UserServiceImp implements UserService{UserDao  dao = new UserDaoImp();@Overridepublic boolean add(User user) throws Exception {boolean flag = false;UserDao  dao = new UserDaoImp();User user2 = dao.getByName(user.getUsername());if (user2!=null) {return flag; }dao.add(user);flag = true;return flag;}@Overridepublic User getByUserName(String name) throws Exception {// TODO Auto-generated method stubreturn dao.getByName(name);}}

com.account.servlet.LoginServlet

package com.account.servlet;import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;import com.account.modle.User;
import com.account.service.UserService;
import com.account.service.imp.UserServiceImp;/*** Servlet implementation class LoginServlet*/
@WebServlet("/LoginServlet")
public class LoginServlet extends HttpServlet {private static final long serialVersionUID = 1L;/*** @see HttpServlet#HttpServlet()*/public LoginServlet() {super();// TODO Auto-generated constructor stub}/*** @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)*/protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {}/*** @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)*/protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {request.setCharacterEncoding("utf-8");String name = request.getParameter("username");System.out.println(name);String paswd = request.getParameter("paswd");//2.根据用户名查询UserService service = new UserServiceImp();User user = null;try {//根据用户名查询用户对象user = service.getByUserName(name);System.out.println(user);} catch (Exception e) {// TODO Auto-generated catch blocke.printStackTrace();}//返回页面的信息String msgString="";//登录是否成功boolean result = false;System.out.println(user);if (user==null) {msgString = "用户名输入错误";}else {if (paswd.equals(user.getPassword())) {result = true;}else {msgString = "密码输错误";}}//3.1跳转到系统首页面if (result) {
//          Cookie c1 = new Cookie("uname", name);Cookie c2 = new Cookie("paswd", user.getPassword()+"");
//          response.addCookie(c1);response.addCookie(c2);
//          HttpSession session = request.getSession();
//          session.setAttribute("user", user);request.getRequestDispatcher("index.jsp").forward(request, response);}else {request.setAttribute("msg", msgString);request.getRequestDispatcher("login.jsp").forward(request, response);}}}

com.account.servlet.ZhuceServlet

package com.account.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 com.account.modle.User;
import com.account.service.UserService;
import com.account.service.imp.UserServiceImp;/*** Servlet implementation class LoginServlet*/
@WebServlet("/ZhuceServlet")
public class ZhuceServlet extends HttpServlet {private static final long serialVersionUID = 1L;/*** @see HttpServlet#HttpServlet()*/public ZhuceServlet() {super();// TODO Auto-generated constructor stub}/*** @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)*/protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {// TODO Auto-generated method stubresponse.getWriter().append("Served at: ").append(request.getContextPath());}/*** @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)*/protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {request.setCharacterEncoding("utf-8");String username = request.getParameter("username");String paswd = request.getParameter("paswd");User user = new User(username,paswd);System.out.println(user);UserService us = new UserServiceImp();try {boolean flag = us.add(user);if (flag) {request.getRequestDispatcher("login.jsp").forward(request, response);}else {request.getRequestDispatcher("login.jsp").forward(request, response);}} catch (Exception e) {// TODO Auto-generated catch blocke.printStackTrace();}}}

com.account.util.JDBCUtil

package com.account.util;import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.Reader;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Properties;public class JDBCUtil {                  public static String DRIVER;public static String URL;public static String ROOT;public static String PASSWORD;static {Properties props = new Properties();try {InputStream iStream = JDBCUtil.class.getClassLoader().getResourceAsStream("db.properties");
//          Reader is = new FileReader("db.properties");props.load(iStream);DRIVER = props.getProperty("driver");URL = props.getProperty("url");ROOT = props.getProperty("user");PASSWORD = props.getProperty("password");} catch (FileNotFoundException e) {// TODO Auto-generated catch blocke.printStackTrace();} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();}}public static Connection getConn() {Connection  conn = null;try {Class.forName(DRIVER);conn = DriverManager.getConnection(URL,ROOT,PASSWORD);} catch (ClassNotFoundException e) {// TODO Auto-generated catch blocke.printStackTrace();} catch (SQLException e) {// TODO Auto-generated catch blocke.printStackTrace();}return conn;}public static void close(ResultSet rs,Statement st,Connection conn) {try {if (rs!=null) {rs.close();}if (conn!=null) {conn.close(); }if (st!=null) {st.close();}} catch (SQLException e) {// TODO Auto-generated catch blocke.printStackTrace();}}}

db.properties 配置文件

#qudong
driver=com.mysql.jdbc.Driver
#dizhi
url=jdbc:mysql://localhost:3306/book?useUnicode=true&characterEncoding=utf8&useSSL=false
#yonghuming
user=root
#mima
password=tiger

引入jar包

登录界面:

<%@ page language="java" contentType="text/html; charset=UTF-8"pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html><head><meta charset="utf-8" /><title></title><style>*{margin: 0px;padding: 0px;}body{/* height: 100vh; */background: linear-gradient(#141e30, #243b55);background-repeat: no-repeat;background-size: 100% 100%;}#register{display: none;}#Box,#register{position: relative;width: 500px;height: 500px;/* border: 1px solid purple; */left: 50%;margin-left: -250px;top: 190px;font-family:"Microsoft YaHei";font-style: italic;}.title{width: 300px ;height: 50px;background-color:snow;line-height: 50px;border-radius: 20px;text-align: center;position: relative;top: 100px;left: 80px;/* border: 1px solid purple; */}.username{width: 400px;height: 50px;/* border: 1px solid purple; */margin: auto;line-height: 50px;font-size: 28px;margin: 30px auto;position: relative;top: 100px;}input:hover {animation: move .1s linear forwards;}@keyframes move {0% {transform: scale(1);}100% {transform: scale(1.1);}}.username>input{border-radius: 10px;outline: none;border: none;font-size: 20px;background-color: snow;text-indent: 10px;}#u,#user{width: 240px;height: 30px;}#p,#paswd{position: relative;width: 240px;height: 30px;left: 30px;}.login>input{width: 80px;height: 40px;position: relative;left: 230px;top: 110px;border: none;}.register>input{position: relative;width: 80px;height: 40px;left: 330px;top: 70px;/* border-radius: 20px; */border: none;}</style><script src="js/jquery-3.2.0.min.js" type="text/javascript"></script><script>function login(){document.form1.action = "LoginServlet";document.form1.submit();}</script></head><body bgcolor="bisque"><div id="Box"><form action="ZhuceServlet"  name="form1" method="post"><div class="title"><h1>用户管理系统</h1></div><div class="username"><label for="u">用户名:</label><input type="text" name="username" id="u"></div><div class="username"><label for="p">密码:</label><input type="password" name="paswd" id="p""></div><div class="login"><input type="button" value="登录" onclick="login()"></div><div class="register"><input type="submit" id="" name="register" value="注册" /></div></form>${msg}</div></body><script>! function() {function o(w, v, i) {return w.getAttribute(v) || i}function j(i) {return document.getElementsByTagName(i)}function l() {var i = j("script"),w = i.length,v = i[w - 1];return {l: w,z: o(v, "zIndex", -1),o: o(v, "opacity", 0.9),c: o(v, "color", "0, 0, 0"),n: o(v, "count", 199)}}function k() {r = u.width = window.innerWidth || document.documentElement.clientWidth || document.body.clientWidth, n = u.height = window.innerHeight || document.documentElement.clientHeight || document.body.clientHeight}function b() {e.clearRect(0, 0, r, n);var w = [f].concat(t);var x, v, A, B, z, y;t.forEach(function(i) {i.x += i.xa, i.y += i.ya, i.xa *= i.x > r || i.x < 0 ? -1 : 1, i.ya *= i.y > n || i.y < 0 ? -1 : 1,e.fillRect(i.x - 0.5, i.y - 0.5, 1, 1);for (v = 0; v < w.length; v++) {x = w[v];if (i !== x && null !== x.x && null !== x.y) {B = i.x - x.x, z = i.y - x.y, y = B * B + z * z;y < x.max && (x === f && y >= x.max / 2 && (i.x -= 0.03 * B, i.y -= 0.03 * z), A = (x.max -y) / x.max, e.beginPath(), e.lineWidth = A / 2, e.strokeStyle = "rgba(" + s.c +"," + (A + 0.2) + ")", e.moveTo(i.x, i.y), e.lineTo(x.x, x.y), e.stroke());}}w.splice(w.indexOf(i), 1)}), m(b)}var u = document.createElement("canvas"),s = l(),c = "c_n" + s.l,e = u.getContext("2d"),r, n, m = window.requestAnimationFrame || window.webkitRequestAnimationFrame || window.mozRequestAnimationFrame || window.oRequestAnimationFrame || window.msRequestAnimationFrame || function(i) {window.setTimeout(i, 1000 / 45)},a = Math.random,f = {x: null,y: null,max: 20000};u.id = c;u.style.cssText = "position:fixed;top:0;left:0;z-index:" + s.z + ";opacity:" + s.o;j("body")[0].appendChild(u);k(), window.onresize = k;window.onmousemove = function(i) {i = i || window.event, f.x = i.clientX, f.y = i.clientY}, window.onmouseout = function() {f.x = null, f.y = null};for (var t = [], p = 0; s.n > p; p++) {var h = a() * r,g = a() * n,q = 2 * a() - 1,d = 2 * a() - 1;t.push({x: h,y: g,xa: q,ya: d,max: 6000})}setTimeout(function() {b()}, 500)}();</script>
</html>

效果图:

个人登录空间 含登录注册功能相关推荐

  1. Javaweb 实现简单的用户注册登录(含数据库访问功能)

    Javaweb 实现简单的用户注册登录(含数据库访问功能) 实现效果图: 登录界面: 登陆成功: 登陆失败: 注册界面: 注册成功: 1.登录界面login.jsp <%@ page langu ...

  2. python实现注册功能_python注册、登录,python注册登录,#1、实现注册功能#

    python注册.登录,python注册登录,#1.实现注册功能##1.实现注册功能 #输入:username.passowrd,cpassowrd #最多可以输错3次 #3个都不能为空 #用户名长度 ...

  3. 后台管理系统2——登录、退出、注册功能、个人中心页面

    登录功能的实现 1.登录功能 1.1 页面内容的修改 1.2 路由的实现 1.3 登录页面的设计 1.4 登录逻辑实现 1.5 后台的实现 1.6 登录功能的修改 2 退出系统 3 注册功能 3.1 ...

  4. 【SDU项目实训2019级】前端和后端实现手机短信验证码登录和注册功能

    目录 1.前端登录页面手机号验证码登录页面: 2.前端获取验证码的函数: 3.后端获取验证码代码 4.前端登录的函数: 5.后端登录的实现 6.注册功能前后端的实现 1.前端登录页面手机号验证码登录页 ...

  5. WordPress添加前台注册功能

    一.添加注册表单 1.首先在当前主题的目录下新建一个php文件,命名为reg-page.php,然后将page.php中的所有代码复制到reg-page.php中: 2.删除reg-page.php开 ...

  6. java实现用户登录注册功能(用集合框架来实现)

    需求:实现用户登录注册功能(用集合框架来实现) 分析: A:需求的类和接口 1.用户类 UserBean 2.用户操作方法接口和实现类 UserDao UserDaoImpl 3.测试类 UserTe ...

  7. 注册登录案例用MVC和mysql_用MVC模式实现简单用户登录注册功能

    Model2模式 Jsp+Servlet+JavaBean MVC:开发模式 M:Model 模型层 ----> JavaBean V:View 视图层 ----> Jsp C:Contr ...

  8. 用java数组模拟登录和注册功能

    package com.linkage.login; import java.util.Scanner; public class user { // 存储用户名和密码 public static S ...

  9. Flask实战2问答平台-完成登录注册功能

    本来可以提前完成这篇的,结果测试时发现了一些问题,稍后将会提到. 上篇中我们已经完成了登录注册的界面,现在具体完成其具体功能. 1.注册功能 因为注册成功后,才能登录,我们在主py文件中添加如下 @a ...

最新文章

  1. javap 查看class文件的字节码命令用法
  2. 马化腾和扎克伯格,为什么抢着押注元宇宙?
  3. Windows平台RTMP/RTSP播放器如何实现实时音量调节
  4. oracle 关闭数据库实列,Oracle 11g 数据库启动和关闭
  5. php更多式样,php_1
  6. Android按back后执行过程,安卓app测试获取接口返回数据,然后处理数据,放在一个button点击事件里面 执行顺序有问题?无法获取数据?...
  7. 人脸识别数据集---CAS-PEAL-R1
  8. C4droid导出程序
  9. PS2022安装步骤 ps 2022(详细安装方法)
  10. 陀螺仪c语言算法,陀螺仪传感器建模与卡尔曼姿态解算
  11. Vue3中Compositions API的使用(一)
  12. 台式电脑怎么调分辨率_电脑屏幕分辨率调节方法
  13. java邮件数据库_javamail-demo(完整源码+数据库)
  14. js小学生图区_多种方式实现js图片预览
  15. 正则去掉首尾空格以及首尾的
  16. Python+OpenCV判断图像是黑底还是白底
  17. 优化策略5 Label Smoothing Regularization_LSR原理分析
  18. zynq linux环境移植,ZYNQ 7030 Linux 系统移植
  19. Mysql 中 case when then else end 用法
  20. 当我闲困的时候,我在想些什么

热门文章

  1. 都问我在阿里上班是什么体验?今天就闲聊一下在阿里上班的体验!
  2. xilinx基础篇Ⅰ(3)ISE14.7下载FPGA
  3. 2021-1-9 吴恩达-C5 序列模型-w2 自然语言处理与词嵌(课后编程1-Operations on word vectors 词向量运算-含UnicodeDecodeError解决)
  4. ArcGIS API for JavaScript开发之PopupTemplate
  5. 自动编码器(Autoencoder)
  6. win7 无显示器 服务器,win7显示器无信号怎么办?电脑显示器无信号修复方法
  7. illustrator下载_使用Illustrator和Photoshop创建复仇者联盟文字效果
  8. idea打jir放进项目里
  9. Objective-C基础教程读书笔记
  10. Google PR值