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

来源:互联网 发布:郭敬明 梦里花落知多少 编辑:程序博客网 时间:2024/05/19 02:43

显示界面 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>

 

 

 

原创粉丝点击