jsp登录注册
只帖源代码,,,,不讲解。
1、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>
<title>登录</title>
</head> <body>
<form action="LoginServlet" method="post">
账号:<input type="text" name="account"/><br/>
密码:<input type="password" name="password"/><br/><br/>
<input type="submit" name="submit" value="登录" />
<a href="Register.jsp">注册</a>
</form>
</body>
</html>
2、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>
<title>注册</title>
</head> <body>
<form action="RegisterServlet" method="post">
账号:<input type="text" name="account"><br/>
密码:<input type="password" name="password"><br/>
性别:<input type="text" name="sex"><br/>
邮箱:<input type="text" name="email"><br/><br/>
<input type="submit" name="submit" value="注册">
</form>
</body>
</html>
3、User类,构造函数封装数据
package com.cn.jspT2;
public class User {
private String account;
private String password;
private String sex;
private String email;
public String getAccount() {
return account;
}
public void setAccount(String account) {
this.account = account;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public User(String account, String password, String sex, String email) {
super();
this.account = account;
this.password = password;
this.sex = sex;
this.email = email;
}
public User() {
super();
}
}
4、DBHelper类
package com.cn.jspT2; import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.Statement; /**
* 获取数据库操作的连接对象
* 关闭数据库操作的各种资源
* @author 晏先政
*
*/
public class DBHelper {
private static final String className = "com.mysql.jdbc.Driver";
private static final String url = "jdbc:mysql://localhost:3306/jsp?characterEncoding=utf8&useSSL=true";
private static final String uname = "root";
private static final String upass = "211599100yxz"; /**
* 获取数据库连接对象的方法
*/
public static Connection getConn(){
Connection conn = null;
try{
Class.forName(className);
conn = DriverManager.getConnection(url,uname, upass);
} catch(Exception e){
e.printStackTrace();
} return conn;
} /**
* 关闭数据库连接对象
*/
public static void closeConn(Connection conn){
try{
if(conn!=null){
conn.close();
}
} catch(Exception e){
e.printStackTrace();
}
} /**
* 关闭数据库操作对象
*/
public static void closeStmt(Statement stmt){
try{
if(stmt!=null){
stmt.close();
}
} catch(Exception e){
e.printStackTrace();
}
} /**
* 关闭数据库操作对象
*/
public static void closePstmt(PreparedStatement pstmt){
try{
if(pstmt!=null){
pstmt.close();
}
} catch(Exception e){
e.printStackTrace();
}
} /**
* 关闭数据库操作对象
*/
public static void closeRs(ResultSet rs){
try{
if(rs!=null){
rs.close();
}
} catch(Exception e){
e.printStackTrace();
}
}
}
5、UserDao类,操作数据库
package com.cn.jspT2; import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet; public class UserDao {
private Connection conn = null;
private PreparedStatement pstmt = null;
private ResultSet rs = null;
public void addUser(User user){
try {
conn = DBHelper.getConn();
String sql = "insert into user_info(account,password,sex,email) values(?,?,?,?)";
PreparedStatement pstmt = conn.prepareStatement(sql);
pstmt.setString(1, user.getAccount());
pstmt.setString(2, user.getPassword());
pstmt.setString(3, user.getSex());
pstmt.setString(4, user.getEmail());
pstmt.execute();
} catch (Exception e) {
e.printStackTrace();
}finally{
DBHelper.closePstmt(pstmt);
DBHelper.closeConn(conn);
}
}
public Boolean queryUser(String account,String password){
try {
conn = DBHelper.getConn();
String sql = "select * from user_info where account=? and password=?";
pstmt = conn.prepareStatement(sql);
pstmt.setString(1, account);
pstmt.setString(2, password);
rs = pstmt.executeQuery();
if(rs.next()) {
return true;
}else{
return false;
}
} catch (Exception e) {
e.printStackTrace();
}finally{
DBHelper.closeRs(rs);
DBHelper.closePstmt(pstmt);
DBHelper.closeConn(conn);
}
return false;
}
}
6、LoginServlet
package com.cn.jspT2; import java.io.IOException;
import java.io.PrintWriter; import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse; public class LoginServlet extends HttpServlet { public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { this.doPost(request, response);
} public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { request.setCharacterEncoding("utf-8");
String account = request.getParameter("account");
String password = request.getParameter("password");
UserDao userDao = new UserDao();
Boolean user = userDao.queryUser(account, password);
if(user){
response.setContentType("text/html;charset=utf-8");
PrintWriter pw = response.getWriter();
pw.print("<script> window.onload=function(){alert('"+account+"恭喜你登录成功');}</script>");
}else{
response.setContentType("text/html;charset=utf-8");
PrintWriter pw = response.getWriter();
pw.print("<script> window.onload=function(){alert('对不起账号或者密码不正确');}</script>");
} }
}
7、RegisterServlet
package com.cn.jspT2; import java.io.IOException;
import java.io.PrintWriter; import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse; public class RegisterServlet extends HttpServlet { public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { this.doPost(request, response);
} public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { request.setCharacterEncoding("utf-8");
String account = request.getParameter("account");
String password = request.getParameter("password");
String sex = request.getParameter("sex");
String email = request.getParameter("email");
User user = new User();
user.setAccount(account);
user.setPassword(password);
user.setSex(sex);
user.setEmail(email);
UserDao userdao = new UserDao();
userdao.addUser(user);
response.setContentType("text/html;charset=utf-8");
// response.setCharacterEncoding("utf-8");
PrintWriter pw = response.getWriter();
pw.print("<script> window.onload=function(){alert('"+account+"恭喜你注册成功');}</script>");
response.sendRedirect("Login.jsp"); }
}
jsp登录注册的更多相关文章
- javaweb学习总结(二十二)——基于Servlet+JSP+JavaBean开发模式的用户登录注册
一.Servlet+JSP+JavaBean开发模式(MVC)介绍 Servlet+JSP+JavaBean模式(MVC)适合开发复杂的web应用,在这种模式下,servlet负责处理用户请求,jsp ...
- 纯JSP实现用户登录注册,记事本
没有美化,没有格式,没有样式 1.JSP登陆注册 将用户注册的信息保存在application对象中,用于登录时的验证. 首页如下: 如果未登录,在 session 中找不到 currentUser ...
- JavaWeb学习 (二十一)————基于Servlet+JSP+JavaBean开发模式的用户登录注册
一.Servlet+JSP+JavaBean开发模式(MVC)介绍 Servlet+JSP+JavaBean模式(MVC)适合开发复杂的web应用,在这种模式下,servlet负责处理用户请求,jsp ...
- JSP:注册&登录
数据库:Mysql 除了_id自动增长,其余全是varchar 注册:register.jsp <%@ page language="java" import="j ...
- 基于Servlet+JSP+JavaBean开发模式的用户登录注册
http://www.cnblogs.com/xdp-gacl/p/3902537.html 一.Servlet+JSP+JavaBean开发模式(MVC)介绍 Servlet+JSP+JavaBea ...
- javaweb(二十二)——基于Servlet+JSP+JavaBean开发模式的用户登录注册
一.Servlet+JSP+JavaBean开发模式(MVC)介绍 Servlet+JSP+JavaBean模式(MVC)适合开发复杂的web应用,在这种模式下,servlet负责处理用户请求,jsp ...
- 登录注册案例(Servlet+JSP+Maven)
项目案例模板之登录注册的实现 案例演示 案例代码 设计表 pom.xml <dependencies> <dependency> <groupId>jun ...
- Java Web项目案例之---登录注册和增删改查(jsp+servlet)
登录注册和增删改查(jsp+servlet) (一)功能介绍 1.用户输入正确的密码进行登录 2.新用户可以进行注册 3.登录后显示学生的信息表 4.可以添加学生 5.可以修改学生已有信息 6.可以删 ...
- JavaWeb实现用户登录注册功能实例代码(基于Servlet+JSP+JavaBean模式)
一.Servlet+JSP+JavaBean开发模式(MVC)介绍 Servlet+JSP+JavaBean模式(MVC)适合开发复杂的web应用,在这种模式下,servlet负责处理用户请求,jsp ...
随机推荐
- python3 字典dict
字典是用大括号{}表示 dict() 键必须是唯一的,但值则不必:键是不可变的,如字符串.数字.元组,值可以取任意数据类型: 可以迭代, del可以删除一对键值,del Dict['key'] 重复给 ...
- Tarjan求无向图割点、桥详解
tarjan算法--求无向图的割点和桥 一.基本概念 1.桥:是存在于无向图中的这样的一条边,如果去掉这一条边,那么整张无向图会分为两部分,这样的一条边称为桥无向连通图中,如果删除某边后,图变成不 ...
- jmeter生成测试报告
- springboot jar包运行中获取资源文件
1. 今天晚上写了一个程序,基于Spring boot的一个小网站,发现使用FileUtils.class.getResource(path)来获取jar包中的资源文件并不能成功,其路径很奇怪 fil ...
- 洛谷P3321 序列统计
气死了,FFT了半天发现是NTT... 1004535809 这个东西是NTT模数,原根为3. 题意:给定集合,元素的大小不超过M.用这些元素组成长为n的序列,要求乘积模M为k,求方案数. n < ...
- 【洛谷P2142 高精度减法】
题目描述 高精度减法 输入输出格式 输入格式: 两个整数a,b(第二个可能比第一个大) 输出格式: 结果(是负数要输出负号) 输入输出样例 输入样例#1: 复制 2 1 输出样例#1: 复制 1 说明 ...
- JMeter关联(正则表达式提取器)
正则表达式总结 关联:与系统交互过程中,系统返回的内容,需要在接下来的交互中用到,如防止csrf攻击而生成的token. 从前一个请求中取,用Regular Expression Extractor ...
- P2602 [ZJOI2010]数字计数
https://www.luogu.org/problemnew/show/P2602 数位dp #include <bits/stdc++.h> using namespace std; ...
- SpringCloud-初识
说道SpringCloud,原来就去了解过,也有很大兴趣,只是当初不知道这是个什么东西.在它之前,我学习Spring,在官网肆无忌惮的逛的时候,发现了SpringBoot,那个时候就打算开始学习Spr ...
- tensorflow中tf.ConfigProto()用法解释
在看C3D代码的时候,看见有一段代码是 config = tf.ConfigProto()#主要是配置tf.Session的运行方式,GPU还是CPU,在这里选择的是GPU的运行方式 config.g ...