用html和jsp实现网站的登陆功能

显示界面 login.html

<html >
<head>
<meta http-equiv="Content-Type" content="text/html; charset=GB2312" />
<title>登陆</title>
</head>

<body>
<center>
<form action="
http://127.0.0.1:8080/DB/login.jsp" method="post">
用户名:<input type="text" maxlength="20" size="20" id="muser" name="muser">
<br>
密码:<input type="password" maxlength="20" size="20" id="passwd" name="passwd">
<br>
<input type="submit" value="登陆" id="mlogin" name="mlogin">&nbsp;&nbsp;&nbsp;
<input type="reset" value="重置" id="mre" name="mre">
</form>
<br>
<a href="#">忘记密码?</a>&nbsp;&nbsp;&nbsp;<a href="protocol.html">立即注册</a>
</center>
</body>
</html>

登陆提交到的jsp页面login.jsp

<%@ page contentType="text/html" language="java" import="java.sql.*" errorPage="" pageEncoding="GB2312"%>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=GB2312" />
<meta http-equiv="Expires" content="0">
<meta http-equiv="kiben" content="no-cache">
<title>登录验证</title>
<%
request.setCharacterEncoding("GB2312");//设置请求的编码格式为GB2312
//开始连接数据库
String Suser = request.getParameter("muser");
String Spasswd = request.getParameter("passwd");
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver").newInstance();
Connection  conn = DriverManager.getConnection("jdbc:sqlserver://localhost:1433;DatabaceName=ProjManagerPlatform", "sa", "100862");

Statement  stmt = conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_READ_ONLY);
try{
ResultSet rs;
rs = stmt.executeQuery("select userName from ProjManagerPlatform.dbo.UsersInfo where userName='"+Suser+"'");//查询语句
if(rs.next()){
 Suser = rs.getString("userName");         //得到userName在数据库中的值
}else{
 rs.close();
 stmt.close();
   conn.close();
%>

<!--window.location.href = "login.html"是跳转到login.html页面-->

<script language="javascript" type="text/javascript">alert("用户名不存在!");window.location.href = "login.html"</script>
<%
}
rs = stmt.executeQuery("select password from ProjManagerPlatform.dbo.UsersInfo where userName='"+Suser+"'");
rs.next();
Spasswd = rs.getString("password");       //得到password在数据库中的值
if(request.getParameter("passwd").equals(Spasswd)){
 rs.close();
 stmt.close();
   conn.close();
%>
<!--window.location.href = "indexm.htm"登陆成功跳转到主页面-->
<script language="javascript" type="text/javascript">window.location.href = "indexm.htm"</script>
<%
}else{
 rs.close();
 stmt.close();
   conn.close();
%>
<script language="javascript" type="text/javascript">alert("用户密码错误!");window.location.href = "login.html"</script>
<%
 }
 }
 catch(Exception e){
  out.println(e.getMessage());
 }
%>
</head>

<body>


</body>
</html>

 

 

 

  • 3
    点赞
  • 17
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
使用jsp内置对象实现登录和注销功能可以通过以下步骤实现: 1. 创建一个登录页面,包含用户名和密码的输入框以及登录按钮。 2. 在登录页面中使用form表单提交数据到一个jsp页面。 3. 在jsp页面中使用request对象获取用户名和密码,并进行验证。 4. 如果验证通过,将用户信息保存到session对象中。 5. 在其他需要登录才能访问的页面中,使用session对象判断用户是否已经登录,如果没有登录则跳转到登录页面。 6. 在注销功能中,使用session对象清除保存的用户信息。 示例代码如下: 登录页面: ``` <form action="login.jsp" method="post"> <label>用户名:</label> <input type="text" name="username"><br> <label>密码:</label> <input type="password" name="password"><br> <input type="submit" value="登录"> </form> ``` login.jsp页面: ``` <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <% String username = request.getParameter("username"); String password = request.getParameter("password"); if("admin".equals(username) && "123456".equals(password)){ session.setAttribute("username", username); response.sendRedirect("index.jsp"); }else{ out.println("用户名或密码错误!"); } %> ``` 其他需要登录才能访问的页面: ``` <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <% String username = (String)session.getAttribute("username"); if(username == null){ response.sendRedirect("login.jsp"); } %> <html> <head> <title>需要登录才能访问的页面</title> </head> <body> <h1>欢迎访问需要登录才能访问的页面!</h1> <a href="logout.jsp">注销</a> </body> </html> ``` 注销页面: ``` <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <% session.invalidate(); response.sendRedirect("login.jsp"); %> ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

蟹道人

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值