10.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!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>登陆</title>
</head>
<body>
<h3 align="center">用户登录</h3>
<form action="11.jsp" method="post">
<table border="1" align="center">
<tr>
<td>用户名:</td>
<td><input type="text" name="usersname" value=""/></td>
</tr>
<tr>
<td>密码:</td>
<td><input type="password" name="passwordd" value=""/></td>
</tr>
<tr>
<td colspan="2" align="center"><input type="submit" name="submit" value="登陆"/></td>
</tr>
</table>
</form>
</body>
</html>
11.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>连接数据库</title>
</head>
<body>
<%
request.setCharacterEncoding("UTF-8");
String url="jdbc:mysql://127.0.0.1:3306/user";
String username="root";
String password="123456";
Class.forName("com.mysql.jdbc.Driver"); //加载数据库驱动
Connection conn=DriverManager.getConnection(url,username,password);//建立连接
Statement stmt=conn.createStatement();
ResultSet rs=stmt.executeQuery("select *from user");//执行查询语句
int i=0,j=0;
String name=request.getParameter("usersname");
String pwd=request.getParameter("passwordd");
if(name=="")
{
out.println("<script language='javascript'>alert('请输入用户名');window.location.href='10.jsp';</script>");
}
else if(pwd=="")
{
out.println("<script language='javascript'>alert('请输入密码');window.location.href='10.jsp';</script>");
}
else
{
while(rs.next())
{
if(name.equals(rs.getString(2)))
{
i=1;
if(pwd.equals(rs.getString(3)))
{
out.println("<script language='javascript'>alert('登陆成功');");
session.setAttribute("name1",name);
session.setAttribute("pwd1",pwd);
request.getRequestDispatcher("12.jsp").forward(request,response);
}
else
{
out.println("<script language='javascript'>alert('密码错误,请重新登陆');window.location.href='10.jsp';</script>");
}
}
}
if(i==0)
{
out.println("<script language='javascript'>alert('该用户不存在');window.location.href='10.jsp';</script>");
}
}
rs.close();
stmt.close();
conn.close();
%>
</body>
</html>
12.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!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>信息首页</title>
</head>
<body>
<%
int number;
if(application.getAttribute("number")==null)
{
number=1;
}
else
{
number = Integer.parseInt((String) application.getAttribute("number"));
number = number+1;
}
application.setAttribute("number",String.valueOf(number));
String name=(String)session.getAttribute("name1");
String pwd=(String)session.getAttribute("pwd1");
%>
<h3 align="center">您的信息如下</h3>
<table align="center" border="1">
<tr>
<td>您的用户名是:</td>
<td><%=name %></td>
</tr>
<tr>
<td>您的密码是:</td>
<td><%=pwd %></td>
</tr>
<tr>
<td colspan="2" align="center">在线人数:<%=number%></td>
</tr>
<tr>
<td colspan="2" align="center"><a href="13.jsp">注销</a></td>
</tr>
</table>
</body>
</html>
13.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!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>Insert title here</title>
</head>
<body>
<%
int number = Integer.parseInt((String)application.getAttribute("number"));
number=number-1;
application.setAttribute("number",String.valueOf(number));
session.removeAttribute("name1");
session.removeAttribute("pwd1");
out.print("<script language='javascript'>alert('已注销!');window.location.href='10.jsp';</script>\n");
%>
</body>
</html>