【javaweb】筆記

一:禁止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>

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章