今天做了一个十分简单的jsp内置对象实验,通过生成jsp登录界面,判断登录信息是否正确,正确跳转到显示登录正确信息的界面,否则跳转到显示错误信息的界面。
实验工具:tomcat5.0 Myeclipse
首先是登录界面Login.jsp的代码:
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<
<body>
<form action="check.jsp" method="post">
用户名:<input type="text" name="username"/><br/>
密 码:<input type="password" name="passwd"/><br/><br/>
<input type="submit" value="确定"/>
<input type="reset" value="取消"/>
</form>
</body>
</html>
检查登录信息是否正确的check.jsp:
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<body>
<% String Name=request.getParameter("username");
String Passwd=request.getParameter("passwd");
if(Name.equals("liuweifeng") && Passwd.equals("3152052051601"))
{%>
<jsp:forward page="welcome.jsp"/>
<% }else response.sendRedirect("error.jsp");%>
</body>
</html>
登录成功的界面welcome.jsp:
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<body>
<%String Name=request.getParameter("username");%>
欢迎你,<%=Name%>成功登录!
</body>
</html>
登陆失败的界面error.jsp:
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<body>
对不起,你的信息有误,请重新登录! <br/>
<form action="login.jsp" method="post">
<input type="submit" value="返回登录"/>
</form>
</body>
</html>
本文转自 寂岚峰 51CTO博客,原文链接:http://blog.51cto.com/13271983/1969592,如需转载请自行联系原作者