在上一节,我们自己写的web框架,只能运行显示一个HelloWorld。现在我们对其进行一次加工,让他至少能运行一个登陆程序。
首先看login.jsp
<%@ page contentType="text/html; charset=UTF-8" language="java" import="java.sql.*" errorPage="" %>
<%@ page import="
java.util.*,
javax.servlet.*,
javax.servlet.http.*,
com.gc.action.User"%>
<%!public static final String _AppId = "login";%>
<%
HashMap<String,Object> infoOut=null;
if(request.getAttribute("infoOut") == null)
infoOut=new HashMap<String,Object>();
else
infoOut=(HashMap<String,Object>)request.getAttribute("infoOut");
String msg = infoOut.get("msg") == null ? "" : (String) infoOut
.get("msg");
User user = infoOut.get("user") == null ? new User()
: (User) infoOut.get("user");
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>采用新的框架实现用户登录验证</title>
<script language=Javascript>
function submit(target, action) {
form1.target = target;
form1.action.value = action;
form1.submit();
}
function check() {
form1.forwardJsp.value="login"; //再次跳转回来
form1.logicName.value="LoginAction";
submit(‘<%="login_" + session.getId()%>‘,‘login‘);
}
</script>
</head>
<body leftmargin="0" topmargin="0">
<%-- 这个action 目前没有用 我们可以随意写 --%>
<form name="form1" action="xx.do" method="post">
<H3>
<font color=‘red‘><%=msg%></font>
</H3>
用户名:<input type="text" name="username"><br> <br>
密码: <input type="text" name="password">
<br>
<input type="button" name="button" value="提交" onClick="return check()">
<input type="reset" name="button" value="重置">
<%-- 这一次我们要从jsp端发起请求,设置三个参数 --%>
<input type="hidden" name="action" value="">
<input type="hidden" name="forwardJsp" value="">
<input type="hidden" name="logicName" value="">
</form>
<script language=Javascript>
window.name = "<%="login_"+session.getId()%>";
</script>
</body>
</html>
表现层有了,控制层我们可以复用第一节的GdServlet,现在就差模型层了。
package com.gc.action;
import java.util.HashMap;
import com.gd.action.Action;
public class LoginAction implements Action{
public HashMap<String, Object> doAction(HashMap<String, Object> infoIn) {
String action = (infoIn.get("action") == null) ? "" : (String) infoIn
.get("action");
HashMap<String, Object> infoOut = infoIn;
if (action.equals(""))
infoOut = this.doInit(infoIn);
else if (action.equals("login"))
infoOut = this.doLogin(infoIn);
return infoOut;
}
/**该方法用来实现没有传入动作时要处理的内容
* @param infoIn
* @return HashMap
*/
private HashMap<String, Object> doInit(HashMap<String, Object> infoIn) {
HashMap<String, Object> infoOut = infoIn;
infoOut.put("msg", "请输入用户名和密码");
return infoOut;
}
/**该方法用来实现输出HelloWorld
* @param infoIn
* @return HashMap
*/
public HashMap<String, Object> doLogin(HashMap<String, Object> infoIn){
HashMap<String, Object> infoOut = infoIn;
String username = (infoIn.get("username") == null) ? "" : (String)infoIn.get("username");
String password = (infoIn.get("password") == null) ? "" : (String)infoIn.get("password");
if ("gd".equals(username) && "123456".equals(password)) {
infoOut.put("forwardJsp", "success");
infoOut.put("msg", "登录成功");
} else if ("gd".equals(username) && !"123456".equals(password)) {
infoOut.put("msg", "密码错误");
} else if (!"gd".equals(username) && "123456".equals(password)) {
infoOut.put("msg", "用户名错误");
} else if (!"gd".equals(username) && !"123456".equals(password)) {
infoOut.put("msg", "用户名和密码都输入错误");
} else if ("".equals(username) && "".equals(password)) {
infoOut.put("msg", "请输入用户名和密码");
}
return infoOut;
}
}
infoOut.put(“forwardJsp”, “success”);
如果登陆成功,就返回success.jsp。
注意:本来forwardJsp在login.jsp里就设置了,是login。这里的逻辑是一旦成功登陆,就返回success。
success.jsp
<%@ page contentType="text/html; charset=GBK" language="java" import="java.sql.*" errorPage="" %>
<%@ page import="java.sql.*,java.util.*,javax.servlet.*,
javax.servlet.http.*,java.text.*,java.math.*,com.gd.mvc.io.InfoInAndOut,com.gd.mvc.io.impl.GdInfoInAndOut"
%>
<%! public static final String _AppId = "login"; %>
<%
HashMap<String,Object> infoOut=null;
if(request.getAttribute("infoOut") == null)
infoOut=new HashMap<String,Object>();
else
infoOut=(HashMap<String,Object>)request.getAttribute("infoOut");
String msg = infoOut.get("msg") == null ? "" : (String)infoOut.get("msg");
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>采用新的框架实现用户注册验证</title>
</head>
<body leftmargin="0" topmargin="0">
<form name="form1" action="/myApp/do" method="post">
<H3><font color=‘red‘><%=msg%></font><H3>
<input type="hidden" name="action" value="">
</form>
<script language=Javascript>
window.name = "<%="login_"+session.getId()%>";
</script>
</body>
</html>
咱们看看效果:
我直接访问了servlet,没有经过jsp那就自然没有用户名与密码了。
点提交之后
别的效果,我就不贴图了,大家应该都能想出来。
集腋成裘,聚沙成塔。慢慢来,在下一节,我们继续完善我们的框架。
原文地址:http://blog.csdn.net/dlf123321/article/details/45399605