javaweb简单的登录注册功能实现
63 阅读
0 评论
42 点赞
概述
原文:http://blog.csdn.net/qq_24473141/article/details/51363662
下面是用户登录注册流程图
登陆界面
[html]
view plain
copy
- <%@ 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">
- -->
- <script type="text/javascript">
- function change(){
- var img =document.getElementById("verify");
- img.src="VerifyCodeServlet?a="+new Date().getTime();
- }
- </script>
- </head>
- <body>
- <center>
- <div>
- <h1>欢迎登陆</h1>
- <form action="LoginServlet" method="post">
- <table>
- <tr>
- <td width="66" align="right"><font size="3">帐号:</font></td><td colspan="2"><input type="text" name="username" value="${username }" style="width:200;height:25;"/></td>
- </tr>
- <tr>
- <td align="right"><font size="3">密码:</font></td><td colspan="2"><input type="text" name="password" style="width:200;height:25;"/></td>
- </tr>
- <tr>
- <td align="right"><font size="3">验证码:</font></td>
- <td width="108" valign="middle"><input type="text" name="verifycode" style="width:100;height:25;"/></td>
- <td width="90" valign="middle"><a href="javascript:change()"><img src="VerifyCodeServlet" id="verify" border="0"></a></td>
- </tr>
- <tr><td colspan="3" align="center"><input type="submit" value="登录" style="width:130;height:30;"/></td></tr>
- </table>
- </form>
- <a href="regist.jsp"><font size="2"><i>没有帐号?点击注册</i></font></a>
- <font color="red" size="2"> ${msg }</font>
- </div>
- </center>
- </body>
- </html>
注册界面
[html]
view plain
copy
- <%@ 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 'regist.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>
- <center>
- <div>
- <h1>注册</h1>
- <form action="RegistServlet" method="post">
- 请输入帐号:<input type="text" name="username"><br/>
- 请输入密码:<input type="password" name="password"><br/>
- 请确认密码:<input type="password" name="rpsw"><br/>
- <input type="submit" value="注册">
- </form>
- <font color="red" size="2"> ${msg }</font>
- </div>
- </center>
- </body>
- </html>
[html]
view plain
copy
- <%@ 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 'welcome.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>
- <h1>这是主页</h1>
- <h2>${msg }</h2>
- </body>
- </html>
[html]
view plain
copy
- import java.io.IOException;
- import javax.servlet.ServletException;
- import javax.servlet.http.HttpServlet;
- import javax.servlet.http.HttpServletRequest;
- import javax.servlet.http.HttpServletResponse;
- import com.wzc.loginDao.UserDao;
- public class LoginServlet extends HttpServlet {
- private static final long serialVersionUID = 1L;
- public void doPost(HttpServletRequest request, HttpServletResponse response)
- throws ServletException, IOException {
- request.setCharacterEncoding("utf-8");
- response.setContentType("text/html;charset=utf-8");
- String username = request.getParameter("username");
- String password = request.getParameter("password");
- String verifyc = request.getParameter("verifycode");<span style="font-family: Arial, Helvetica, sans-serif;">//得到表单输入的内容</span>
- String svc =(String) request.getSession().getAttribute("sessionverify");
- String psw =new UserDao().findUsername(username);
- if(!svc.equalsIgnoreCase(verifyc)){
- request.setAttribute("msg", "验证码错误!");
- request.getRequestDispatcher("/index.jsp").forward(request, response);
- return;
- }
- if(psw ==null){
- request.setAttribute("msg", "没有这个用户!");
- request.getRequestDispatcher("/index.jsp").forward(request, response);
- return;
- }
- if(psw!=null&&!psw.equals(password)){
- request.setAttribute("msg", "密码错误请重新输入!");
- request.getRequestDispatcher("/index.jsp").forward(request, response);
- return;
- }
- if(psw.equals(password)){
- request.setAttribute("msg", "用户:"+username+",欢迎访问");
- request.getRequestDispatcher("/welcome.jsp").forward(request, response);
- //response.setHeader("Refresh","1;url=welcome.jsp");
- }
- }
- }
[html]
view plain
copy
- import java.io.IOException;
- import javax.servlet.ServletException;
- import javax.servlet.http.HttpServlet;
- import javax.servlet.http.HttpServletRequest;
- import javax.servlet.http.HttpServletResponse;
- import com.wzc.loginDao.UserDao;
- public class RegistServlet extends HttpServlet {
- private static final long serialVersionUID = 1L;
- public void doPost(HttpServletRequest request, HttpServletResponse response)
- throws ServletException, IOException {
- request.setCharacterEncoding("utf-8");
- response.setContentType("text/html;charset=utf-8");
- String username = request.getParameter("username");
- String password = request.getParameter("password");
- String rpsw = request.getParameter("rpsw");//得到表单输入的内容
- if(username==null||username.trim().isEmpty()){
- request.setAttribute("msg", "帐号不能为空");
- request.getRequestDispatcher("/regist.jsp").forward(request, response);
- return;
- }
- if(password==null||password.trim().isEmpty()){
- request.setAttribute("msg", "密码不能为空");
- request.getRequestDispatcher("/regist.jsp").forward(request, response);
- return;
- }
- if(!password.equals(rpsw)){
- request.setAttribute("msg", "两次输入的密码不同");
- request.getRequestDispatcher("/regist.jsp").forward(request, response);
- return;
- }
- UserDao u = new UserDao();
- u.addUser(username,password);//调用addUser()方法
- request.setAttribute("msg", "恭喜:"+username+",注册成功");
- request.getRequestDispatcher("/index.jsp").forward(request, response);
- }
- }
VerifyCodeServlet
[html]
view plain
copy
- import java.awt.image.BufferedImage;
- import java.io.IOException;
- import javax.servlet.ServletException;
- import javax.servlet.http.HttpServlet;
- import javax.servlet.http.HttpServletRequest;
- import javax.servlet.http.HttpServletResponse;
- import com.wzc.utils.VerifyCode;
- public class VerifyCodeServlet extends HttpServlet {
- private static final long serialVersionUID = 1L;
- public void doGet(HttpServletRequest request, HttpServletResponse response)
- throws ServletException, IOException {
- VerifyCode vc = new VerifyCode();
- BufferedImage image = vc.getImage(85,23);//设置验证码图片大小
- request.getSession().setAttribute("sessionverify", vc.getText());//将验证码文本保存到session域
- VerifyCode.output(image, response.getOutputStream());
- }
- }
VerifyCode
[html]
view plain
copy
- public class VerifyCode {
- public static final char[] CHARS = { '2', '3', '4', '5', '6', '7', '8',
- '9', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'J', 'K', 'L', 'M',
- 'N', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z' };
- public static Random random = new Random();
- public String getRandomString() {
- StringBuffer buffer = new StringBuffer();
- for (int i = 0; i < 4; i++) {
- buffer.append(CHARS[random.nextInt(CHARS.length)]);
- }
- return buffer.toString();
- }
- public Color getRandomColor() {
- return new Color(random.nextInt(255), random.nextInt(255), random
- .nextInt(255));
- }
- public Color getReverseColor(Color c) {
- return new Color(255 - c.getRed(), 255 - c.getGreen(), 255 - c
- .getBlue());
- }
- String text = getRandomString();
- public String getText() {
- return text;
- }
- public BufferedImage getImage(int width,int height ){
- Color color = getRandomColor();
- Color reverse = getReverseColor(color);
- BufferedImage bi = new BufferedImage(width, height,
- BufferedImage.TYPE_INT_RGB);
- Graphics2D g = bi.createGraphics();
- g.setFont(new Font(Font.SANS_SERIF, Font.BOLD, 20));
- g.setColor(color);
- g.fillRect(0, 0, width, height);
- g.setColor(reverse);
- g.drawString(text, 10, 22);
- for (int i = 0, n = random.nextInt(80); i < n; i++) {
- g.drawRect(random.nextInt(width), random.nextInt(height), 1, 1);
- }
- return bi;
- }
- public static void output(BufferedImage image, OutputStream out) throws IOException{
- ImageIO.write(image, "JPEG", out);
- }
- }
UserDao
[html]
view plain
copy
- import java.sql.Connection;
- import java.sql.DriverManager;
- import java.sql.PreparedStatement;
- import java.sql.ResultSet;
- import java.sql.SQLException;
- public class UserDao {
- public String findUsername(String username){
- String psw = null;
- Connection con =null;
- PreparedStatement pstmt =null;
- ResultSet rs = null;
- try {
- String driver ="com.mysql.jdbc.Driver";
- String url ="jdbc:mysql://localhost:3306/zhuce";
- String user ="root";
- String password ="root";<span style="font-family: Arial, Helvetica, sans-serif;">//改为自己的用户名密码和数据库名</span>
- Class.forName(driver);
- con = DriverManager.getConnection(url, user, password);
- String sql = "select * from zc where name=?";
- pstmt = con.prepareStatement(sql);
- pstmt.setString(1, username);
- rs = pstmt.executeQuery();
- if(rs==null){
- return null;
- }
- if(rs.next()){
- psw=rs.getString("password");
- }else{
- psw=null;
- }
- } catch (ClassNotFoundException e) {
- e.printStackTrace();
- } catch (SQLException e) {
- e.printStackTrace();
- }finally {
- try {
- if(pstmt!=null)pstmt.close();
- if(con!=null)con.close();
- }
- catch (SQLException e) {
- }
- }
- return psw;
- }
- public void addUser(String username,String psw){
- Connection con =null;
- PreparedStatement pstmt =null;
- try {
- String driver ="com.mysql.jdbc.Driver";
- String url ="jdbc:mysql://localhost:3306/zhuce";
- String user ="root";
- String password ="root";//改为自己的用户名密码和数据库名
- Class.forName(driver);
- con = DriverManager.getConnection(url, user, password);
- String sql = "INSERT INTO ZC VALUES(?,?)";
- pstmt = con.prepareStatement(sql);
- pstmt.setString(1, username);
- pstmt.setString(2, psw);
- pstmt.executeUpdate();
- } catch (ClassNotFoundException e) {
- e.printStackTrace();
- } catch (SQLException e) {
- e.printStackTrace();
- }finally {
- try {
- if(pstmt!=null)pstmt.close();
- if(con!=null)con.close();
- }
- catch (SQLException e) {
- }
- }
- }
- //单独测试使用
- //public static void main(String[] args) {
- //String psw =new UserDao().findUsername("345");
- //System.out.println(psw);
- //UserDao u = new UserDao();
- //u.addUser("345", "345");
- //}
- }
创建的数据库的user表有两个属性name,password 类型为varchar
点击下载源码
最后
以上就是忧心草莓为你收集整理的javaweb简单的登录注册功能实现的全部内容,希望文章能够帮你解决javaweb简单的登录注册功能实现所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
- 本文分类:java
- 浏览次数:63 次浏览
- 发布日期:2024-06-18 04:05:02
- 本文链接:https://www.kaopuke.com/article/k-p-k_13_u_7_o_22_f0_14_j_2_x.html
发表评论 取消回复