JSP_tomcat_mysql_注冊验证用户;
本文出自: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_注冊验证用户;的更多相关文章
- Android实战简易教程-第二十三枪(基于Baas的用户注冊验证username是否反复功能!)
接上一篇,加入验证用户名是否已经注冊功能! 仅仅须要改动MainActivity.java: package com.example.logintest; import java.util.List; ...
- YII用户注冊和用户登录(二)之登录和注冊在视图通过表单使用YII小物件并分析
2 登录和注冊在视图通过表单使用YII小物件并分析 <?php $form = $this -> beginWidget('CActiveForm', array( 'enableClie ...
- YII用户注冊和用户登录(三)之模型中规则制定和分析
3 模型中规则制定和分析 YII模型主要分为两类,一个数据模型,处理和数据库相关的增删改查.继承CActiveRecord.还有一个是表单模型,继承CFormModel.不与数据库进行交互.操作与数据 ...
- 4pda.ru注冊验证的解码算法
代码源于看雪林版在我群里介绍注冊一个俄文安卓论坛.发出来了链接大家在測试注冊. http://4pda.ru/forum/index.php? 註册方式請参看: _https://forum.tuts ...
- YII用户注冊和用户登录(五)之进行session和cookie分析 ,并在前后区分session和cookie
5 进行session和cookie分析 ,并在前后区分session和cookie: 记住登录状态 这样下次再登录站点的时候.就不用反复输入username和password. 是浏览器的cooki ...
- 怎样利用WordPress创建自己定义注冊表单插件
来源:http://www.ido321.com/1031.html 原文:Creating a Custom WordPress Registration Form Plugin 译文:创建一个定制 ...
- struts2学习笔记(三)—— 在用户注冊程序中使用验证框架
实现目标: 1.使用验证框架对用户注冊信息进行验证 2.验证username.password.邮箱不能为空 3.验证username.password长度 ...
- struts2+jquery验证注冊用户是否存在
注冊界面 register.jsp <%@ page language="java" contentType="text/html; charset=UTF-8&q ...
- YII用户注冊表单的实现熟悉前台各个表单元素操作方式
模还是必须定义两个基本方法.还有部分label标签映射为汉字,假设进行表单验证,还要定义一些验证规则: <? php /* * 用户模型 * */ class user extends CAct ...
随机推荐
- Eclipse与github整合
Eclipse与github整合 Windows系统下: github官方指南:https://help.github.com/articles/set-up-git Git?是个正快速成长的版本控制 ...
- python-pandas 高级功能(通过学习kaggle案例总结)
方法.iterrows()遍历循环df中的元素. for index,row in df.iterrows(): pass 更改df一个元素中的变量值. data1.set_value(index,' ...
- pytorch实现花朵数据集读取
import os from PIL import Image from torch.utils import data import numpy as np from torchvision imp ...
- jquery----扩展事件
常用事件 blur([[data],fn]) 失去焦点 focus([[data],fn]) 获取焦点( 搜索框例子) change([[data],fn]) 当select下拉框中的元素发生改变的时 ...
- Luogu P4944 【PION贪吃蛇】
简单模拟题 用一个数据结构存储这条蛇 考虑蛇的移动 1,如果死了,就把整个蛇清空,所有位置标记为食物 2,如果吃了东西,把这个位置更新为蛇头 3,如果正常走路,这个位置设为蛇头,同时删掉尾巴 蛇的存储 ...
- node.js开发博客系统---前端项目搭建(一)
Express: https://github.com/petecoop/generator-express 安装node.js和npm 执行: npm install -g yo npm insta ...
- jq:翻页时,保存上页多选框checkbox选中状态
这里主要讲一种:中间的 checkbox 是 通过Ajax调出的. 则翻页时,为了保存上页的选定状态,可在页面中定义一个变量,用来存储选中状态的值. <input class="cli ...
- SpringBatch 错误积累
1.如果nextStep在该JOB中还没有配置,也就是说nextStep还不存在的情况下,就会报错 <end on="EIXT WITH IMBALANCE" /> & ...
- [转] Java接口_interface_implements
相对抽象类来讲,接口就是比抽象类还要抽象的抽象类,丝毫不带半点实现的内容.接口可以更加规范的对子类进行约束.接口全面地专业地实现了:规范和具体实现的分离.接口就是规范,定义的是一组规则,提现了现实世界 ...
- LeetCode 4. Median of Two Sorted Arrays (分治)
两个有序的数组 nums1 和 nums2 维数分别为m,n.找所有数的中位数,复杂度 O(log (m+n)) 注意:奇偶个数,分治法求解,递归出口特殊处理.取Kth smallest数时,分治取m ...