登录、注册页面及后台代码

一.登录页面及后台代码

1.登录页面如图1所示

首先进行身份选择,由“管理员”和“用户”两种身份进行选择,选择不同的身份,程序会进入不同的数据表检索登录信息;当用户名或密码为空时会提示;当用户名或密码在数据表中没有检索到时,会提示登录失败;

有一些细节优化,在程序中有所体现:

(1)由“管理员”和“用户”两种身份进行选择,选择不同的身份,程序会进入不同的数据表检索登录信息;

(2)管理员和用户两种身份登录,会跳转到不同的页面;//Response.Redirect("AdminPage.aspx");

(3)使用session暂存用户名信息,实现不同页面之间的信息共享,在另一个页面直接应用session即可;//Session["LoginName"] = TextBox1.Text;



图1


2.后台代码如下:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Data;

namespace WebApplication1
{
    public partial class WebForm1 : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            //连接数据库
            if (DropDownList1.Text == "管理员")
            {

                SqlConnection con = new SqlConnection("Data Source=PC-PC;Initial Catalog=TunnelMonitor;Integrated Security=True");
                con.Open();
                //定义字符串sql,其含义为从数据表中查找列LoginName中TextBox1.Text的记录,列Password中TextBox2.Text的记录
                string sql = "select * from AdminInfo where LoginName=  '" + TextBox1.Text + "'   and Password=   '" + TextBox2.Text + "'  ";
                //定义数据适配器da,将da的数据填充至Dataset类的对象dt中
                DataTable dt = new DataTable();
                SqlDataAdapter da = new SqlDataAdapter(sql, con);
                da.Fill(dt);
                //如果记录为TextBox1.Text和TextBox2.Text的行已经存在
                if (dt.Rows.Count > 0)
                {
                    //将TextBox1.Text的信息暂存
                    Session["LoginName"] = TextBox1.Text;
                    string count = Session["LoginName"].ToString();
                    //如果以管理员账号进,就转到AdminPage.aspx     
                    Response.Redirect("AdminPage.aspx");
                }
                //用户输入的内容不存在于数据库中
                else
                {
                    Label1.Text = "您输入的用户名或密码错误,登录失败!";
                }
                con.Close();
            }

            if (DropDownList1.Text == "用户")
            {
                SqlConnection con = new SqlConnection("Data Source=PC-PC;Initial Catalog=TunnelMonitor;Integrated Security=True");
                con.Open();
                //定义字符串sql,其含义为从数据表中查找列LoginName中TextBox1.Text的记录,列Password中TextBox2.Text的记录
                string sql = "select * from UserInfo where UserName=  '" + TextBox1.Text + "'   and Password=   '" + TextBox2.Text + "'  ";
                //定义数据适配器da,将da的数据填充至Dataset类的对象dt中
                DataTable dt = new DataTable();
                SqlDataAdapter da = new SqlDataAdapter(sql, con);
                da.Fill(dt);
                //如果记录为TextBox1.Text和TextBox2.Text的行已经存在
                if (dt.Rows.Count > 0)
                {
                    //将TextBox1.Text的信息暂存
                    Session["UserName"] = TextBox1.Text;
                    string count = Session["UserName"].ToString();
                    //如果不是管理员账号,即转入
                    Response.Redirect("UserPage.aspx");

                }
                //用户输入的内容不存在于数据库中
                else
                {
                    Label1.Text = "您输入的用户名或密码错误,登录失败!";
                }
                con.Close();
            }
            
        }

        protected void Button2_Click(object sender, EventArgs e)
        {
            TextBox1.Text = "";
            TextBox2.Text = "";
            Label1.Text = "";
        }

        protected void Button3_Click(object sender, EventArgs e)
        {
            Response.Redirect("Register.aspx");
        }
    }
}
二.登录页面及后台代码

1.注册页面如图2所示:

(1)输入用户名时,连接数据库验证用户名是否已经存在,若存在会提示信息

(2)验证输入是否为空,使用RequiredFieldValidator控件;验证输入是否为邮箱格式,使用RegularExpressionValidator控件


图2

2.程序后台代码如下:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Data;
using System.Windows.Forms;

namespace WebApplication1
{
    public partial class Register1 : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }
          //验证用户名是否存在
        private bool CheckUser()
        {
            //连接数据库
            SqlConnection con = new SqlConnection("Data Source=PC-PC;Initial Catalog=TunnelMonitor;Integrated Security=True");
            con.Open();
            //定义字符串sql,其含义为从数据表中查找列UserName中TextBox1.Text的记录
            string sql = "select * from UserInfo where UserName=  '" + TextBox11.Text + "'   ";
            //定义数据适配器da,将da的数据填充至Dataset类的对象dt中
            DataTable dt = new DataTable();
            SqlDataAdapter da = new SqlDataAdapter(sql, con);
            da.Fill(dt);
            //如果记录为TextBox1.Text的行已经存在
            if (dt.Rows.Count > 0)
            {
                Label11.Text = " 该用户名已存在";
                return false;
            }
            else
            {
                return true;
            }
            con.Close();
        }
        //注册按钮
        protected void Button34_Click(object sender, EventArgs e)
        {
            //检查用户名是否已经存在
            if (CheckUser() == false)
            {
                return;
            }
            //用户名未注册
            SqlConnection con = new SqlConnection("Data Source=PC-PC;Initial Catalog=TunnelMonitor;Integrated Security=True");
            con.Open();
            SqlCommand cmd = new SqlCommand("", con);
            //在数据表中插入用户输入的注册信息
            cmd.CommandText = "INSERT INTO UserInfo(UserName,Password,Email)VALUES('" + TextBox11.Text.Trim() + "','" + TextBox22.Text.Trim() + "','" + TextBox4.Text.Trim() + "')";
            cmd.ExecuteNonQuery();
            con.Close();
            //MessageBox.Show("注册成功,请点击“确定”返回登录界面!", "温馨提示", MessageBoxButtons.YesNo);
            Response.Redirect("Login.aspx");
        }
        //重置按钮
        protected void Button35_Click(object sender, EventArgs e)
        {
            TextBox11.Text = "";
            TextBox22.Text = "";
            TextBox3.Text = "";
            TextBox4.Text = "";
        }
        //返回按钮
        protected void Button36_Click(object sender, EventArgs e)
        {
            Response.Redirect("Login.aspx");
        }

    
       
    }
}


### 回答1: 以下是使用 JavaWeb 实现登录注册互换页面的后台代码: 首先,我们需要创建一个名为 "LoginServlet" 的 Servlet,用于处理用户登录: ```java import java.io.*; import javax.servlet.*; import javax.servlet.http.*; public class LoginServlet extends HttpServlet { public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setContentType("text/html;charset=UTF-8"); PrintWriter out = response.getWriter(); String username = request.getParameter("username"); String password = request.getParameter("password"); // 检查用户名和密码是否正确 if (username.equals("admin") && password.equals("admin123")) { HttpSession session = request.getSession(); session.setAttribute("username", username); response.sendRedirect("welcome.jsp"); } else { out.println("用户名或密码错误!"); } out.close(); } } ``` 在这个 Servlet 中,我们首先获取用户提交的用户名和密码,然后检查它们是否正确。如果正确,我们将用户名保存到会话中,然后重定向到欢迎页面;否则,我们输出一个错误消息。 接下来,我们需要创建一个名为 "RegisterServlet" 的 Servlet,用于处理用户注册: ```java import java.io.*; import javax.servlet.*; import javax.servlet.http.*; public class RegisterServlet extends HttpServlet { public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setContentType("text/html;charset=UTF-8"); PrintWriter out = response.getWriter(); String username = request.getParameter("username"); String password = request.getParameter("password"); String repassword = request.getParameter("repassword"); // 检查密码和确认密码是否一致 if (!password.equals(repassword)) { out.println("密码和确认密码不一致!"); out.close(); return; } // 在此处添加将用户名和密码保存到数据库的代码 out.println("注册成功!"); out.close(); } } ``` 在这个 Servlet 中,我们首先获取用户提交的用户名、密码和确认密码,然后检查密码和确认密码是否一致。如果不一致,我们输出一个错误消息并返回;否则,我们将用户名和密码保存到数据库中(此处省略),然后输出一个成功消息。 最后,我们需要创建一个名为 "welcome.jsp" 的 JSP 页面,用于欢迎用户: ```jsp <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>欢迎页面</title> </head> <body> <h1>欢迎,<%= session.getAttribute("username") %>!</h1> <a href="logout.jsp">退出登录</a> </body> </html> ``` 在这个 JSP 页面中,我们使用了 JSP 的内置对象 "session" 来获取保存在会话中的用户名,并输出一个欢迎消息。同时,我们还提供了一个退出登录的链接,用于让用户注销并返回登录页面。 现在,我们已经完成了登录注册互换页面的后台代码。需要注意的是,我们还需要创建一个名为 "logout.jsp" 的 JSP 页面,用于处理用户注销的请求,并在其中调用 "session.invalidate()" 方法来销毁会话。 ### 回答2: 使用JavaWeb实现登录注册互换页面的后台代码需要以下步骤: 1. 创建两个JSP页面,一个用于登录,一个用于注册,分别命名为login.jsp和register.jsp,并编写相应的表单。 2. 创建一个Servlet类,命名为LoginRegisterServlet,继承HttpServlet。 3. 在LoginRegisterServlet中重写doGet方法和doPost方法。 4. 在doGet方法中,获取请求的参数,判断参数中是否包含login字段,如果包含则跳转到login.jsp页面;如果不包含则跳转到register.jsp页面。 5. 在doPost方法中,获取请求的参数,判断参数中是否包含login字段,如果包含则执行登录的逻辑,验证用户信息是否正确;如果不包含则执行注册的逻辑,将用户信息保存到数据库中。 6. 在登录逻辑中,可以使用数据库查询验证用户名和密码是否正确,如果正确则登录成功,跳转到登录成功页面;如果不正确则返回登录页面,并显示相应的错误信息。 7. 在注册逻辑中,可以使用数据库插入操作将用户信息保存到数据库中,如果插入成功则注册成功,跳转到注册成功页面;如果插入失败则返回注册页面,并显示相应的错误信息。 8. 在doGet和doPost方法中,通过request.getRequestDispatcher()方法获取跳转的页面,并使用request.forward()方法进行页面跳转。 9. 在web.xml文件中配置LoginRegisterServlet的映射路径。 以上是使用JavaWeb实现登录注册互换页面的后台代码的基本步骤,具体的实现细节根据具体的业务需求可能会有所不同。 ### 回答3: 使用JavaWeb实现登录注册页面互换的后台代码可以通过Servlet和JSP来完成。下面是一个简单的示例代码: 1. 创建一个LoginServlet来处理登录请求: ``` public class LoginServlet extends HttpServlet { protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // 获取用户输入的用户名和密码 String username = request.getParameter("username"); String password = request.getParameter("password"); // 通过数据库或其他方式进行用户验证 boolean isValidUser = validateUser(username, password); if (isValidUser) { // 用户验证通过,将用户信息存储到Session中 request.getSession().setAttribute("username", username); // 跳转到主页 response.sendRedirect("home.jsp"); } else { // 用户验证失败,返回登录页面并显示错误信息 request.setAttribute("error", "用户名或密码错误"); request.getRequestDispatcher("login.jsp").forward(request, response); } } private boolean validateUser(String username, String password) { // 在这里进行用户验证 // 返回true表示用户验证通过,返回false表示用户验证失败 } } ``` 2. 创建一个RegisterServlet来处理注册请求: ``` public class RegisterServlet extends HttpServlet { protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // 获取用户输入的用户名和密码 String username = request.getParameter("username"); String password = request.getParameter("password"); // 在这里进行注册逻辑,如将用户信息插入数据库等 // 注册成功后,将用户信息存储到Session中 request.getSession().setAttribute("username", username); // 跳转到主页 response.sendRedirect("home.jsp"); } } ``` 3. 在login.jsp页面中,用户输入用户名和密码后,将表单提交到LoginServlet: ``` <form action="login" method="post"> <input type="text" name="username" placeholder="请输入用户名" required><br> <input type="password" name="password" placeholder="请输入密码" required><br> <input type="submit" value="登录"> </form> ``` 4. 在register.jsp页面中,用户输入用户名和密码后,将表单提交到RegisterServlet: ``` <form action="register" method="post"> <input type="text" name="username" placeholder="请输入用户名" required><br> <input type="password" name="password" placeholder="请输入密码" required><br> <input type="submit" value="注册"> </form> ``` 通过以上代码,实现了登录注册页面之间的互相切换,用户输入用户名和密码后,可以登录注册,并跳转到主页。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值