登录界面:
find.jsp:
<%@page contentType="text/html" import ="java.sql.*" pageEncoding="UTF-8"%>
<html>
<head><title>登录</title></head>
<body>
<%
request.setCharacterEncoding("utf-8");
String id=request.getParameter("id");
String passwd=request.getParameter("passwd");
boolean flag=false;
PreparedStatement sql=null;
ResultSet rs=null;
Connection conn=null;
%>
<%
String driverName = "com.mysql.jdbc.Driver";
String userName = "root";
String userPwd = "aaa312";
String dbName="students";
String url1 = "jdbc:mysql://localhost:3306/"+dbName;
String url2="?user="+userName+"&password="+userPwd;
String url3="&useUnicode=true&characterEncoding=utf-8";
String url=url1+url2+url3;
Class.forName(driverName);//加载驱动
conn= DriverManager.getConnection(url,userName,userPwd);
sql =conn.prepareStatement("select * from students_info where id=? and passwd=?");
sql.setString(1,id);
sql.setString(2,passwd);
rs=sql.executeQuery();
if (rs.next()) {
flag=true;
}
rs.close();
sql.close();
conn.close();
%>
<!-- 判断是否是正确的登录用户 -->
<% if(flag==true)
{%>
<jsp:forward page="s.jsp"/>
<% }else response.sendRedirect("f.jsp");%>
</body>
</html>
s.jsp:
<%@ 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 'welcome.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>
<%String Name=request.getParameter("username");%>
欢迎你,<%=Name%>成功登录!
</body>
</html>
f.jsp:
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<body>
对不起,你的信息有误,请重新登录! <br/>
<form action="login.jsp" method="post">
<input type="submit" value="返回登录"/>
</form>
</body>
</html>
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。