JSP完成简单的登陆程序
1.login.jsp
用户输入信息的表单
<%@page contentType="text/html;charset=gb2312"%>
<html>
<head>
<title>登陆</title>
</head>
<body>
<center>
<h1>登陆范例-----用户及密码固定</h1>
<br>
<br>
<br>
<form action="login_conf.jsp" method="post">
<table>
<tr>
<td colspan="2">用户登陆</td>
</tr>
<tr>
<td>用户名:</td>
<td><input type="text" name="uname"></td>
</tr>
<tr>
<td>密  码:</td>
<td><input type="password" name="upassword"></td>
</tr>
<tr>
<td colspan="2">
<input type="submit" value="登陆">
<input type="reset" value="重置">
</td>
</tr>
</table>
</center>
</body>
</html>
2.login_conf.jsp
接收参数
判断参数
根据参数是否合法进行跳转
//问题:当不从login.jsp进入这里,而是直接进行这个页面会报空参数错误
<%@page contentType="text/html;charset=gb2312"%>
<html>
<head>
<title>登陆</title>
</head>
<body>
<center>
<h1>登陆范例-----用户及密码固定</h1>
<br>
<br>
<br>
<%
//接收用户请求
String name=request.getParameter("uname");
String password=request.getParameter("upassword");
%>
<%
//if("mldn"==name&&"lxh"==passoword)//这里比较的是地址,所以不能用这个
if(name.equals("mldn")&&password.equals("lxh"))//如果直接跳到这个页面,而name和passwrod没有值,因为没有加载 第一个页面,所以也就不能传值过来,所以有报有空异常
修改:”mldn”是一个字符串对象,所以可以把”mldn”放到name位置,改为下:
if(“mldn”.equals(name)&&”lxh”.equals(password))//有经验的开发者都会这么做
{
//合法用户
%>
<jsp:forward page="login_success.jsp">
<jsp:param name="name" value="<%=name%>"/>
</jsp:forward>
//红色代码也可以修改为:<jsp:forward page=”login_success.jsp”/>其它的成了多余的代码了
<%
}
else
{
//非法用户
%>
<jsp:forward page="login_failure.jsp"/>
<%
}
%>
</center>
</body>
</html>
3.login_success.jsp
显示欢迎信息
<%@page contentType="text/html;charset=gb2312"%>
<html>
<head>
<title>登陆</title>
</head>
<body>
<center>
<h1>登陆范例-----用户及密码固定</h1>
<br>
<br>
<br>
<h2>登陆成功</h2>
<h3>欢迎<font color="red" size="15">
<%=request.getParameter("name")%>
</font>光临!!</h3>
</center>
</body>
</html>
1. login_failure.jsp
显示登陆信息
给出一个超级链接,可以使用户重新登陆
<%@page contentType="text/html;charset=gb2312"%>
<html>
<head>
<title>登陆</title>
</head>
<body>
<center>
<h1>登陆范例-----用户及密码固定</h1>
<br>
<br>
<br>
<h2>登陆失败</h2>
<h3>错误的用户名或密码!!</h3>
<h3><a href="login.jsp">重新登陆</a></h3>
</center>
</body>
</html>
重要知识点:
- equals使用进要当心空指向错误
- 只要服务器端跳转<jsp:forward>,则请求内容可以再跳转到之后的页面继续得到.
- equals与”==”的区别
在比较一个字符串内容的时候必须使用”equals”
而在比较字符串的地址时使用”==”;