JSP登录
JSP登录页面
包括login.html, login_check.jsp,login_success.html, login_fail.html.
Login.html
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Login Page</title>
</head>
<body>
<div>LOGIN</div>
<form action="login_check.jsp" method="post">
<table>
<tr>
<td>ID:</td>
<td><input name="id"type="text"></td>
</tr>
<tr>
<td>Password:</td>
<td><input name="password"type="password"></td>
</tr>
<tr>
<td>
<input name="submit" type="submit">
</td>
<td>
<input name="reset" type="reset">
</td>
</tr>
</table>
</form>
</body>
</html>
login_check.jsp
<%@ page language="java" contentType="text/html;charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ page import="java.sql.*" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Login_check</title>
</head>
<body>
<%!
public static final String driver = "com.mysql.jdbc.Driver";
public static final String url = "jdbc:mysql://localhost:3306/mldn";
public static final String user = "root";
public static final String password = "";
%>
<%
Connection conn = null;
PreparedStatement pstmt = null;
ResultSet rs = null;
boolean flag = false;
String name = null;
%>
<%
try{
Class.forName(driver);
conn =DriverManager.getConnection(url, user, password);
String sql = "select name from user where userid=?and password=?";
pstmt = conn.prepareStatement(sql);
pstmt.setString(1,request.getParameter("id"));
pstmt.setString(2,request.getParameter("password"));
rs = pstmt.executeQuery();
if(rs.next()){
name = rs.getString(1);
flag = 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="uname"value="<%=name %>"/>
</jsp:forward>
<% }else{
%>
<jsp:forward page="login_fail.jsp"/>
<%
}
%>
</body>
</html>
login_success.html
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>SuccessPage</title>
</head>
<body>
Welcome! <%=request.getParameter("uname") %>! You have been login success!
</body>
</html>
login_fail.html
<!DOCTYPE html PUBLIC "-//W3C//DTDHTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<center>Sorry,login fail!</center>
</body>
</html>
数据库
create table user(
userid varchar(10),
namevarchar(10),
password varchar(10)
);
insert into user values ("*", "**", "***");