本文出自:http://blog.csdn.net/svitter

资源下载:

github:

git clone https://github.com/Svtter/JSP-tomcat-mysql

我把注冊和登陆写在一个Servlet处理里面了。一个用get,一个用post,这是不对的。

以后有时间改正吧。。假设单纯为了作业能够提交这个。

get和post简单来说差别就在于post更加安全。

能够详细查一查。。

使用了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;
}
}

JSP_tomcat_mysql_注冊验证用户;的更多相关文章

  1. Android实战简易教程-第二十三枪(基于Baas的用户注冊验证username是否反复功能!)

    接上一篇,加入验证用户名是否已经注冊功能! 仅仅须要改动MainActivity.java: package com.example.logintest; import java.util.List; ...

  2. YII用户注冊和用户登录(二)之登录和注冊在视图通过表单使用YII小物件并分析

    2 登录和注冊在视图通过表单使用YII小物件并分析 <?php $form = $this -> beginWidget('CActiveForm', array( 'enableClie ...

  3. YII用户注冊和用户登录(三)之模型中规则制定和分析

    3 模型中规则制定和分析 YII模型主要分为两类,一个数据模型,处理和数据库相关的增删改查.继承CActiveRecord.还有一个是表单模型,继承CFormModel.不与数据库进行交互.操作与数据 ...

  4. 4pda.ru注冊验证的解码算法

    代码源于看雪林版在我群里介绍注冊一个俄文安卓论坛.发出来了链接大家在測试注冊. http://4pda.ru/forum/index.php? 註册方式請参看: _https://forum.tuts ...

  5. YII用户注冊和用户登录(五)之进行session和cookie分析 ,并在前后区分session和cookie

    5 进行session和cookie分析 ,并在前后区分session和cookie: 记住登录状态 这样下次再登录站点的时候.就不用反复输入username和password. 是浏览器的cooki ...

  6. 怎样利用WordPress创建自己定义注冊表单插件

    来源:http://www.ido321.com/1031.html 原文:Creating a Custom WordPress Registration Form Plugin 译文:创建一个定制 ...

  7. struts2学习笔记(三)—— 在用户注冊程序中使用验证框架

    实现目标:       1.使用验证框架对用户注冊信息进行验证       2.验证username.password.邮箱不能为空       3.验证username.password长度     ...

  8. struts2+jquery验证注冊用户是否存在

    注冊界面 register.jsp <%@ page language="java" contentType="text/html; charset=UTF-8&q ...

  9. YII用户注冊表单的实现熟悉前台各个表单元素操作方式

    模还是必须定义两个基本方法.还有部分label标签映射为汉字,假设进行表单验证,还要定义一些验证规则: <? php /* * 用户模型 * */ class user extends CAct ...

随机推荐

  1. 转载:《理解OAuth 2.0》 阮一峰

    原文:http://www.ruanyifeng.com/blog/2014/05/oauth_2_0.html OAuth是一个关于授权(authorization)的开放网络标准,在全世界得到广泛 ...

  2. Node.js ECONNREFUSED错误

    1 现象 node服务器 遇见此错误,如下:events.js:71throw arguments[1]; // Unhandled 'error' event^Error: connect ECON ...

  3. 取消Eclipse控制台显示行数的限制

    --------------------------------------------------------------------------------------------------- ...

  4. CPU密集型 VS IO密集型

    CPU密集型 CPU密集型也叫计算密集型,指的是系统的硬盘.内存性能相对CPU要好很多,此时,系统运作大部分的状况是CPU Loading 100%,CPU要读/写I/O(硬盘/内存),I/O在很短的 ...

  5. LeetCode(37): 每k个一组翻转链表

    Hard! 题目描述: 编写一个程序,通过已填充的空格来解决数独问题. 一个数独的解法需遵循如下规则: 数字 1-9 在每一行只能出现一次. 数字 1-9 在每一列只能出现一次. 数字 1-9 在每一 ...

  6. cf1061D 贪心+multiset 好题!

    cf上的思维题真好! 本题是在模拟的基础上贪心即可:将n段时间按照左端点(右端点为第二关键字)从小到大排序,然后遍历每一个时间段. 对于每一个时间段[li,ri],先找到multiset中最靠近li但 ...

  7. JMeter 聚合报告 90%响应时间

    90%的响应时间理解 官方解释:90% Line - 90% of the samples took no more than this time. The remaining samples at ...

  8. ERP合同管理二(三十)

    未审核表单列表显示: 1.用户登录后,根据登录用户加载审核流程表中属于当前登录用户的未审核表单.2.点击选中未审核表单跳转到指定审核流程页面 if (Request.QueryString[" ...

  9. hdu 1455 N个短木棒 拼成长度相等的几根长木棒 (DFS)

    N根短木棒 能够拼成几根长度相等的长木棒 求长木棒的长度 如果答案不止一种 输出最小的 Sample Input95 2 1 5 2 1 5 2 141 2 3 40 Sample Output65 ...

  10. openstack时间不同步问题

    一.出现的问题 我们在安装openstack的时候如果没有设置计算节点和控制节点的的时间同步,当你虚拟机开机之后会存在控制节点和计算节点的时间 不一样,导致opstack无法登陆,报如下错误: 二.设 ...