1.编写一个JSP程序能够获得表单的所有参数,并获得参数的所有参数值
Main1.jsp
<%@ page language="java" contentType="text/html; charset=gb2312"%>
<html>
<head>
<title>表单</title>
</head>
<body>
<center>
<form action="Main.jsp" method="post">
<table>
<tr>
<td>姓 名:</td>
<td><input type="text" name="name"/></td>
</tr>
<tr>
<td>年 龄:</td>
<td><input type="text" name="age"/></td>
</tr>
<tr>
<td>性 别:</td>
<td><input type="radio" name="sex" value="Male">男
<input type="radio" name="sex" value="Female">女</td>
</tr>
<tr>
<td>电话号码:</td>
<td><input type="tel" name="tel"/></td>
</tr>
<tr>
<td>爱 好:</td>
<td><input type="checkbox" name="hobby" value="Music">音乐
<input type="checkbox" name="hobby" value="Video">影视
<input type="checkbox" name="hobby" value="Read">阅读
<input type="checkbox" name="hobby" value="Game">游戏</td>
</tr>
<tr>
<td colspan="2">
<input type="submit" value="提交">
<input type="reset" value="重置">
</td>
</tr>
</table>
</form>
</center>
</body>
</html>
Main.jsp
<%@ page language="java" contentType="text/html;charset=UTF-8" import="java.util.*"%>
<html>
<head>
<title>获得所有的参数名和值</title>
</head>
<body>
<%
Map mapParameter = request.getParameterMap();
String [] strName = (String[]) mapParameter.get("name");
out.println("姓名: " + strName[0] + "<br>");
String [] strAge = (String[]) mapParameter.get("age");
out.println("年龄: " + strAge[0] + "<br>");
String [] strSex = (String[]) mapParameter.get("sex");
out.println("性别: " + strSex[0] + "<br>");
String [] strTel = (String[]) mapParameter.get("tel");
out.println("电话号码: " + strTel[0] + "<br>");
String [] strHobby = (String[]) mapParameter.get("hobby");
out.println("爱好: ");
for(String hobby : strHobby){
out.println(hobby);
}
%>
</body>
</html>
运行结果:
2.编写一个JSP程序能够进行页面自动刷新。
<%@ page language="java" contentType="text/html;charset=gb2312" import="java.util.*"%>
<html>
<head>
<title>Refresh</title>
</head>
<body>
<%
response.addIntHeader("Refresh",10);
%>
<%
Date now = new Date();
out.print(now);
%>
</body>
</html>
运行结果:
借助显示时间来证明页面自动刷新,每十秒刷新一次。
3.编写一个JSP程序用来判断用户信息是否正确,如果正确,则在session范围添加属性"login",其值为”true"。在用户登录成功页面增加对session范围中"login"属性值的判断,如果正确,则显示用户登录成功,否则跳转到用户登录页面。
LoginDemo.jsp
<%@ page language="java" contentType="text/html;charset=gb2312"%>
<html>
<head>
<title>登录页面</title>
</head>
<body>
<center>
<%
session.setAttribute("login","true");
%>
<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"); //接收用户密码参数
String strLogin = (String)session.getAttribute("login");
%>
<%
//判断用户名及密码,如果为指定用户且login属性为true则跳转到登录成功页面
if ("James".equals(username) && "1234".equals(userpassword) && "true".equals(strLogin)){
%>
<jsp:forward page = "LoginSuccess.jsp"/>
<%
}
//如果不是指定用户或者login属性不为true则跳转到登录失败页面
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>
运行结果:
此时login为true。
当把login的值改为false时,登录失败
还是有不小的BUG的,当login为其他字符时也是登录失败的,应该把login定性为boolean性质的,由于时间问题,容后再琢磨琢磨。