jsp+service+servlet+Oracle实现简单的数据库交互(登录注册)
2018-01-09 14:29
1091 查看
java部分的整体架构
代码部分:
1.Javabean代码:
package com.login.bean;public class Login {
private String name;
private String pwd;
private String email;
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPwd() {
return pwd;
}
public void setPwd(String pwd) {
this.pwd = pwd;
}
}
2.dao代码:
LoginDao.javapackage com.orcl.dao; import java.sql.SQLException; import java.util.ArrayList; import javax.xml.ws.Response; import org.apache.tomcat.jni.User; import com.login.bean.Login; import com.xm.util.Oboperation; public class loginDao extends Oboperation{ public Login getNamePwd(String name,String pwd,String email) { Login lg=null; getConn(); //查询语句 String sql="select * from userlogin where pwd=? and name=? or email=?"; extQuery(sql,pwd,name,email); try { //处理结果集 if(rs.next()) { lg=new Login(); lg.setName(rs.getString("name")); lg.setPwd(rs.getString("pwd")); lg.setEmail(rs.getString("email")); } } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } closeAll(); return lg; } //按name条件查询 与忘记密码联系 public Login getName(String name) { Login lg2=null; getConn(); String sql="select * from userlogin where name=? "; extQuery(sql,name); try { if(rs.next()) { lg2=new Login(); lg2.setName(rs.getString("name")); lg2.setPwd(rs.getString("pwd")); lg2.setEmail(rs.getString("email")); } } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } closeAll(); return lg2; } }
zhuceDao.java
package com.orcl.dao;
import java.sql.SQLException;
import java.util.ArrayList;
import javax.swing.JOptionPane;
import com.login.bean.Login;
import com.xm.util.Oboperation;
public class ZhuceDao extends Oboperation{
public ArrayList zhuce(String name,String pwd,String email) {
ArrayList arr=new ArrayList();
getConn();
String sql="insert into userlogin values(?,?,?)";
extUpdate(sql, name,pwd,email);
closeAll();
return arr;
}
}
wangjiDao.java
package com.orcl.dao;
import java.util.ArrayList;
import com.xm.util.Oboperation;
public class WangjiDao extends Oboperation {
public ArrayList wangji(String name,String pwd) {
ArrayList arr=new ArrayList();
getConn();
//更新密码语句
String sql="update userlogin set pwd=? where name=?";
extUpdate(sql,pwd,name);
closeAll();
return arr;
}
}
3.service
loginservice.javapackage com.xm.service;
import java.util.ArrayList;
import com.login.bean.Login;
import com.orcl.dao.loginDao;
public class LoginService {
loginDao ld=new loginDao();
// public ArrayList selLogin(String name,String pwd) {
// return ld.selLogin(name, pwd);
// }
public Login getNamePwd(String name,String pwd,String email) {
return ld.getNamePwd(name, pwd,email);
}
public Login getName(String name) {
return ld.getName(name);
}
}
zhuceservice.java
package com.xm.service; import java.util.ArrayList; import com.login.bean.Login; import com.orcl.dao.ZhuceDao; public class ZhuceService { ZhuceDao zd=new ZhuceDao(); public ArrayList zhuce(String name,String pwd,String email) { return zd.zhuce(name, pwd, email); } }
wangjiservice.java
package com.xm.service;
import java.util.ArrayList;
import com.orcl.dao.WangjiDao;
public class WangjiService {
WangjiDao wj=new WangjiDao();
public ArrayList wangji(String name,String pwd) {
return wj.wangji(name, pwd);
}
}
4.Servlet
loginServlet.javapackage com.xm.servlet; import java.io.IOException; import java.util.ArrayList; import javax.servlet.ServletException; import javax.servlet.annotation.WebServlet; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import javax.swing.JOptionPane; import org.apache.tomcat.jni.User; import com.login.bean.Login; import com.orcl.dao.loginDao; import com.xm.service.LoginService; /** * Servlet implementation class LoginServlet */ @WebServlet("/LoginServlet") public class LoginServlet extends HttpServlet { LoginService ld=new LoginService(); protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { request.setCharacterEncoding("UTF-8"); //获取数据 String uname=request.getParameter("username"); String upwd=request.getParameter("userpwd"); String uemail=request.getParameter("email"); if(uname.equals("")||upwd.equals("")) { JOptionPane.showMessageDialog(null, "用户名或密码不能为空!!"); request.getRequestDispatcher("login.jsp").forward(request, response); }else { //传递数据给LoginDao中的getNamePwd()方法 Login ln=ld.getNamePwd(uname, upwd,uemail); //判断是否查询成功 if(ln==null) { request.getRequestDispatcher("field.jsp").forward(request, response); }else { request.getRequestDispatcher("index.html").forward(request, response); } } } protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doGet(request, response); } }
zhuceServlet.java
package com.xm.servlet;
import java.io.IOException;
import java.util.ArrayList;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.swing.JOptionPane;
import com.login.bean.Login;
import com.xm.service.LoginService;
import com.xm.service.ZhuceService;
@WebServlet("/ZhuceServlet")
public class ZhuceServlet extends HttpServlet {
ZhuceService zs=new ZhuceService();
LoginService ls=new LoginService();
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
request.setCharacterEncoding("UTF-8");//设置编码格式
//获取表单数据
String uname=request.getParameter("zuser");
String upwd=request.getParameter("zpwd");
String uemail=request.getParameter("zemail");
Login ln=ls.getNamePwd(uname, upwd, uemail);
//判断用户名是否存在
if(ln!=null) {
JOptionPane.showMessageDialog(null, "用户名或邮箱已存在");
request.getRequestDispatcher("zhuce.jsp").forward(request, response);
}else {
//判断表单中的数据是否为空
if(uname.equals("")||upwd.equals("")||uemail.equals("")) {
JOptionPane.showMessageDialog(null, "用户名、密码或邮箱为空");
request.getRequestDispatcher("zhuce.jsp").forward(request, response);
}else {
//不为空将表单中的数据传给ZhuceDao()
ArrayList al=zs.zhuce(uname, upwd, uemail);
request.getRequestDispatcher("zhuceChenggong.jsp").forward(request, response);
}
}
//request.getRequestDispatcher("zhuceChengg
4000
ong.jsp").forward(request, response);
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doGet(request, response);
}
}
wangjiServlet.java
package com.xm.servlet;
import java.io.IOException;
import java.util.ArrayList;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.swing.JOptionPane;
import com.login.bean.Login;
import com.xm.service.LoginService;
import com.xm.service.WangjiService;
@WebServlet("/WangjiServlet")
public class WangjiServlet extends HttpServlet {
WangjiService ws=new WangjiService();
LoginService ls=new LoginService();
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
request.setCharacterEncoding("UTF-8");//设置编码格式
//获取表单数据
String wuser=request.getParameter("wuser");
String wrepwd=request.getParameter("wrepwd");
//将用户名传给getName()方法
Login ln=ls.getName(wuser);
//判断数据库中用户名是否存才
if(ln!=null) {
//将表单中数据传给wangji()方法
ArrayList al=ws.wangji(wuser, wrepwd);
if(wuser.equals("")||wrepwd.equals("")) {
JOptionPane.showMessageDialog(null, "用户名或密码不可为空");
request.getRequestDispatcher("wangji.jsp").forward(request, response);
}else {
if(al!=null) {
JOptionPane.showMessageDialog(null, "修改成功");
request.getRequestDispatcher("wangji.jsp").forward(request, response);
}else {
JOptionPane.showMessageDialog(null, "修改失败");
request.getRequestDispatcher("wangji.jsp").forward(request, response);
}
}
}else {
JOptionPane.showMessageDialog(null, "用户名不存在!");
request.getRequestDispatcher("wangji.jsp").forward(request, response);
}
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doGet(request, response);
}
}
5.jsp页面
login.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> <script type="text/javascript" src="js/jquery-1.11.1.js"></script> <style type="text/css"> *{ margin:0px; padding: 0px; } form{ width:298px; height:230px; margin:36px auto 0px; } label{ color: black; font-family: '宋体'; } .u{ width: 221px; height: 34px; border-radius: 5px; border: solid 1px; border-color: #00ff8a; margin-top: 5px; } .b { width: 70px; height: 30px; margin-top: 16px; margin-left: 11px; color: blue; background-color: #00edff; border-radius: 6px; } #dl{ margin-left:85px; } a{ text-decoration: none; } </style> </head> <body class="bjtp"> <img alt="" src="images/loginbj.jpg" style="width: 100% "> <form action="LoginServlet" method="post"> <label>用户名:</label><input type="text" class="u" name="username" id="user"/> <span id="sp1"></span><br> <label>密 码:</label><input type="password" class="u" name="userpwd" id="pwd"/><br> <input type="submit" value="登录" class="b"/> <a href="wangji.jsp"><input type="button" value="忘记密码" class="b"/></a> <a href="zhuce.jsp"><input type="button" value="注册" class="b" /></a> </form> </body> </html>
zhuce.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"> <script type="text/javascript" src="js/jquery-1.11.1.js"></script> <title>注册界面</title> <style type="text/css"> *{margin:0px; padding:0px; } .yh{ margin:0px auto 0px; } body{ background: url("images/zhuce.jpg") no-repeat; background-size: cover; } form{ padding-top:200px; } .inp{ width:200px; height:25px; border:1px solid; border-radius: 5px } td{ padding-top:10px; } .btn{ width: 87px; height: 30px; } a{ text-decoration: none; } span{ color:red; } </style> </head> <body> <form action="ZhuceServlet" method="post"> <h2 style="text-align: center;">用户注册</h2> <table class="yh"> <tr> <td> <label>用 户 名:</label> <input type="text" class="inp" id="user" name="zuser"/> <span id="sp1"></span> </td> </tr> <tr> <td> <label>密 码:</label> <input type="password" class="inp" id="pwd" name="zpwd"/> <span id="sp2"></span> </td> </tr> <tr> <td> <label>重输密码:</label> <input type="password" class="inp" id="repwd" name="repwd"/> <span id="sp3"></span> </td> </tr> <tr> <td> <label>邮 箱:</label> <input type="text" class="inp" id="em" name="zemail"/> <span id="sp4"></span> </td> </tr> <tr> <td align="center"><input type="submit" value="注册" class="btn" id="bt"/> <a href="login.jsp"> <input type="button" value="取消" class="btn"/></a></td> </tr> </table> </form> <script type="text/javascript"> $("#user").blur(function(){ var user=$("#user").val(); var uzz=/^\w{3,20}$/; var sp1=$("#sp1"); if(!uzz.exec(user)){ sp1.html("用户名格式错误!!"); }else{ sp1.html(""); } }); $("#pwd").blur(function(){ var p=$("#pwd").val(); var sp2=$("#sp2"); var pzz=/\w{3,15}/; if(!pzz.exec(p)){ sp2.html("密码格式错误!!!"); }else{ sp2.html(""); } }); $("#repwd").blur(function(){ var rep=$("#repwd").val(); var pp=$("#pwd").val(); var sp3 =$("#sp3"); if(rep==pp){ $("#bt").attr("disabled",true); sp3.html(""); }else{ $("#bt").attr("disabled","disabled"); sp3.html("前后不一致!"); } }); $("#em").blur(function(){ var e=$("#em").val(); var sp4=$("#sp4"); var ezz=/\w@+[a-z]{2,3}.+\w{2,3}/; if(!ezz.exec(e)){ sp4.html("邮箱格式错误!!!"); }else{ sp4.html(""); } }); </script> </body> </html>
wangji.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> <style type="text/css"> *{margin:0px; padding:0px; } body{ background: url("images/wangji.jpg") no-repeat; background-size: cover; } a{ text-decoration: none; } form{ padding-top:200px; } .tab{ margin: 0px auto 0px; } label{ color:red; } .k{ width:200px; height:25px; border:1px solid; border-radius: 5px } .btn{ width: 87px; height: 30px; margin: 10px 5px 0px 28px; } </style> </head> <body> <form action="WangjiServlet" method="post" class="" > <table class="tab"> <tr> <td> <label>请输入用户名:</label><input type="text" name="wuser" class="k"><br> </td> </tr> <tr> <td> <label>请输入新密码:</label><input type="password" name="wrepwd" class="k"><br> </td> </tr> <tr > <td> <input type="submit" value="确认修改" class="btn"> <a href="login.jsp"><input type="button" value="返回" class="btn"></a> </td> </tr> </table> </form> </body> </html>
field.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> <h2>登录失败!!你将在3秒后跳转到登录界面</h2> <% response.setHeader("refresh", "3;URL=login.jsp"); %> </body> </html>
zhucechenggong.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> <h2>你将在3秒后跳转到登录界面</h2> <% response.setHeader("refresh", "3;URL=login.jsp"); %> </body> </html>
相关文章推荐
- jsp+servlet+ajax+oracle数据库-实现简单的登陆、注册、找回密码功能(与Oralce数据库连接)
- jsp和servlet 简单登录界面(不连接数据库)
- 一个简单的jsp+servlet实例,实现简单的登录
- 简单的JSP-Servlet登录,注册,修改密码操作
- 一个简单的jsp+servlet实例,实现简单的登录
- 一个简单的jsp+servlet实例,实现简单的登录
- 在jsp中用bean和servlet联合实现用户注册、登录
- JSP+Servlet + JDBC 实现简单的登录验证模块
- JSP+Servlet实现注册登录功能
- 一个简单的jsp+servlet实例,实现简单的登录
- jsp实现的简单注册登录
- ODBC + WIN32 API 访问MYSQL 数据库实现简单QQ用户注册和登录 的代码分析
- 在jsp中用bean和servlet联合实现用户注册、登录
- JSP连接数据库实现注册登录(附带上传头像)
- 一个简单的jsp+servlet实例,实现简单的登录
- 在jsp中用bean和servlet联合实现用户注册、登录
- 用JSP和数据库操作实现的简单的用户登录
- JSP+JavaBean+Servlet技术实现某网站用户注册和登录功能
- 一个简单的jsp+servlet实例,实现简单的登录
- 在jsp中用bean和servlet联合实现用户注册、登录