数据库:Mysql

  除了_id自动增长,其余全是varchar

注册:register.jsp

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme() + "://"
+ request.getServerName() + ":" + request.getServerPort()
+ path + "/";
%> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>"> <title>My JSP 'Register.jsp' starting page</title> <meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<link rel="stylesheet" href="css/ah.css" />
<script type="text/javascript" src="js/jquery.min.js"></script>
<script>
function chkUname() {
var v = $("input[name='uname']").val();
if (v.trim() == "") {
alert("用户名不能为空");
return false;
}
return true;
} function chkPwd() {
var v = $("input[name='pwd']").val();
if (v.trim() == "") {
alert("密码不能为空");
return false;
}
return true;
} function chkPwd2() {
var pwd1 = $("input[name='pwd']").val();
var pwd2 = $("input[name='pwd2']").val();
if (pwd1 != pwd2) {
alert("两次输入的密码不一致");
return false;
}
return true;
} function chkEmail() {
var v = $("input[name='email']").val();
if (v.trim() == "") {
alert("邮箱不能为空");
return false;
} // 格式匹配
var reg = /^([a-zA-Z0-9_-])+@([a-zA-Z0-9_-])+(.[a-zA-Z0-9_-])+/;
if (!reg.test(v)) {
alert("邮箱的格式不正确");
return false;
}
return true;
}
function check() {
if (chkUname() && chkPwd() && chkPwd2() && chkEmail()) {
return true;
}
return false;
}
</script> </head> <body>
<form action="S003User?approach=register" method="post"
onsubmit="return check();">
<table align="center" border="0" class="padding_top">
<tr>
<td colspan="3" class="title_1">用户注册</td>
</tr>
<tr>
<td>用户名:</td>
<td><input type="text" name="uname" onblur="chkUname()"></td>
<td><span class='must_w'>*</span></td>
</tr>
<tr>
<td>密码:</td>
<td><input type="password" name="pwd" onblur="chkPwd()"></td>
<td><span class='must_w'>*</span></td>
</tr>
<tr>
<td>密码确认:</td>
<td><input type="password" name="pwd2" onblur="chkPwd2()"></td>
<td><span class='must_w'>*</span></td>
</tr>
<tr>
<td>邮箱:</td>
<td><input type="text" name="email" onblur="chkEmail()"></td>
<td><span class='must_w'>*</span></td>
</tr>
<tr>
<td>手机:</td>
<td><input type="text" name="tel"></td>
</tr>
<tr>
<td colspan="3" align="center"><input type="submit" value="提交" />
&nbsp;&nbsp;<input type="reset" value="重置" /> <a href="login.jsp">去注册</a>
</td>
</tr>
</table>
</form>
</body>
</html>

登录:login.jsp

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme() + "://"
+ request.getServerName() + ":" + request.getServerPort()
+ path + "/";
%> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>"> <title>My JSP 'login.jsp' starting page</title> <meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<link rel="stylesheet" href="css/ah.css" />
<script type="text/javascript" src="js/jquery.min.js"></script> </head> <body>
<!-- <form action="S003User?t=login" method="post"> -->
<form action="S003User?approach=login" method="post"
onsubmit="return check();">
<table align="center" border="0" class="padding_top">
<tr>
<td colspan="3" class="title_1">用户登录</td>
</tr>
<tr>
<td>用户名:</td>
<td><input type="text" name="uname" onblur="chkUname()"></td>
</tr>
<tr>
<td>密码:</td>
<td><input type="password" name="pwd" onblur="chkPwd()"></td>
</tr>
<tr>
<td colspan="3" align="center"><input type="submit" value="提交" />
&nbsp;&nbsp;<input type="reset" value="重置" /> <a
href="register.jsp">直接登录</a></td>
</tr>
</table>
</form>
</body>
</html>

CSS:ah.css

#tag {
color: red;
background-color: #00FFFF;
border-radius: 3px;
padding: 5px;
} /* 必须输入 */
.must_w {
color: red;
}
/* 上隔离 */
.padding_top {
padding-top: 50px;
} /* 页面标题1 */
.title_1 {
font-family: "微软雅黑";
font-size: larger;
font-weight: bold;
}

Servlet:S003User.java

package org.ah.servlet;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse; import org.ah.bean.UserBean;
import org.ah.dao.UserDao; public class S003User extends HttpServlet {
private static final long serialVersionUID = 1L; public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException { int ret = 0;
String uName = request.getParameter("uname");
String uPwd = request.getParameter("pwd");
UserDao dao = new UserDao(); String approach = request.getParameter("approach");
if ("register".equals(approach)) {
// 注册
String eMail = request.getParameter("email");
String tel = request.getParameter("tel");
UserBean u = new UserBean(uName, uPwd, eMail, tel);
ret = dao.register(u);
if (ret > 0) {
response.sendRedirect("login.jsp");
} else {
response.sendRedirect("register.jsp");
}
} else if ("login".equals(approach)) {
// 登录
UserBean u = new UserBean(uName, uPwd, "", "");
ret = dao.login(u);
if (ret > 0) {
response.sendRedirect("ArticleListServlet");
} else {
response.sendRedirect("login.jsp");
}
}
}
}

数据库连接:

package org.ah.dao;

import java.sql.*;

public class BaseDao {

    private static final String DRIVER = "com.mysql.jdbc.Driver";
private static final String URL = "jdbc:mysql://localhost:3306/ahrecommend";
public Connection conn;
public PreparedStatement ps;
public ResultSet rs; public void getConn() {
try {
Class.forName(DRIVER);
conn = (Connection) DriverManager
.getConnection(URL, "root", "root");
} catch (SQLException e) {
e.printStackTrace();
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
} public void closeConn() { try {
if (conn != null) {
conn.close();
conn = null;
}
if (ps != null) {
ps.close();
ps = null;
}
if (rs != null) {
rs.close();
rs = null;
}
} catch (SQLException e) {
e.printStackTrace();
}
}
}
package org.ah.dao;

import java.sql.ResultSet;
import java.sql.SQLException; import org.ah.bean.UserBean; public class UserDao extends BaseDao {
public int register(UserBean u) {
int ret = 0; super.getConn();
try {
StringBuffer sb = new StringBuffer();
sb.append(" INSERT INTO");
sb.append(" usermst");
sb.append(" VALUES");
sb.append(" (");
sb.append(" 0,");// 自增
sb.append(" ?,");
sb.append(" ?,");
sb.append(" ?,");
sb.append(" ?");
sb.append(" )");
String strSQL = sb.toString();
ps = conn.prepareStatement(strSQL);
ps.setString(1, u.getuName());
ps.setString(2, u.getuPwd());
ps.setString(3, u.geteMail());
ps.setString(4, u.getTel());
ret = ps.executeUpdate();
} catch (SQLException e) {
e.printStackTrace();
} finally {
super.closeConn();
}
return ret;
} public int login(UserBean u) {
int ret = 0;
super.getConn();
try {
StringBuffer sb = new StringBuffer();
sb.append(" SELECT");
sb.append(" uName");
sb.append(" FROM");
sb.append(" usermst");
sb.append(" WHERE");
sb.append(" uName = ?");
sb.append(" AND");
sb.append(" uPwd = ?");
String strSQL = sb.toString();
ps = conn.prepareStatement(strSQL);
ps.setString(1, u.getuName());
ps.setString(2, u.getuPwd());
super.rs = ps.executeQuery();
if (rs.next()) {
// 查询到(一条)数据
ret = 1;
}
} catch (SQLException e) {
e.printStackTrace();
} finally {
super.closeConn();
}
return ret;
}
}

JSP:注册&登录的更多相关文章

  1. JSP注册登录页教程

    转载请标明原文地址:http://www.cnblogs.com/zhangyukof/p/6785258.html  一.准备工作 已搭建好的SSH框架工程一个,如果没有,请参考我的上一篇文章< ...

  2. 我的第一个jsp程序-实现注册登录留言功能

    1,注册功能,包括两个页面 zhuce.jsp注册页面 <%@ page language="java" contentType="text/html; chars ...

  3. JavaWeb笔记——注册登录系统项目思路

    功能:   > 注册   > 登录 --------------------------------- JSP:   * login.jsp  --> 登录表单   * regist ...

  4. 注册登录系统项目思路 -- javaweb

    功能:   > 注册   > 登录   ---------------------------------   JSP:   * login.jsp  --> 登录表单   * re ...

  5. JavaWeb_利用Servlet处理注册登录请求

    利用Servlet处理注册登录请求 程序结构 <%@page import="com.Gary.model.User"%> <%@ page language=& ...

  6. java使用face++简单实现人脸识别注册登录

    java使用face++简单实现人脸识别注册登录 前言 人脸识别,好高大上!!! 理解之后很简单. 支付宝使用的就是face++, 至于face++账号信息,apikey…..,本文不做讲述,网上很多 ...

  7. 基于javaweb人脸识别注册登录系统

    ---恢复内容开始--- 现在是2019年,人脸识别技术已经相当成熟了,百度自2017年发布人脸识别技术,已经被广泛应用,不管从现在的iphoneX掀起的面部解锁到手机应用端的各种人脸认证,这一技术已 ...

  8. 一步步开发自己的博客 .NET版(3、注册登录功能)

    前言 这次开发的博客主要功能或特点:    第一:可以兼容各终端,特别是手机端.    第二:到时会用到大量html5,炫啊.    第三:导入博客园的精华文章,并做分类.(不要封我)    第四:做 ...

  9. Android开发案例 - 注册登录

    本文只涉及UI方面的内容, 如果您是希望了解非UI方面的访客, 请跳过此文. 在微博, 微信等App的注册登录过程中有这样的交互场景(如下图): 打开登录界面 在登录界面中, 点击注册, 跳转到注册界 ...

随机推荐

  1. ubuntu 16.04卸载不必要的默认安装软件

    两个办法,一个在ubuntu软件里一个一个删,明显的windows下做法. 还有一个通过终端来删除.ctrl+alt+t打开终端. 1.卸载libreoffices(要删一起删了,然后去装office ...

  2. tomcat中配置https

    HTTPS配置中分为单向连接和双向连接,单向连接只需要服务器安装证书,客户端不需要,双向连接需要服务器和客户端都安装证书: 一.Keytool命令: 1.生成密钥对: keytool -genkey ...

  3. Jmeter实现接口自动化测试

    一.环境准备 1.Jdk1.7或以上: 下载地址:http://www.oracle.com/technetwork/java/javase/downloads/jdk8-downloads-2133 ...

  4. VC++6 调用teststand api的方法

    参考example中的vs2010的例子,创建MFC工程 打开project->setting ,c/c++ tab  cat 选 Proprocessor ->Additional in ...

  5. spring-IOC容器(一)

    ApplicationContext 代表IOC容器(控制反转) ApplicationContext的主要实现类: ——ClassPathXmlApplicationContext:从类路径下加载配 ...

  6. Redis sortedset有效集合数据结构

    1. 增加一个有效集合 2. 查看元素个数 3. zscore 4. zcount 5. 返回指定元素的索引 zrank 6.zincrby 给元素a加90分 7. zrange查看范围

  7. App音频内录 录音

    1.android模拟器 天天模拟器+BlueStacks 2.高清内录软件 Audio Record Wizard.exe 3.音频剪切软件 Adobe Audition CS6

  8. phpmailer使用qq邮箱、163邮箱成功发送邮件实例代码

    以前使用qq邮箱.163服务器发送邮件,帐号直接使用密码,现在不行了,得使用授权码,简单记录下 1.首先开通POP3/SMTP服务,qq邮箱——帐号——设置,找到POP3/SMTP点开启,输入短信会有 ...

  9. DOM confirm setTimeout url刷新

    console.log 输出框 alert 弹出框 confirm 确认框 // URL和刷新 location.href 获取URL location.href = "url" ...

  10. 如何让classmethod只允许使用用类对象来调用

    Django REST framework里面有这样一段代码,在网上查@classonlymethod的意思是使得classmethod只允许使用用类对象来调用 @classonlymethod de ...