一:
编写一个页面,不允许以192.开头的客户访问,如果访同,则给它回送信息“话。禁止”。
<%@ page language="java" import="java.sql.*" contentType="text/html; charset=gb2312"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<body>
<%
String remoteAddr =request.getRemoteAddr();
if(remoteAddr.startsWith("192.")){
out.println("访问禁止!");
}
%>
</body>
</html>
2.在贝面1中输人一个
图书价格,到达页面2.在页面2中输人一个汇率,提交,在而界面3中显示价格/汇率的结果。
<%@ page language="java" import="java.sql.*" contentType="text/html; charset=gb2312"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<body>
<form action="ans02_1.jsp" method="post">
输入图书价格:<input name="money" type="text"><br>
<input type="submit" value="提交">
</form>
</body>
</html>
<%@ page language="java" import="java.sql.*" contentType="text/html; charset=gb2312"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<body>
<%
String money1=request.getParameter("money");
%>
<form action="ans02_2.jsp" method="post">
输入汇率:<input name="huilv" type="text"><br>
<input name="newmoney" type="hidden" value="<%=money1%>">
<input type="submit" value="提交">
</form>
</body>
</html>
<%@ page language="java" import="java.sql.*" contentType="text/html; charset=gb2312"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<body>
<%
String money2=request.getParameter("newmoney");
String huilv=request.getParameter("huilv");
Double newmoney2=Double.parseDouble(money2)/Double.parseDouble(huilv);
out.println("转换后的价格为:"+newmoney2);
%>
</body>
</html>
3.在登录页面中用户输人用户名和密码,如果两者相等,则登录成功,跳转到欢迎面面;如果不成功,则不跳转,并显示“登录错误”。
<%@ page language="java" contentType="text/html; charset=gb2312"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<body>
欢迎登录
<script type="text/javascript">
function validate(){
if(loginForm.account.value!=""&&loginForm.password.value!=""&&loginForm.account.value==loginForm.password.value){
alert("登录成功");
loginForm.submit();
return;
}else{
alert("登录错误");
return;
}
}
</script>
<form name="loginForm" action="ans03_1.jsp" >
请输入账号:<input name="account" type="text"><br>
请输入密码:<input name="password" type="password"><br>
<input type="button" value="登录" onClick="validate()">
</form>
</body>
</html>
<%@ page language="java" import="java.sql.*" contentType="text/html; charset=gb2312"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<body>
<%
out.println("欢迎登录!");
%>
</body>
</html>
4.用户访问首页,用一个下拉菜单选择背景颜色,提交,到达欢迎页面,背景颜色为用户选择的颜色。下次用户访问欢迎页面,直接显示该颜色,无须重新选择。
<%@ page language="java" import="java.util.*,javax.servlet.http.*" contentType="text/html; charset=gb2312"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<body>
<form action="ans04.jsp">
请选择背景颜色:
<select name="color">
<option value="white">白色</option>
<option value="blue">蓝色</option>
<option value="red">红色</option>
<option value="green">绿色</option>
<option value="yellow">黄色</option>
</select>
<input type="submit" value="提交">
</form>
<%
String color1=request.getParameter("color");
if(color1!=null){
Cookie cookie=new Cookie("bgcolor1",color1);
cookie.setMaxAge(60000);
response.addCookie(cookie);
response.sendRedirect("ans04_1.jsp");
}
%>
</body>
</html>
<%@ page language="java" import="java.sql.*,javax.servlet.http.*" contentType="text/html; charset=gb2312"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<body>
欢迎进入。。。。。
<%
String str=null;
Cookie[] cookies=request.getCookies();
for(int i=0;i<cookies.length;i++){
if(cookies[i].getName().equals("bgcolor1")){
str=cookies[i].getValue();
break;
}
}
%>
<script>
var str1="<%=str%>";
document.body.style.backgroundColor=str1;
</script>
</body>
</html>
5.在用户登录界面中输人账号和密码,让用户选择“是否保存登录状态”,如果账号和密码相符.则登录成功,进人欢迎页面。在登录时,如果保存了登录状态,下次登录时若访问登录页面,则进人欢迎页面,如果客户没有经过登录就访问欢迎页面,则跳转到登录页面。
<%@ page language="java" contentType="text/html; charset=gb2312"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<body>
<%
//首先检查
Cookie[] cookies = request.getCookies();
String account = "";
String password = "";
if (cookies != null && cookies.length != 0) {
for (int i = 0; i < cookies.length; i++) {
if (cookies[i] != null) {
if (cookies[i].getName().equals("account")) {
account = cookies[i].getValue();
}
}
if (cookies[i] != null) {
if (cookies[i].getName().equals("password")) {
password = cookies[i].getValue();
}
}
}
}
if (!account.equals("") && !(password.equals(""))
&& account.equals(password)) {
request.getRequestDispatcher("/ans03_1.jsp").forward(request,response);
} else {
%>
欢迎登录
<form name="loginForm" action="ans05_2.jsp">
请输入账号:<input name="account" type="text"><br> 请输入密码:<input
name="password" type="password"><br> 是否保存登录状态:<input
type="radio" name="saveLoginState" value="1" checked="checked">是
<input type="radio" name="saveLoginState" value="0">否<br>
<input type="submit" value="登录">
<%
}
%>
</form>
</body>
</html>
<%@ page language="java" contentType="text/html; charset=gb2312"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<%
String account = request.getParameter("account");
String password = request.getParameter("password");
String saveLoginState = request.getParameter("saveLoginState");
if (account != null && !(account.equals("")) && password != null
&& !(password.equals("")) && account.equals(password)) {
//若用户选择
if (saveLoginState != null && saveLoginState.equals("1")) {
Cookie aCookie = new Cookie("account", account);
response.addCookie(aCookie);
Cookie pCookie = new Cookie("password", password);
response.addCookie(pCookie);
}
response.sendRedirect("ans03_1.jsp");
} else {
response.sendRedirect("ans05_1.jsp");
}
%>
<body>
</body>
</html>