📄 login.jsp
字号:
<%@ page session="true" contentType="text/html;charset=GBK" %>
<html>
<head>
<title>用户登录</title>
</head>
<body>
<%String B1=request.getParameter("B1");%>
<%if (B1==null){
//没有点击过“确定”按钮%>
<div align="center">
<center>
<table border="2" width="400" bordercolorlight="#FFFFFF" bordercolordark="#000000" cellspacing="0" cellpadding="0">
<tr>
<td width="100%">
<form method="POST" action="login.jsp" name="login_form">
<p align="center">用户:<input type="text" name="userName" size="20"></p>
<p align="center">密码:<input type="password" name="passwd" size="20"></p>
<p align="center"><input class="buttonface" type="submit" value="确定" name="B1">
<input class="buttonface" type="reset" value="重写" name="B2"></p>
</form>
</td>
</tr>
</table>
</center>
</div>
</body>
</html>
<%}else{
request.setCharacterEncoding("GBK");
//初始会话值为false
session.setAttribute("login","false");
//获取用户名和口令
String userName,passwd;
userName=request.getParameter("userName");
passwd=request.getParameter("passwd");
//去掉两头的空格
userName=userName.trim();
passwd=passwd.trim();
//用户名或密码为空,重新登录
if ((userName==null)||(passwd==null)) {
response.sendRedirect("./login.jsp");
}else{
if (userName.equals("cxd")&&passwd.equals("admin")){
//这里只是简单的比较字符串,如果数据存储在文件或数据库中,应先取得该值
//尽量避免将用户名、密码等以明文写在代码中。
//用户名或密码都正确,设置会话属性login值为true
session.setAttribute("login","true");
session.setAttribute("userName",userName);
%>
<h2>用户登录成功</h2>
可以自动或提供URL来转向其工作页面<br/>
工作页面中使用session.getAttribute("login")获取session属性,为"true"时方可进行其他操作。<br/>
<a href="workPage.jsp">工作页面</a></br>
<a href="chat.jsp">聊天室</a>
<% }else{
response.sendRedirect("./login.jsp");
}
}
}%>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -