<%@ 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"> --> </head> <body> <form name="form1" method="post" action="Copy of do.jsp"> 用户:<input type="text" name="username" id="username" /> <br> 密码:<input type="password" name="password" id="password" /> <br> <input type="submit" value="注册" /> <input type="submit" value="登录" /> </form> </body> </html>
<%@ page import="dao.StuDao"%> <%@ page import="dao.Stu"%> <%@ page contentType="text/html;charset=UTF-8" language="java"%> <html> <head> <title>Title</title> </head> <body> <% Stu s = new Stu(); String username = request.getParameter("username"); String password = request.getParameter("password"); s.setUname(username); s.setUpwd(password); StuDao dao = new StuDao(); boolean flag = dao.StuRegister(s); if (flag) { HttpSession session1 = request.getSession(); session1.setAttribute("username", username); session1.setAttribute("password", password); request.getRequestDispatcher("Copy of ok.jsp").forward(request, response); } else { response.sendRedirect("Copy of false.jsp"); } %> </body> </html>
<%@ page contentType="text/html;charset=UTF-8" language="java" %> <html> <head> <title></title> </head> <body> 用户或密码错误,请重新进行输入!!! </body> </html>
<%@ page contentType="text/html;charset=UTF-8" language="java" %> <html> <head> <title>Title</title> </head> <body> 您已成功登录!注册信息:用户名:<%=request.getSession().getAttribute("username")%> 密码:<%=request.getSession().getAttribute("password")%> </body> </html>
package dao; public class Stu { private String uname; private String upwd; public String getUname() { return uname; } public void setUname(String uname) { this.uname = uname; } public String getUpwd() { return upwd; } public void setUpwd(String upwd) { this.upwd = upwd; } public Stu(int sid, String uname, String upwd) { super(); this.uname = uname; this.upwd = upwd; } public Stu() { super(); } }
package dao; import dao.Stu; import java.sql.Connection; import java.sql.DriverManager; import java.sql.PreparedStatement; import java.sql.ResultSet; public class StuDao { Stu s = new Stu(); // 获取连接 protected static Connection getConnection() throws Exception { Class.forName("com.mysql.jdbc.Driver"); // 建立连接 Connection conn = DriverManager.getConnection( "jdbc:mysql://localhost:3306/mysql", "root", "root"); return conn; } // 关闭连接 public static void closeAll(Connection conn, PreparedStatement ps, ResultSet rs) throws Exception { if (rs != null) rs.close(); if (ps != null) ps.close(); if (conn != null) conn.close(); } public int StuAdd(Stu s) throws Exception { int i = 0; Connection conn = getConnection(); String sql = "insert into stu(uname,upwd) values(?,?)"; PreparedStatement ps = conn.prepareStatement(sql); ps.setString(1, s.getUname()); ps.setString(2, s.getUpwd()); return i = ps.executeUpdate(); } public boolean StuRegister(Stu s) throws Exception { int flag = 0; Connection conn = getConnection(); String sql = "select * from stu where uname = ? and upwd = ?"; PreparedStatement ps = conn.prepareStatement(sql); ps.setString(1, s.getUname()); ps.setString(2, s.getUpwd()); ResultSet rs = ps.executeQuery(); if(rs.next()){ return true; }else{ return false; } } }