一、一个简单的用户登录案例
LoginForm.jsp
<%@ page language="java" contentType="text/html;charset=gb2312"%>
<html>
<head>
<title>用户登录</title>
</head>
<body>
<center>
<form action = "LoginConf.jsp" method="post">
<table>
<tr>
<td colspan="2">用户登录</td>
</tr>
<tr>
<td>用户名:</td>
<td><input type="text" name="username"></td>
</tr>
<tr>
<td>密 码:</td>
<td><input type="password" name="userpassword"></td>
</tr>
<tr>
<td colspan="2">
<input type="submit" value="登录">
<input type="reset" value="重置">
</td>
</tr>
</table>
</form>
</center>
</body>
</html>
LoginConf.jsp
<%@ page language="java" contentType="text/html; charset=gb2312"%>
<html>
<head>
<title>登录判断</title>
</head>
<body>
<center>
<%
String username = request.getParameter("username"); //接收用户名参数
String userpassword = request.getParameter("userpassword"); //接收用户密码参数
%>
<%
//判断用户名及密码,如果为指定用户则跳转到登录成功页面
if ("James".equals(username) && "1234".equals(userpassword)){
%>
<jsp:forward page = "LoginSuccess.jsp"/>
<%
}
//如果不是指定用户则跳转到登录失败页面
else {
%>
<jsp:forward page = "LoginFailure.jsp"/>
<%
}
%>
</center>
</body>
</html>
LoginSuccess.jsp
<%@ page language="java" contentType="text/html;charset=gb2312"%>
<html>
<head>
<title>登录成功</title>
</head>
<body>
<center>
<h1>登录成功</h1>
</center>
</body>
</html>
LoginFailure.jsp
<%@ page language="java" contentType="text/html;charset=gb2312"%>
<html>
<head>
<title>登录失败</title>
</head>
<body>
<center>
<h1>登录失败</h1>
</center>
</body>
</html>
运行结果:
二、填空。
1.与Servlet有关的内置对象,包括 page 和 config 两个内置对象。
2.JSP中提供了4种属性保存范围,分别为 page 、request 、 session 、 application。
3.通过request对象的getParameter(String name)方法,可以获得参数名为name的参数值。
4.通过response对象的sendRedirect(URL)方法设置页面重定向,从而实现页面跳转。
5.通过out对象的print(String str)方法和println(String str)方法进行页面输出。
6.通过session对象的setMaxInactiveInterval()方法设置session生命周期,通过getMaxInactiveInterval()方法获得session生命周期。