Chinaunix首页 | 论坛 | 博客
  • 博客访问: 2316460
  • 博文数量: 252
  • 博客积分: 5472
  • 博客等级: 大校
  • 技术积分: 3107
  • 用 户 组: 普通用户
  • 注册时间: 2011-09-17 18:39
文章分类

全部博文(252)

文章存档

2012年(96)

2011年(156)

分类: 系统运维

2011-12-06 21:25:09

1 数据库:
  1. create database student;
  2. use student;
  3. create table user(
  4. id int primary key auto_increment,
  5. user varchar(50) not null,
  6. pwd varchar(50) not null,
  7. name varchar(50) not null,
  8. *** varchar(50) not null,
  9. age int(50) not null
  10. );

 

2 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. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
  7. <html>
  8. <head>
  9.     <base href="<%=basePath%>">
  10.     
  11.     <title>欢迎来到学生管理系统</title>
  12. <meta http-equiv="pragma" content="no-cache">
  13. <meta http-equiv="cache-control" content="no-cache">
  14. <meta http-equiv="expires" content="0">
  15. <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
  16. <meta http-equiv="description" content="This is my page">

  17. </head>

  18. <body>
  19.     <div align="center"> <font size=" 2" color="#FF6633">用户登录</font>
  20. </div>
  21. <form id="form1" name="form1" method="post" action="loginServlet">
  22. <table width="357" border="0" align="center">
  23.     <tr>
  24.       <td width="128">用户名:</td>
  25.       <td width="219"><label>
  26.         <input name="user" type="text" id="user" />
  27.       </label></td>
  28.     </tr>
  29.     <tr>
  30.       <td>密 码:</td>
  31.       <td><label>
  32.         <input name="pwd" type="password" id="pwd" />
  33.       </label></td>
  34.     </tr>
  35.     <tr>
  36.       <td><label>
  37.         <input type="submit" name="Submit" value="登录" />
  38.       </label></td>
  39.       <td><label><a href="addUser.jsp">用户注册</a></label></td>
  40.     </tr>
  41. </table>
  42. <p>&nbsp;</p>
  43. </form>
  44. </body>
  45. </html>

3 addUser.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. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
  7. <html>
  8. <head>
  9.     <base href="<%=basePath%>">
  10.     
  11.     <title>用户注册</title>
  12.     
  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. </head>

  19. <body>
  20.     <p align="center"><font size=" 3" color="#FF3300">用户注册</font></p>
  21. <form id="form1" name="form1" method="post" action="addUserServlet">
  22. <table width="340" border="0" align="center">
  23.     <tr>
  24.       <td width="141">用户名:</td>
  25.       <td width="189"><label>
  26.         <input name="user" type="text" id="user" />
  27.       </label></td>
  28.     </tr>
  29.     <tr>
  30.       <td>密码:</td>
  31.       <td><label>
  32.         <input name="pwd" type="password" id="pwd" />
  33.       </label></td>
  34.     </tr>
  35.     <tr>
  36.       <td>重输密码:</td>
  37.       <td><label>
  38.         <input name="pwd1" type="password" id="pwd1" />
  39.       </label></td>
  40.     </tr>
  41.     <tr>
  42.       <td>姓名:</td>
  43.       <td><label>
  44.         <input name="name" type="text" id="name" />
  45.       </label></td>
  46.     </tr>
  47.     <tr>
  48.       <td>性别:</td>
  49.       <td><label>
  50.         <input name="***" type="radio" value="男" checked="checked" />
  51.       男
  52.       <input type="radio" name="***" value="女" />
  53.       女</label></td>
  54.     </tr>
  55.     <tr>
  56.       <td>年龄:</td>
  57.       <td><label>
  58.         <input name="age" type="text" id="age" />
  59.       </label></td>
  60.     </tr>
  61.     <tr>
  62.       <td><label>
  63.         <input type="submit" name="Submit" value="提交" />
  64.       </label></td>
  65.       <td><label>
  66.         <input type="reset" name="Submit2" value="重置" />
  67.       </label></td>
  68.     </tr>
  69. </table>
  70. </form>
  71. </body>
  72. </html>

 

4 DAO.java  实现与数据库的连接用户名验证与用户名新建添加

 

  1. package dao;

  2. import getConnection.GetConnection;

  3. import java.sql.Connection;
  4. import java.sql.PreparedStatement;
  5. import java.sql.SQLException;

  6. import com.mysql.jdbc.ResultSet;

  7. import bean.User;

  8. public class Dao {
  9. private Connection conn;
  10. private PreparedStatement pstat;
  11. String sql="";
  12. /**
  13. *
  14. * 用户登录
  15. */
  16. public boolean logoin(User user) throws SQLException{
  17.    conn = GetConnection.getConnection();
  18.    boolean i = false;
  19.    sql = "select * from user where user=? and pwd=?";
  20.   
  21.    pstat = conn.prepareStatement(sql);
  22.   
  23.    pstat.setString(1, user.getUser());
  24.    pstat.setString(2, user.getPwd());
  25.   
  26.    ResultSet rs1 = (ResultSet) pstat.executeQuery();
  27.    if (rs1.next())
  28.    {
  29.     i = true;
  30.     rs1.close();
  31.     pstat.close();
  32.    }
  33.    else
  34.    {
  35.     i = false ;
  36.     rs1.close();
  37.     pstat.close();
  38.    }
  39.    conn.close();
  40.    return i;
  41. }
  42. /**
  43. * 用户注册
  44. */
  45. public void addUser(User user){
  46.    conn = GetConnection.getConnection();
  47.    sql = "insert into user values(?,?,?,?,?)";
  48.    try{
  49.     pstat = conn.prepareStatement(sql);
  50.     pstat.setString(1,user.getUser());
  51.     pstat.setString(2,user.getPwd());
  52.     pstat.setString(3,user.getName());
  53.     pstat.setString(4,user.get***());
  54.     pstat.setInt(5,user.getAge());
  55.     pstat.executeUpdate();
  56.     pstat.close();
  57.     conn.close();
  58.    
  59.    }catch(SQLException e){
  60.     e.printStackTrace();
  61.    }
  62.   
  63. }
  64. }

5 User.java

 

  1. package bean;

  2. public class User {
  3. private String user;
  4. private String pwd;
  5. private String name;
  6. private String ***;
  7. private int age;
  8. public String getUser() {
  9.    return user;
  10. }
  11. public void setUser(String user) {
  12.    this.user = user;
  13. }
  14. public String getPwd() {
  15.    return pwd;
  16. }
  17. public void setPwd(String pwd) {
  18.    this.pwd = pwd;
  19. }
  20. public String getName() {
  21.    return name;
  22. }
  23. public void setName(String name) {
  24.    this.name = name;
  25. }
  26. public String get***() {
  27.    return ***;
  28. }
  29. public void set***(String ***) {
  30.    this.*** = ***;
  31. }
  32. public int getAge() {
  33.    return age;
  34. }
  35. public void setAge(int age) {
  36.    this.age = age;
  37. }
  38. }

 

6 GetConnection.java  实现与数据库之间的连接

 

  1. package getConnection;

  2. import java.sql.Connection;
  3. import java.sql.DriverManager;
  4. import java.sql.SQLException;

  5. /**
  6. *
  7. * 用JDBC的方法获得数据库的连接
  8. *
  9. */
  10. public class GetConnection {
  11. //通过静态方法注册驱动,获得连接

  12. public static Connection getConnection(){
  13.    String driver = "com.mysql.jdbc.Driver";
  14.    String url = "jdbc:mysql://localhost/student";
  15.    Connection con = null;
  16.    try {
  17.     Class.forName(driver);
  18.     try {
  19.      con = DriverManager.getConnection(url,"root","");
  20.     } catch (SQLException e) {
  21.      e.printStackTrace();
  22.     }
  23.    } catch (ClassNotFoundException e) {
  24.     e.printStackTrace();
  25.    }
  26.    System.out.println("已获得数据库的连接");
  27.    return con;
  28. }
  29. /*public static void main(String args[]){
  30.    getConnection();
  31. }*/
  32. }

7 addUserServlet.java

 

  1. package servlet;

  2. import java.io.IOException;

  3. import javax.servlet.ServletException;
  4. import javax.servlet.http.HttpServlet;
  5. import javax.servlet.http.HttpServletRequest;
  6. import javax.servlet.http.HttpServletResponse;

  7. import dao.Dao;

  8. import bean.User;

  9. public class addUserServlet extends HttpServlet {

  10. /**
  11. * Destruction of the servlet.

  12. */
  13. public void destroy() {
  14.    super.destroy(); // Just puts "destroy" string in log

  15.    // Put your code here

  16. }

  17. public void doGet(HttpServletRequest request, HttpServletResponse response)
  18.     throws ServletException, IOException {

  19.    response.setContentType("text/html");
  20. doPost(request,response);
  21. }

  22. public void doPost(HttpServletRequest request, HttpServletResponse response)
  23.     throws ServletException, IOException {

  24.    response.setContentType("text/html");
  25.    response.setCharacterEncoding("utf-8");
  26.    String user1 = request.getParameter("user");
  27.    String pwd = request.getParameter("pwd");
  28.    String name = new String(request.getParameter("name").getBytes("ISO8859_1"),"utf-8");
  29.    String *** = new String(request.getParameter("***").getBytes("ISO8859_1"),"utf-8");
  30.    String age1 = request.getParameter("age");
  31.    User user = new User();
  32.    user.setUser(user1);
  33.    user.setPwd(pwd);
  34.    user.setName(name);
  35.    user.set***(***);
  36.    int age;
  37.    if(age1!=null)
  38.    {
  39.     age = Integer.parseInt(age1);
  40.     user.setAge(age);
  41.    }
  42.    Dao dao = new Dao();
  43.    dao.addUser(user);
  44. /* request.setAttribute("info",new String("

    添加成功!恭喜!!" +

  45.    "
    "));
  46.    request.setAttribute("id", new String("a"));
  47.    request.setAttribute("denglu",new String("

    登陆

    "));*/
  48.   
  49.   
  50.    request.getRequestDispatcher("info.jsp").forward(request,response);
  51. }

  52. public void init() throws ServletException {
  53.    // Put your code here

  54. }

  55. }

 

 

8  loginServlet.java

 

  1. package servlet;

  2. import java.io.IOException;
  3. import java.io.PrintWriter;
  4. import java.sql.SQLException;

  5. import javax.servlet.ServletException;
  6. import javax.servlet.http.HttpServlet;
  7. import javax.servlet.http.HttpServletRequest;
  8. import javax.servlet.http.HttpServletResponse;

  9. import bean.User;
  10. import dao.Dao;

  11. public class loginServlet extends HttpServlet {
  12. public void destroy() {
  13.    super.destroy(); // Just puts "destroy" string in log

  14.    // Put your code here

  15. }
  16. public void doPost(HttpServletRequest request, HttpServletResponse response)
  17.     throws ServletException, IOException {
  18.    request.setCharacterEncoding("utf-8");
  19.    response.setContentType("text/html");
  20.    PrintWriter out = response.getWriter();
  21.    String name = new String (request.getParameter("user").getBytes("ISO8859_1"), "GBK");
  22.    String pwd = new String (request.getParameter("pwd").getBytes("ISO8859_1"),"UTF-8");
  23.    User user = new User();
  24.    user.setUser(name);
  25.    user.setPwd(pwd);
  26.    Dao dao = new Dao();
  27.    boolean isLogin;
  28.    try
  29.    {
  30.     isLogin = dao.logoin(user);
  31.    
  32.     if(isLogin)
  33.     {
  34.      response.sendRedirect("MyJsp.jsp");
  35.     }else{
  36.      response.sendRedirect("index.jsp");
  37.     }
  38.    
  39.    } catch (SQLException e)
  40.    {
  41.     e.printStackTrace();
  42.    }
  43. }
  44. public void doGet(HttpServletRequest request, HttpServletResponse response)
  45. throws ServletException, IOException {
  46. doPost(request,response);
  47. }
  48. public void init() throws ServletException {
  49. }

  50. }

 

 

 

 

阅读(37938) | 评论(0) | 转发(1) |
给主人留下些什么吧!~~