login.jsp~29~
上传用户:top0756
上传日期:2022-08-11
资源大小:6501k
文件大小:2k
源码类别:

Jsp/Servlet

开发平台:

VBScript

  1. <%@ page contentType="text/html; charset=GB2312" %>
  2. <%@ page import="java.sql.*" %>
  3. <%@ page language="java" %>
  4. <%@ page import="java.util.*" %>
  5. <%@ page import=" proj112.ISOtoGb2312  "%>
  6. <html>
  7. <head>
  8. <title>
  9. 身份验证
  10. </title>
  11. </head>
  12. <body bgcolor="#ffffff">
  13. <%
  14. String user=ISOtoGb2312.convert(request.getParameter("username"));
  15. String pwd=ISOtoGb2312.convert(request.getParameter("password"));
  16. String select=request.getParameter("id");
  17. String url ="jdbc:odbc:dsn112";
  18. String userName = "sa";
  19. String password = "";
  20. String URL="";
  21. String sql="";
  22. Connection conn = null;
  23. try {
  24. Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
  25.                  } catch(ClassNotFoundException e) {
  26. out.println("加载驱动器类时出现异常");
  27. }
  28. try {
  29.  conn = DriverManager.getConnection(url, userName, password);
  30. //创建Statement语句
  31.                         }
  32.                 catch(SQLException e) {
  33. out.println("连接数据库的过程中出现SQL异常<br>");
  34. }
  35. Statement stmt = conn.createStatement();
  36.                         if(select.equals("student")){
  37. sql = "SELECT * FROM student where stu_id='"+user+"'and stu_password='"+pwd+"'";
  38.                         URL="student.jsp";
  39.                         }
  40.                         else if(select.equals("admin")){
  41.                          sql = "SELECT * FROM admin where name='"+user+"'and ad_password='"+pwd+"'";
  42.                          URL="admin.jsp";
  43.                          }
  44.                          //使用executeQuery执行SQL查询语句
  45. ResultSet rs = stmt.executeQuery(sql);
  46.                         if(rs.next()){
  47.                           String a=rs.getString("stu_name");
  48.                           request.getSession(true);
  49.                           session.setAttribute("stu_name",a);
  50.                           session.setAttribute("id",user);
  51.                           response.sendRedirect(URL) ;}
  52.                         else
  53.                         response.sendRedirect("error.jsp") ;
  54. %>
  55. </body>
  56. </html>