第七周作业
index.jsp
1 <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> 2 <% 3 String path = request.getContextPath(); 4 String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; 5 %> 6 7 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> 8 <html> 9 <head> 10 <base href="<%=basePath%>"> 11 12 <title>My JSP 'index.jsp' starting page</title> 13 <meta http-equiv="pragma" content="no-cache"> 14 <meta http-equiv="cache-control" content="no-cache"> 15 <meta http-equiv="expires" content="0"> 16 <meta http-equiv="keywords" content="keyword1,keyword2,keyword3"> 17 <meta http-equiv="description" content="This is my page"> 18 <!-- 19 <link rel="stylesheet" type="text/css" href="styles.css"> 20 --> 21 </head> 22 23 <body> 24 <form name="form1" method="post" action="control.jsp" > 25 <table> 26 <tr> 27 <td>编号:</td> 28 <td> <input type="text" name="sid" id="userName" ></td> 29 </tr> 30 <tr> 31 <td>用户名:</td> 32 <td> <input type="text" name="uname" id="userName" ></td> 33 </tr> 34 <tr> 35 <td>输入登录密码:</td> 36 <td><input type="password" name="upwd" id="pwd"></td> 37 </tr> 38 39 <tr> 40 <td colspan="2"><input type="submit" value="注册"></td> 41 </tr> 42 </table> 43 </form> 44 </body> 45 </html>
control.jsp
1 <%@page import="com.gb.gao.StuDao"%> 2 <%@page import="com.gb.gao.Stu"%> 3 <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> 4 <% 5 Stu s = new Stu(); 6 7 String uname = request.getParameter("uname"); 8 s.setUname(uname); 9 String upwd = request.getParameter("upwd"); 10 s.setUpwd(upwd); 11 12 StuDao sd=new StuDao(); 13 if(sd.addStu(s)>0){ 14 //跳转注册成功页面 15 }else{ 16 //错误页面 17 } 18 19 20 21 %> 22 <% 23 if(uname.equals("zs")&&upwd.equals("123")){ 24 //跳转成功登录页面 25 request.getRequestDispatcher("ok.jsp").forward(request, response); 26 session.setAttribute("uname",uname); 27 } 28 else{ 29 //错误页面 30 request.getRequestDispatcher("index.jsp").forward(request, response); 31 } 32 %>
Stu.java
1 package com.gb.gao; 2 3 public class Stu { 4 private int uid; 5 private String uname; 6 private String upwd; 7 //访问器 8 public int getUid() { 9 return uid; 10 } 11 public void setUid(int uid) { 12 this.uid = uid; 13 } 14 public String getUname() { 15 return uname; 16 } 17 public void setUname(String uname) { 18 this.uname = uname; 19 } 20 public String getUpwd() { 21 return upwd; 22 } 23 public void setUpwd(String upwd) { 24 this.upwd = upwd; 25 } 26 //构造器 27 public Stu(int uid, String uname, String upwd) { 28 super(); 29 this.uid = uid; 30 this.uname = uname; 31 this.upwd = upwd; 32 } 33 public Stu() { 34 super(); 35 } 36 37 38 }
StuDao.java
1 package com.gb.gao; 2 3 import java.sql.Connection; 4 import java.sql.DriverManager; 5 import java.sql.PreparedStatement; 6 7 import com.gb.bean.Stu; 8 9 10 11 public class StuDao { 12 //学生数据访问类 13 14 //添加学生 15 public int addStu(Stu s){ 16 int i=0; 17 18 try { 19 //加载驱动 20 Class.forName("com.mysql.jdbc.Driver"); 21 //建立连接 22 Connection con=DriverManager.getConnection("jdbc:mysql://localhost:3306/userdatabase", "root", "root"); 23 //写SQL语句 24 String sql="insert into userlist(uname,upwd) values('张四','123' 26 PreparedStatement ps=con.prepareStatement(sql); 27 ps.setString(1, s.getUname()); 28 ps.setString(2, s.getUpwd()); 29 i=ps.executeUpdate(); 30 31 32 } catch (Exception e) { 33 // TODO Auto-generated catch block 34 e.printStackTrace(); 35 } 36 return i; 37 38 39 40 41 42 43 44 }