用户注册和登录实例

1.  Eclipse开发环境的基础配置

配置步骤

1、修改Eclipse的基础编码、包括

Content Type中的Java Class File

WorkSpace中的 Text file Encoding

JSP文件的Encoding

2、创建Server/Tomcat

3、创建动态Web项目

注意在最后一步勾选创建web.xml文件

2. 创建数据表

数据表SQL

DROP TABLE IF EXISTS `t_account`;

CREATE TABLE `t_account` (

  `id` int(11) NOT NULL,

  `account` varchar(255) DEFAULT NULL,

  `password` varchar(255) DEFAULT NULL,

  `type` int(11) DEFAULT NULL,

  `stu_id` int(11) DEFAULT NULL,

  PRIMARY KEY (`id`)

) ENGINE=InnoDB DEFAULT CHARSET=utf8;

 

-- ----------------------------

-- Table structure for t_stu_info

-- ----------------------------

DROP TABLE IF EXISTS `t_stu_info`;

CREATE TABLE `t_stu_info` (

  `id` int(11) NOT NULL AUTO_INCREMENT,

  `name` varchar(255) DEFAULT NULL,

  `code` varchar(255) DEFAULT NULL,

  `age` int(11) DEFAULT NULL,

  `sex` int(11) DEFAULT NULL,

  `email` varchar(255) DEFAULT NULL,

  `qq` varchar(255) DEFAULT NULL,

  `idcard` varchar(255) DEFAULT NULL,

  PRIMARY KEY (`id`)

) ENGINE=InnoDB DEFAULT CHARSET=utf8;

3.制作登录页面-index.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>

<form action="">

         账号:<input type="text" name="account" /> <br/>

         密码:<input type="password" name="password" /> <br/>

         <input type="button" name="reg" value="注册" />

         <input type="reset" name="reset" value="重置" />

         <input type="submit" name="submit" value="登录" />

</form>

</body>

</html>

项目部署到Tomcat容器中,启动试运行。修改项目的path

<Context docBase="StudentManager" path="/sm" reloadable="true" source="org.eclipse.jst.jee.server:StudentManager"/></Host>

4.制作用户登录账号信息验证-checkLogin.jsp

把JDBC驱动程序拷贝到WEB-INF/lib目录中

在WEB-INF目录中有两个特殊的目录,lib和classes

lib:放置的是引用的第三方的jar文件

classes:放置的是编译编程的class文件

<%@page import="java.sql.ResultSet"%>

<%@page import="com.sun.corba.se.spi.orbutil.fsm.Guard.Result"%>

<%@page import="java.sql.PreparedStatement"%>

<%@page import="java.sql.DriverManager"%>

<%@page import="java.sql.Connection"%>

<%@ page language="java" contentType="text/html; charset=UTF-8"

         pageEncoding="UTF-8"%>

<%

         request.setCharacterEncoding("UTF-8");

 

         String username = request.getParameter("username");

         String password = request.getParameter("password");

 

         Integer id = null;

         String name = "";

         String code = "";

         Integer sex = null;

         String email = "";

 

         Class.forName("com.mysql.jdbc.Driver");

         Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/student?characterEncoding=UTF-8",

                            "root", "");

         PreparedStatement pst = conn.prepareStatement(

                            "select a.id, s.name,s.code,s.sex,s.email from t_account a left join t_stu_info s on a.stu_id = s.id where a.username = ? and a.password = ?");

         pst.setString(1, username);

         pst.setString(2, password);

         ResultSet rs = pst.executeQuery();

         while (rs.next()) {

                   id = rs.getInt("a.id");

                   name = rs.getString("s.name");

                   code = rs.getString("s.code");

                   sex = rs.getInt("s.sex");

                   email = rs.getString("s.email");

         }

         pst.close();

         conn.close();

 

         if (null != id) {

%>

<jsp:forward page="success.jsp">

         <jsp:param name="stuName" value="<%=name%>" />

         <jsp:param name="stuCode" value="<%=code%>" />

         <jsp:param name="stuSex" value="<%=sex%>" />

         <jsp:param name="stuEmail" value="<%=email%>" />

</jsp:forward>

<%

         } else {

%>

<jsp:forward page="index.jsp">

         <jsp:param name="msg" value="账号或密码错误" />

</jsp:forward>

<%

         }

%>

5.制作登录成功页面-success.jsp

如果登录成功,显示登录账号信息和用户信息列表

<%@page import="com.ntqn.Student"%>

<%@page import="java.util.*"%>

<%@page import="java.sql.*"%>

<%@ 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>Insert title here</title>

<!-- CSS goes in the document HEAD or added to your external stylesheet -->

<style type="text/css">

table.gridtable {

         font-family: verdana, arial, sans-serif;

         font-size: 11px;

         color: #333333;

         border-width: 1px;

         border-color: #666666;

         border-collapse: collapse;

}

 

table.gridtable th {

         border-width: 1px;

         padding: 8px;

         border-style: solid;

         border-color: #666666;

         background-color: #dedede;

}

 

table.gridtable td {

         border-width: 1px;

         padding: 8px;

         border-style: solid;

         border-color: #666666;

         background-color: #ffffff;

}

</style>

</head>

<body>

         <%

                   String stuName = request.getParameter("stuName");

                   String stuCode = request.getParameter("stuCode");

                   String stuSex = request.getParameter("stuSex");

                   String stuEmail = request.getParameter("stuEmail");

         %>

         学员:<%=stuName%>

         &nbsp;学号:<%=stuCode%>

         &nbsp;性别:<%=stuSex.equals("1") ? "男" : "女"%>

         &nbsp;E-Mail:<%=stuEmail%>

         &nbsp;

         <br />

 

         <hr />

 

         <%

                   List<Student> students = new ArrayList<Student>();

                   Class.forName("com.mysql.jdbc.Driver");

                   Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/student?characterEncoding=UTF-8",

                                     "root", "");

                   PreparedStatement pst = conn.prepareStatement(

                                     "select a.username,a.password,a.id, s.name,s.code,s.sex,s.email,s.age from t_account a left join t_stu_info s on a.stu_id = s.id");

                   ResultSet rs = pst.executeQuery();

                   while (rs.next()) {

                            Integer id = rs.getInt("a.id");

                            String name = rs.getString("s.name");

                            String code = rs.getString("s.code");

                            Integer sex = rs.getInt("s.sex");

                            String email = rs.getString("s.email");

                            String username = rs.getString("a.username");

                            String password = rs.getString("a.password");

                            Integer age = rs.getInt("s.age");

                            Student stu = new Student(id, username, password, name, code, age, email, sex);

                            students.add(stu);

                   }

                   pst.close();

                   conn.close();

         %>

 

         <table class="gridtable">

                   <thead>

                            <tr>

                                     <th>学号</th>

                                     <th>姓名</th>

                                     <th>性别</th>

                                     <th>年龄</th>

                                     <th>E-Mail</th>

                                     <th>账号</th>

                                     <th>密码</th>

                            </tr>

                   </thead>

                   <tbody>

                            <%

                                     for (Student student : students) {

                            %>

                            <tr>

                                     <td><%=student.getId()%></td>

                                     <td><%=student.getName()%></td>

                                     <td><%=student.getSex() == 1 ? "男" : "女"%></td>

                                     <td><%=student.getAge()%></td>

                                     <td><%=student.getEmail()%></td>

                                     <td><%=student.getUsername()%></td>

                                     <td><%=student.getPassword()%></td>

                            </tr>

                            <%

                                     }

                            %>

 

                   </tbody>

         </table>

 

</body>

</html>

6.修改登录页面-index.jsp

加入登录失败信息提示

         <%

                   String msg = request.getParameter("msg");

                   if (null != msg && !msg.equals("")) {

         %>

         <div style="color:red">

                   <%=msg %>

         </div>

         <%

                   }

         %>

7.制作注册页面-reg.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>

         <form action="checkReg.jsp" method="post">

                   账号:<input type="text" name="username" value="nitianyu"/> <br />

                   密码:<input type="text" name="password" value="123456"/> <br />

                   姓名:<input type="text" name="name" value="倪天宇"/> <br />

                   性别:

                            <select name="sex">

                                     <option value="1">男</option>

                                     <option value="2" selected="selected">女</option>

                            </select>

                    <br />

                   年龄:<input type="text" name="age" value="22"/> <br />

                   学号:<input type="text" name="code" value="40"/> <br />

                   E-Mail:<input type="text" name="email" value="nty@qq.com"/> <br />

                  

                   <input type="button" name="reg" value="返回" οnclick="javascript:window.location='index.jsp';"/>

                   <input type="reset" name="reset" value="重置" />

                   <input type="submit" name="submit" value="注册" />

         </form>

</body>

</html>

8.制作注册信息写入服务-checkReg.jsp

<%@page import="java.sql.ResultSet"%>

<%@page import="java.sql.PreparedStatement"%>

<%@page import="java.sql.DriverManager"%>

<%@page import="java.sql.Connection"%>

<%@ page language="java" contentType="text/html; charset=UTF-8"

         pageEncoding="UTF-8"%>

<%

         request.setCharacterEncoding("UTF-8");

 

         String username = request.getParameter("username");

         String password = request.getParameter("password");

         String name = request.getParameter("name");

         String code = request.getParameter("code");

         String age = request.getParameter("age");

         String sex = request.getParameter("sex");

         String email = request.getParameter("email");

        

 

         Class.forName("com.mysql.jdbc.Driver");

         Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/student?characterEncoding=UTF-8",

                            "root", "");

         PreparedStatement pst = conn.prepareStatement(

                            "insert into t_stu_info(name, code, sex, age, email) values(?,?,?,?,?)");

         pst.setString(1, name);

         pst.setString(2, code);

         pst.setInt(3, new Integer(sex));

         pst.setInt(4, new Integer(age));

         pst.setString(5, email);

         int i = pst.executeUpdate();

         pst.close();

        

         if (i == 1) {

                   pst = conn.prepareStatement(

                                     "insert into t_account(username, password, type, stu_id) values(?,?,1,?)");

                   pst.setString(1, username);

                   pst.setString(2, password);

                   pst.setInt(3, 1);

                   pst.executeUpdate();

                   pst.close();

         }

        

         conn.close();

%>

success

 

9.制作注册信息成功页面-regSuccess.jsp

regSuccess<br/>

<a herf="index.jsp" target="_self">返回登录</a>

  • 2
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值