创建StringUtil的JavaBean类,判断用户名和密码
public class StringUtil {
private String name; //用户名
private String pwd; //密码
private String cue; //提示信息
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPwd() {
return pwd;
}
public void setPwd(String pwd) {
this.pwd = pwd;
}
public String getCue() {
//如果用户名或密码为null或者“”
if((name==null||name.equals("")||(pwd==null)||pwd.equals(""))){
cue="请输入账号和密码!!";
}else{
if((name.equals("zj"))&&(pwd.equals("123"))){
cue="登录成功!!";
}else{
cue="登录失败!!";
}
}
return cue;
}
public void setCue(String cue) {
this.cue = cue;
}
}
创建index.jsp页面
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<form action="login.jsp" method="post">
<table>
<tr>
<td align="right">请输入同户名:</td>
<td><input type="text" name="name"></td>
</tr>
<tr>
<td>请输入密码:</td>
<td><input type="password" name="pwd"></td>
</tr>
<tr>
<td colspan="2" align="center"><input type="submit" value="登录"></td>
</tr>
</table>
</form>
</body>
</html>
创建login.jsp页面,验证用户登录信息
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<%
String name=request.getParameter("name");
String pwd=request.getParameter("pwd");
%>
<jsp:useBean id="strBean" class="com.cn.zj.test.StringUtil"></jsp:useBean>
<jsp:setProperty property="name" name="strBean" value="<%=name %>"/>
<jsp:setProperty property="pwd" name="strBean" value="<%=pwd %>"/>
<table>
<tr>
<td align="right">提示信息:</td>
<td>
<!-- 从StringUtil对象中获得属性值 -->
<jsp:getProperty property="cue" name="strBean"/>
</td>
</tr>
<tr>
<td colspan="2" align="center"><a href="index.jsp">返回</a>
</tr>
</table>
</body>
</html>