配置步骤 |
1、修改Eclipse的基础编码、包括 Content Type中的Java Class File WorkSpace中的 Text file Encoding JSP文件的Encoding |
2、创建Server/Tomcat |
3、创建动态Web项目 注意在最后一步勾选创建web.xml文件 |
数据表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; |
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
账号: 密码: 注册" /> 重置" /> 登录" />
|
项目部署到Tomcat容器中,启动试运行。修改项目的path
|
把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 { %>
<% } %> |
如果登录成功,显示登录账号信息和用户信息列表
<%@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"%>
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; }
<% 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 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) { %> <% } %>
|
加入登录失败信息提示
<% String msg = request.getParameter("msg"); if (null != msg && !msg.equals("")) { %> <%=msg %> <% } %> |
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
账号: 密码: 姓名: 性别:
男 女
年龄: 学号: E-Mail:
返回" οnclick="javascript:window.location='index.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
|
regSuccess |