- 用户名密码验证模版
<%@page import="com.jerehedu.bao.User"%>
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<%
//获取一个用户
User user=(User)session.getAttribute("user");
%>
<%
String uname=request.getParameter("uname");
String upwd=request.getParameter("upwd");
if(uname!=null&&!uname.equals("")&&upwd!=null&&!upwd.equals("")) {
//创建一个user对象
user=new User(uname,upwd);
session.setAttribute("user", user);
}
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>My JSP ‘ch0402.jsp‘ starting page</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
<script type="text/javascript">
/*
*验证用户名或密码是否填写
*/
function check(){
var uname=document.getElementById(‘uname‘).value;
var upwd=document.getElementById(‘upwd‘).value;
if(uname==‘‘) {
alert(‘请填写用户名‘);
return false;
} else if(upwd==‘‘) {
alert(‘请填写密码‘);
return false;
}
}
</script>
</head>
<body>
<form action="login1.jsp" onsubmit="return check()" method="post">
<table>
<tr>
<td>用户名:</td>
<td>
<input type="text" name="uname" id="uname" value="" />
</td>
</tr>
<tr>
<td>密码:</td>
<td>
<input type="password" name="upwd" id="upwd" value="" />
</td>
</tr>
<tr>
<td>
<input type="submit" value="登录" />
</td>
</tr>
<% if(user!=null&&!(user.getUserName().equals("admin")&&user.getPassword().equals("123456"))) {
%>
<tr>
<td colspan="2">
用户名或密码错误
</td>
</tr>
<%
} else if(user!=null){
response.sendRedirect("welcome.jsp");
}
%>
</table>
</form>
</body>
- 注销1:
- <body>
欢迎您: <%=session.getAttribute("user").toString() %>
<a href="Day5_1_zhuxiao.jsp">注销</a>
</body>
- 注销2:
- <body>
<%
session.invalidate();
response.sendRedirect("Day5_1.jsp");
//response.addHeader("refresh","2; URL=Day5_1.jsp");
%>
您已注销
</body>
- 清空对象
- <%
//把session对象清空
session.invalidate();
response.sendRedirect("login.jsp");
%>