login.jsp
<%
String num=request.getParameter("num");
if(num!=null){
if(num.equals("0"))
out.println("<script>confirm('输入的密码有误!,请确认是否继续')</script>");
if(num.equals("1"))
out.println("<script>confirm('输入的用户名不存在!,请确认是否继续')</script>");
}
%>
<form action="LoginC.jsp" method="post">
用户名<input type="text" name="username"><br>
密 码<input type="password" name="password"><br>
<input type="submit" value="登录"> <input type="reset" value="重置">
</form>
logincl.jsp
<%//验证用户名和密码是否正确
String username=request.getParameter("username");
String pass=request.getParameter("password");
Class.forName("com.mysql.jdbc.Driver");//数据库的连接
String url="jdbc:mysql://localhost:3306/JspTest?user=root&password=123456";
Connection ct=DriverManager.getConnection(url);
Statement st=ct.createStatement();
String sql="select password from users where username="+username+" ";
ResultSet rs=st.executeQuery(sql);
if(rs.next()){
if(rs.getString(1).equals(pass)){
//合法用户
response.sendRedirect("Wel.jsp?u="+username);
}else {
//密码错误
response.sendRedirect("Login.jsp?num=0");
}
}
else{
//用户名不存在
response.sendRedirect("Login.jsp?num=1");
}
%>