求大神帮忙做个JAVA jsp的登陆注册
需要用MVC结构,有基本的页面和数据库只要写下登录和注册的代码,有没有大神愿意做的,者留个邮箱,谢谢了...
需要用MVC结构,有基本的页面和数据库只要写下登录和注册的代码,有没有大神愿意做的,者留个邮箱,谢谢了
展开
展开全部
用JSP ,JavaBean ,servlet做的
登陆界面
代码如下:
数据库的连接文件:
package jdbc;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.HashMap;
import java.util.LinkedList;
public class Jdbc {
String url="jdbc:mysql://localhost:3306/bank";
String username="root";
String password="guo";
Statement stat=null;
Connection coon=null;
ResultSet res=null;
/** 构造方法。用于连接数据库
*
*/
public Jdbc(){
try {
Class.forName("com.mysql.jdbc.Driver");
coon=DriverManager.getConnection(url,username,password);
stat=coon.createStatement();
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
}
}
//事务。。。
void setAutoCommit(boolean tag){
try {
coon.setAutoCommit(tag);
} catch (SQLException e) {
e.printStackTrace();
}
}
//执行事务
void commithh(){
try {
coon.commit();
} catch (SQLException e) {
e.printStackTrace();
}
}
/**
* 用于对数据库进行增删改操作
* @param sql sql语句
* @return 影响了几行
*/
public int update(String sql){
try{
int i = stat.executeUpdate(sql);
return i;
}catch(SQLException o){
o.printStackTrace();
}
return -1;
}
/**
* 用于对数据库进行查询操作
* @param sql sql语句
* @return 数据集
*/
private ResultSet query(String sql){
try{
res=stat.executeQuery(sql);
return res;
}catch(SQLException o){
o.printStackTrace();
}
return null;
}
public LinkedList<HashMap<String, String>> queryToList(String sql){
LinkedList<HashMap<String,String>> list=new LinkedList<HashMap<String,String>>();
res = query(sql);
try {
ResultSetMetaData mt = res.getMetaData();
while(res.next()){
HashMap<String,String> map=new HashMap<String,String>();
for(int i=1;i<=mt.getColumnCount();i++){
String ColumnName=mt.getColumnName(i);
String ColumnValue=res.getString(ColumnName);
map.put(ColumnName, ColumnValue);
}
list.add(map);
}
} catch (SQLException e) {
e.printStackTrace();
}
return list;
}
/**
* 对数据库进行批处理。。
* @param sql 要执行的SQL语句数组
* @return 影响了几行,数组。。
*/
public int[] executeBatch(String[] sql){
for(int i=0;i<sql.length;i++){
try {
stat.addBatch(sql[i]);
} catch (SQLException e) {
e.printStackTrace();
}
}
try {
return stat.executeBatch();
} catch (SQLException e) {
e.printStackTrace();
}
return null;
}
/**
* 关闭数据库
*/
public void close(){
try {
if(res!=null){
res.close();
}
} catch (SQLException e) {
e.printStackTrace();
}
try {
if(stat!=null){
stat.close();
}
} catch (SQLException e) {
e.printStackTrace();
}
try {
if(coon!=null){
coon.close();
}
} catch (SQLException e) {
e.printStackTrace();
}
}
}
Denglu.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>登陆界面</title>
</head>
<body bgcolor="yellow">
<br><br>
<center><h1>登陆界面</h1></center><br><hr><br><br>
<form action="yan.view" method="post">
<table align="center">
<tr height=40>
<td>姓名:</td>
<td><input type="text" name="name"></td>
</tr>
<tr height=40>
<td>密码:</td>
<td><input type="password" name="password"></td>
</tr>
<tr height=40>
<td width=60 align="right"><input type="submit" name="submit" value=" 提 交 "></td>
<td> <input type="reset" name="reset" value=" 重 置 "></td>
</tr>
</table>
</form>
</body>
</html>
Successful.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>成功登陆后的界面</title>
</head>
<body bgcolor="yellow">
<h1>you are successful!</h1>
</body>
</html>
Dao.java
package userlogin;
import java.sql.ResultSet;
import java.sql.SQLException;
import jdbc.DBhelper;
public class Dao {
public boolean logoin(User user) throws SQLException{
boolean i = false;
DBhelper db=new DBhelper();
String sql="select * from user where userName='"+user.getName()+"' and pssWord='"+user.getPassword()+"'";
ResultSet rs=db.doQuery(sql);
if (rs.next()) { i = true; }
else { i = false ;}
return i;
}
}
User.java
package userlogin;
public class User {
String name,password;
public void setName(String name){
this.name=name;
}
public String getName(){
return name;
}
public void setPassword(String password){
this.password=password;
}
public String getPassword(){
return password;
}
}
YanServlet.java
package userlogin;
import java.io.IOException;
import java.sql.SQLException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
@WebServlet("/yan.view")
public class YanzServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
public YanzServlet() {
super();
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doPost(request,response);
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
response.setContentType("text/html");
response.setCharacterEncoding("utf-8");
String name=new String(request.getParameter("name").getBytes("ISO-8859-1"),"utf-8");
String password=new String(request.getParameter("password").getBytes("ISO-8859-1"),"utf-8");
User user=new User();
user.setName(name);
user.setPassword(password);
Dao dao=new Dao();
boolean isLogin;
try {
isLogin=dao.logoin(user);
if(isLogin){
response.sendRedirect("sucessful.jsp");
}else{
response.sendRedirect("denglu.jsp");
}
} catch (SQLException e) {
e.printStackTrace();
}
}
}
登陆界面
代码如下:
数据库的连接文件:
package jdbc;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.HashMap;
import java.util.LinkedList;
public class Jdbc {
String url="jdbc:mysql://localhost:3306/bank";
String username="root";
String password="guo";
Statement stat=null;
Connection coon=null;
ResultSet res=null;
/** 构造方法。用于连接数据库
*
*/
public Jdbc(){
try {
Class.forName("com.mysql.jdbc.Driver");
coon=DriverManager.getConnection(url,username,password);
stat=coon.createStatement();
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
}
}
//事务。。。
void setAutoCommit(boolean tag){
try {
coon.setAutoCommit(tag);
} catch (SQLException e) {
e.printStackTrace();
}
}
//执行事务
void commithh(){
try {
coon.commit();
} catch (SQLException e) {
e.printStackTrace();
}
}
/**
* 用于对数据库进行增删改操作
* @param sql sql语句
* @return 影响了几行
*/
public int update(String sql){
try{
int i = stat.executeUpdate(sql);
return i;
}catch(SQLException o){
o.printStackTrace();
}
return -1;
}
/**
* 用于对数据库进行查询操作
* @param sql sql语句
* @return 数据集
*/
private ResultSet query(String sql){
try{
res=stat.executeQuery(sql);
return res;
}catch(SQLException o){
o.printStackTrace();
}
return null;
}
public LinkedList<HashMap<String, String>> queryToList(String sql){
LinkedList<HashMap<String,String>> list=new LinkedList<HashMap<String,String>>();
res = query(sql);
try {
ResultSetMetaData mt = res.getMetaData();
while(res.next()){
HashMap<String,String> map=new HashMap<String,String>();
for(int i=1;i<=mt.getColumnCount();i++){
String ColumnName=mt.getColumnName(i);
String ColumnValue=res.getString(ColumnName);
map.put(ColumnName, ColumnValue);
}
list.add(map);
}
} catch (SQLException e) {
e.printStackTrace();
}
return list;
}
/**
* 对数据库进行批处理。。
* @param sql 要执行的SQL语句数组
* @return 影响了几行,数组。。
*/
public int[] executeBatch(String[] sql){
for(int i=0;i<sql.length;i++){
try {
stat.addBatch(sql[i]);
} catch (SQLException e) {
e.printStackTrace();
}
}
try {
return stat.executeBatch();
} catch (SQLException e) {
e.printStackTrace();
}
return null;
}
/**
* 关闭数据库
*/
public void close(){
try {
if(res!=null){
res.close();
}
} catch (SQLException e) {
e.printStackTrace();
}
try {
if(stat!=null){
stat.close();
}
} catch (SQLException e) {
e.printStackTrace();
}
try {
if(coon!=null){
coon.close();
}
} catch (SQLException e) {
e.printStackTrace();
}
}
}
Denglu.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>登陆界面</title>
</head>
<body bgcolor="yellow">
<br><br>
<center><h1>登陆界面</h1></center><br><hr><br><br>
<form action="yan.view" method="post">
<table align="center">
<tr height=40>
<td>姓名:</td>
<td><input type="text" name="name"></td>
</tr>
<tr height=40>
<td>密码:</td>
<td><input type="password" name="password"></td>
</tr>
<tr height=40>
<td width=60 align="right"><input type="submit" name="submit" value=" 提 交 "></td>
<td> <input type="reset" name="reset" value=" 重 置 "></td>
</tr>
</table>
</form>
</body>
</html>
Successful.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>成功登陆后的界面</title>
</head>
<body bgcolor="yellow">
<h1>you are successful!</h1>
</body>
</html>
Dao.java
package userlogin;
import java.sql.ResultSet;
import java.sql.SQLException;
import jdbc.DBhelper;
public class Dao {
public boolean logoin(User user) throws SQLException{
boolean i = false;
DBhelper db=new DBhelper();
String sql="select * from user where userName='"+user.getName()+"' and pssWord='"+user.getPassword()+"'";
ResultSet rs=db.doQuery(sql);
if (rs.next()) { i = true; }
else { i = false ;}
return i;
}
}
User.java
package userlogin;
public class User {
String name,password;
public void setName(String name){
this.name=name;
}
public String getName(){
return name;
}
public void setPassword(String password){
this.password=password;
}
public String getPassword(){
return password;
}
}
YanServlet.java
package userlogin;
import java.io.IOException;
import java.sql.SQLException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
@WebServlet("/yan.view")
public class YanzServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
public YanzServlet() {
super();
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doPost(request,response);
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
response.setContentType("text/html");
response.setCharacterEncoding("utf-8");
String name=new String(request.getParameter("name").getBytes("ISO-8859-1"),"utf-8");
String password=new String(request.getParameter("password").getBytes("ISO-8859-1"),"utf-8");
User user=new User();
user.setName(name);
user.setPassword(password);
Dao dao=new Dao();
boolean isLogin;
try {
isLogin=dao.logoin(user);
if(isLogin){
response.sendRedirect("sucessful.jsp");
}else{
response.sendRedirect("denglu.jsp");
}
} catch (SQLException e) {
e.printStackTrace();
}
}
}
展开全部
这个。。。。很简单的,新手动动脑子也可以做的
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
挺简单的,你是新手么,,
更多追问追答
追问
是啊,能帮我写下么。。
追答
现在还没下班。。。。
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询