用户注册和登录实例

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"%>

</span><span style="color:#FFFFFF;">学生管理系统

         账号:

         密码:

         注册" />

         重置" />

         登录" />

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

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) {

%>

        

        

        

        

<%

         } else {

%>

         账号或密码错误" />

<%

         }

%>

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"%>

Insert title here

         <%

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

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

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

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

         %>

         学员:<%=stuName%>

          学号:<%=stuCode%>

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

          E-Mail:<%=stuEmail%>

          

        

 

        


 

         <%

                   List students = new ArrayList();

                   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();

         %>

 

        

                  

                           

                                    

                                    

                                    

                                    

                                    

                                    

                                    

                           

                  

                  

                            <%

                                     for (Student student : students) {

                            %>

                           

                                    

                                    

                                    

                                    

                                    

                                    

                                    

                           

                            <%

                                     }

                            %>

 

                  

        

学号 姓名 性别 年龄 E-Mail 账号 密码
<%=student.getId()%> <%=student.getName()%> <%=student.getSex() == 1 ? "男" : "女"%> <%=student.getAge()%> <%=student.getEmail()%> <%=student.getUsername()%> <%=student.getPassword()%>

 

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

加入登录失败信息提示

         <%

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

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

         %>

        

                   <%=msg %>

        

         <%

                   }

         %>

7.制作注册页面-reg.jsp

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

         pageEncoding="UTF-8"%>

</span><span style="color:#FFFFFF;">学生注册

        

                   账号:

                   密码:

                   姓名:

                   性别:

                           

                    

                   年龄:

                   学号:

                   E-Mail

                  

                   返回" οnclick="javascript:window.location='index.jsp';"/>

                   重置" />

                   注册" />

        

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

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

你可能感兴趣的:(用户注册和登录实例)