您的位置:首页 > 编程语言 > Java开发

javaWeb后台版登陆注册

2016-07-19 15:50 573 查看
创建一个web项目名为bbb

1 创建数据库连接

(1)创建一个包cn.edu.hpu.util

(2)写代码

package cn.edu.hpu.util;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

public class DBOperate {
static {
try {
Class.forName("com.mysql.jdbc.Driver");
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}

public static Connection getConnection () {
Connection conn=null;
try {
conn=DriverManager.getConnection("jdbc:mysql://localhost:3306/sas", "root", "123456");
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return conn;
}

public static void close(Statement st,ResultSet rs,Connection conn) {

try {
if(st!=null) {
st.close();
}

if(rs!=null) {
rs.close();
}

if(conn!=null) {
conn.close();
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

}

public static void close(PreparedStatement pst,Connection conn) {
try {
if(pst!=null) {
pst.close();
}
if(conn!=null) {
conn.close();
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}


写一个使用者类

(1)创建包cn.edu.hpu.user

(2)写代码

package cn.edu.phu.user;

public class User {
private int id;
private String username;
private String 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;
}

}


创建管理接口和其实现类

(1)创建包cn.edu.hpu.manager

(2)写接口代码

package cn.edu.hpu.manager;

import cn.edu.phu.user.User;

public interface Manager {
public User CheckLogin(String username, String password);
public boolean RegestUser(User u);
}


实现接口的类

package cn.edu.hpu.manager;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

import cn.edu.hpu.util.DBOperate;
import cn.edu.phu.user.User;

public class ManagerImpl implements Manager {

public User CheckLogin(String username , String password) {
User u =new User();
String sql="select * from ta";

Connection conn=null;
Statement st=null;
ResultSet rs=null;

conn=(Connection) DBOperate.getConnection();
try {
//Statement createStatement()
//创建一个 Statement 对象来将 SQL 语句发送到数据库。
//编译
st=(Statement) conn.createStatement();
//从数据库里查的内容作为结果集
rs=(ResultSet) st.executeQuery(sql);
//遍历这个结果集
while (rs.next()) {
String name=rs.getString("username");
String psw=rs.getString("password");
if(psw.equals(password) && name.equals(username)) {
u.setId(rs.getInt("id"));
u.setUsername(rs.getString("username"));
u.setUsername(rs.getString("password"));

}
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return u;
}

@Override
public boolean RegestUser(User u) {
boolean flag=false;
Connection conn=(Connection) DBOperate.getConnection();
String sql="insert into ta(username,password) value(?,?)";

try {
PreparedStatement pst=conn.prepareStatement(sql);
pst.setString(1, u.getUsername());
pst.setString(2, u.getPassword());

//插入之后需要更新一下
pst.executeUpdate();
flag=true;
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

return flag;
}

}


可以测试一下

(1)创建一个包cn.edu.hpu.text

(2)写代码

package cn.edu.phu.text;

import java.sql.Connection;
import cn.edu.hpu.manager.Manager;
import cn.edu.hpu.manager.ManagerImpl;
import cn.edu.hpu.util.DBOperate;
import cn.edu.phu.user.User;

public class Test {
public static void main(String[] args) {
//测试链接成功
Connection conn=DBOperate.getConnection();
if(conn!=null) {
System.out.println("ok");
}else{
System.out.println("error");
}

//插入信息
Manager ma=new ManagerImpl();
User us=new User();

us.setUsername("222");
us.setPassword("222");
boolean flag=ma.RegestUser(us);
if(flag) {
System.out.println("ok");
}else{
System.out.println("error");
}
//查询信息
Manager m=new ManagerImpl();
User u=new User();
u=m.CheckLogin("123", "123");
if(!(u.getUsername().equals(null))) {
System.out.println("123");
}else{
System.out.println("000");
}

}

}


创建jsp

(1)创建index.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 'index.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" type="text/css" href="styles.css">
-->
</head>

<body>
<form action="cn.edu.hpu.servlet/CheckLogin" method = "post">
<div id = "head" align="center">
<h1>登录•</h1>
</div>
<div id = "body" align="center">
<table>
<tr>
<td>用户名</td>
<td><input id = "username" name = "username"></td>
</tr>
<tr>
<td>密码</td>
<td><input type = "password" id = "password" name = "password"></td>
</tr>
</table>
</div>
<div id = "bottom" align="center">
<input type = "submit" value = "提交">
<input type = "reset"  value = "重置">
<a href= "zhuce.jsp">注册</a>
</div>
</body>
</html>


创建zhuce.jsp

<%@ page language="java" pageEncoding="utf-8"%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html:html locale="true">
<head>
<html:base />

<title>zhuce.jsp</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" type="text/css" href="styles.css">
-->
</head>

<body>
<h1 align="center">注册新用户</h1>
<hr/>
<form action="add" method="post" align="center">
用户名:<input name="Username" type="text"/><br>
<p></p>
密 码:<input name="Password" type="password"/><br>
<p></p>
验证码:<input name="Code" type="text"></br>
<p></p>
<input type="submit" style="width:80px;height:35px "value="注册"/>
</form>
<a href="index.jsp">返回</a>
</body>
</html:html>
(3)创建success.jsp
<%@ page language="java" pageEncoding="ISO-8859-1"%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html:html locale="true">
<head>
<html:base />

<title>success.jsp</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" type="text/css" href="styles.css">
-->
</head>
<body>
<h1>lend success!</h1>
</body>
</html:html>


创建error.jsp

<%@ page language="java" pageEncoding="ISO-8859-1"%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html:html locale="true">
<head>
<html:base />
<title>error.jsp</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" type="text/css" href="styles.css">
-->
</head>

<body>
<h1>lend error!</h1>
</body>
</html:html>


对index.jsp和zhuce.jsp接收一下,创建包cn.edu.hpu.selvert

(1)对index.jsp接收,创建servlet名为CheckLogin

package cn.edu.hpu.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 cn.edu.hpu.manager.Manager;
import cn.edu.hpu.manager.ManagerImpl;
import cn.edu.phu.user.User;

public class CheckLogin extends HttpServlet {

private static final long serialVersionUID=1L;

public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doPost(request,response);
}

public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
//防止乱码
request.setCharacterEncoding("utf-8");
response.setCharacterEncoding("utf-8");
String username=request.getParameter("username");
String password=request.getParameter("password");
//System.out.println(username+" "+password);
Manager mng=new ManagerImpl();
User u=mng.CheckLogin(username, password);
System.out.println(u.getUsername());
if(!u.getUsername().equals(null)) {
response.sendRedirect("../success.jsp");
}else{
response.sendRedirect("../error.jsp");
}
}

}


对zhuce.jsp接收一下,创建servlet名为Register

package cn.edu.hpu.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 cn.edu.hpu.manager.Manager;
import cn.edu.hpu.manager.ManagerImpl;
import cn.edu.phu.user.User;

public class Register extends HttpServlet {
private static final long serialVersionUID=1L;
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doPost(request,response);
}

public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
//防止乱码
request.setCharacterEncoding("utf-8");
response.setCharacterEncoding("utf-8");
//这是取页面的参数用的,当页面的表单参数通过request里面传过来,就可以通过这个方法取出来
String username=request.getParameter("username");
String password=request.getParameter("password");
System.out.println(username+"   "+password);
Manager m=new ManagerImpl();
User u=new User();
u.setUsername(username);
u.setPassword(password);
boolean flag=m.RegestUser(u);
if(flag) {
response.sendRedirect("../success.jsp");
}else{
response.sendRedirect("../error.jsp");
}

}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  java web