model1--->model1.x---->mvc(model2)
1.Login.jsp
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
</head>
<body>
<form action="/jsp1/LoginCheck.jsp" method="post">
用户名:<input type="text" name="id"/><br>
密 码:<input type="password" name="passw"/><br>
<input type="submit" value="提交"/>
<input type="reset" value="重置"/>
<hr>
</form>
</body>
</html>
2.LoginCheck,jsp
<%@ page language="java" import="java.util.*,java.sql.*" 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 'LoginCheck.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>
<%!
//定义数据库驱动(全局变量)
public static final String DRIVER = "com.microsoft.sqlserver.jdbc.SQLServerDriver"; //sqlserver 驱动
public static final String DBURL = "jdbc:sqlserver://localhost:1433;DatabaseName=Demo";//数据库连接地址
public static final String DBUSER = "sa"; //数据库用户名
public static final String DBPASSW = "Founder123";//密码
%>
<%
Connection conn = null; //声明数据库连接对象
PreparedStatement pstmt = null;//声明数据库操作
ResultSet rs = null;//声明数据库结果集
boolean flag = false;//定义标志位
String id = null;
%>
<%
try{
Class.forName(DRIVER);//加载驱动
conn = DriverManager.getConnection(DBURL,DBUSER,DBPASSW);//获取数据库连接
String sql = "select * from user1 where id = ? and password = ? ";
pstmt = conn.prepareStatement(sql);//实例化数据库操作
pstmt.setString(1,request.getParameter("id"));
pstmt.setString(2,request.getParameter("passw"));
rs = pstmt.executeQuery();//执行查询
if(rs.next()){
id = rs.getString(1);
flag = true; //标记为true,表示登陆成功
}
}catch(Exception e){
System.out.println(e);
}
finally{
try{
rs.close();
pstmt.close();
conn.close();
}catch(Exception e){}
}
%>
<%
if(flag){
%>
<jsp:forward page="login_success.jsp">
<jsp:param name="id" value="<%=id %>"/>
</jsp:forward>
<%
}else{
%>
<jsp:forward page="login_failure.jsp"/>
<%} %>
</body>
</html>
3.login_success.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 'login_success.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>
<center>
<h1>登陆成功</h1>
<h2>欢迎<font color="red"><%=request.getParameter("id") %></font>光临</h2>
</center>
</body>
</html>
4.login_failure.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 'login_failure.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>
<center>
<h2>登陆失败,请重新<a href="Login.jsp">登陆</a></h2>
</center>
</body>
</html>