JSP第七周作业
用户表: uid (主键,自动增长) uname upwd
使用分层实现注册。(必做)
使用分层实现登录。(选做)
index.jsp
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> </head> <body bgcolor=pink> <form name="form1" method="post" action="control.jsp" > <table> <tr> <td>编号:</td> <td> <input type="text" name="uid" id="userName" ></td> </tr> <tr> <td>用户名:</td> <td> <input type="text" name="uname" id="userName" ></td> </tr> <tr> <td>密码:</td> <td><input type="password" name="upwd" id="pwd"></td> </tr> <tr> <td colspan="2"><input type="submit" value="注册"> <input type="submit" value="登录"></td> </tr> </table> </form> </body> </html>
control.jsp
<%@page import="com.gd.dao.StuDao"%> <%@page import="com.gd.bean.Stu"%> <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> <% Stu s = new Stu(); int id = Integer.parseInt(request.getParameter("uid")); s.setUid(id); String uname = request.getParameter("uname"); s.setUname(uname); String upwd = request.getParameter("upwd"); s.setUpwd(upwd); StuDao sd=new StuDao(); if(sd.addStu(s)>0){ //跳转注册成功页面 }else{ //错误页面 } %> <% if(uname.equals("ssh")&&upwd.equals("123")){ //跳转成功登录页面 request.getRequestDispatcher("zccg.jsp").forward(request, response); session.setAttribute("uname",uname); } else{ //错误页面 request.getRequestDispatcher("index.jsp").forward(request, response); } %>
zccg.jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" %> <% String uname = (String) request.getSession().getAttribute("uname"); %> <html> <head> </head> <body bgcolor=pink> <h1><%=uname%>登录成功!</h1> </body> </html>
stu.java
package com.gd.bean; public class Stu { private int uid; private String uname; private String upwd; //访问器 public int getUid() { return uid; } public void setUid(int uid) { this.uid = uid; } 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 uid, String uname, String upwd) { super(); this.uid = uid; this.uname = uname; this.upwd = upwd; } public Stu() { super(); } }
studao.java
package com.gd.dao; import java.sql.Connection; import java.sql.DriverManager; import java.sql.PreparedStatement; import com.gd.bean.Stu; public class StuDao { public int addStu(Stu s){ int i=0; try { //加载驱动 Class.forName("com.mysql.jdbc.Driver"); //建立连接 Connection con=DriverManager.getConnection("jdbc:mysql://localhost:3306/mysql?useUnicode=true&characterEncoding=utf8", "root", "root"); //写SQL语句 String sql="insert into stu values(?,?,?)"; //执行 PreparedStatement ps=con.prepareStatement(sql); ps.setInt(1, s.getUid()); ps.setString(2, s.getUname()); ps.setString(3, s.getUpwd()); i=ps.executeUpdate(); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } return i; } public int selectStu(Stu s){ int n=0; try{ Class.forName("com.mysql.jdbc.Driver"); Connection con=DriverManager.getConnection("jdbc:mysql://localhost:3306/mysql?useUnicode=true&characterEncoding=utf8", "root", "root"); String sql="select uname,upwd from stu where uname=?and upwd=?"; PreparedStatement ps=con.prepareStatement(sql); ps.setInt(1, s.getUid()); ps.setString(2, s.getUname()); ps.setString(3, s.getUpwd()); n=ps.executeUpdate(); } catch (Exception e) { e.printStackTrace(); } return n; } }