jsp代码连接数据库实现显示登陆、退出、显示在线人数

本文介绍了一个简单的用户登录系统实现过程,包括登录页面的设计、数据库连接验证及用户信息展示等环节,并涉及了会话管理和注销功能。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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>

 

 

~bug:用户重复登陆

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值