一:禁止IP地址前缀为192的访问,写在JSP里面就可以了
<%
String address=request.getRemoteAddr();
String threew=address.substring(0,3);
if(threew.equals("192"))
out.print("抱歉,你被禁止访问!");
else
out.print("HELLO KIRIKA YE");
%>
2.通过请求实现的jsp页面间颜色传递
2.1 index.jsp
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>My JSP 'index.jsp' starting page</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
</head>
<body>
<form action="selectForm_result.jsp" method="post">
请选择下拉背景:
<select name="color">
<option value="blue" style="background-color:blue">蓝色</option>
<option value="gray" style="background-color:gray">灰色</option>
<option value="green"style="background-color:green">绿色</option>
</select>
<input type="submit" value="提交">
</form>
、
<a href="selectForm_result.jsp">提交</a>
</body>
</html>
2.2selectForm_result.jsp,设置饼干与存活时间
饼干和会话可以参考这两位写的
1:https://blog.csdn.net/liyifan687/article/details/80077928
2:https://www.cnblogs.com/xxtalhr/p/9053906.html
里面加了脚本与ID定位输出,和前端遥控器差不多
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8" import="java.uitl.*"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>My JSP 'selectForm_result.jsp' starting page</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
</head>
<%! String color="";%>
<%
color=request.getParameter("color");
out.print("+nihao+"+color);
Cookie cookie=new Cookie("color1",color);
cookie.setMaxAge(10000);
response.addCookie(cookie);
%>
<body id="a" style="background:<%=color%>">
变色开花
</body>
<script type="text/javascript">
<%-- var color="<%=color%>"; --%>
//使用javascript改变东西
var id=document.getElementById("a");//定位 到这个id
// id.style.background=color;script调用color
</script>
</html>
或者用这个
<body>
Kirika Ye选定一个颜色 <br>
<% String color=""; %>
<%
color=request.getParameter("color");
if(color==null){
Cookie[] cookies=request.getCookies();
for(int i=0;i<cookies.length;i++){
if(cookies[i].getName().equals("color")){
color=cookies[i].getValue();
break;
}
}
}
else{
Cookie cookie=new Cookie("color",color);
cookie.setMaxAge(360000);
response.addCookie(cookie);
}
%>
<script type="text/javascript">
document.body.style.backgroundColor="<%=color%>"
</script>
</body>
三:登录cookie的实现记录
机制可以看这里:https://www.cnblogs.com/tanzq/p/8075473.html
index1.jsp
<body>
This is my JSP page. <br>
<!-- cookie校验 -->
<%
String act=null;
String pwd=null;
/* 每次到这个页面都会先校验一遍,看看有没有cookie记录 */
Cookie[] cookies=request.getCookies();
if(cookies!=null){
for(int i=0;i< cookies.length;i++){
if(cookies[i].getName().equals("act"))
act=cookies[i].getValue();
else if(cookies[i].getName().equals("pwd"))
pwd=cookies[i].getValue();
if(act!=null&&pwd!=null)
break;
}
System.out.println(act+pwd);
if(act!=null&&pwd!=null){
/* if(act!=null&&pwd!=null&&act.equals(pwd)) */
response.sendRedirect("index2.jsp?act="+act+"&pwd="+pwd);
}
}
%>
<form method="post"action="index2.jsp">
请输入账号:<input name="act" type="text"><br/>
请输入密码:<input name="pwd" type="password"><br/>
<input type="submit" value="登录">
<input type="checkbox" name="save" value="yes">是否保存登录状态
</form>
</body>
index2.jsp
<body>
This is my JSP page. <br>
<%
String act=request.getParameter("act");
String pwd=request.getParameter("pwd");
if(act!=null&&pwd!=null)
out.print("登陆成功");
else
response.sendRedirect("index.jsp");
%>
<%
act=request.getParameter("act");
pwd=request.getParameter("pwd");
String save=request.getParameter("save");//判断是否勾选保存,勾选值为yes
if(act!=null&&pwd!=null){
/* 判断是否勾选复选框 */
if(save!=null&&save.equals("yes")){
Cookie cookie1=new Cookie("act",act);
Cookie cookie2=new Cookie("pwd",pwd);
cookie1.setMaxAge(360000);
cookie2.setMaxAge(360000);
response.addCookie(cookie1);
response.addCookie(cookie2);
}
}
%>
</body>