บทความนี้อธิบายถึงการใช้วัตถุตอบสนองใน Java แบ่งปันสำหรับการอ้างอิงของคุณดังนี้:
องค์ประกอบการดำเนินการ <jsp: forward> ใช้เพื่อยุติการดำเนินการของหน้าปัจจุบันทางฝั่งเซิร์ฟเวอร์เมื่อเรียกใช้และย้ายจากหน้าปัจจุบันไปยังหน้าระบุ
ใช้วิธีการ setheader () ของวัตถุตอบสนองเพื่อตั้งค่าช่วงเวลาการรีเฟรชอัตโนมัติของหน้า คำสั่งที่ใช้การโหลดหน้านี้ซ้ำทุก ๆ 60 วินาทีคือ:
การคัดลอกรหัสมีดังนี้: Response.Setheader ("Refresh", 60);
หลังจาก 3 วินาทีคำสั่งของเบราว์เซอร์กำลังโหลดหน้าใหม่ //www.vevb.com คือ:
การคัดลอกรหัสมีดังนี้: Response.Setheader ("Refresh", "3; url = // www.vevb.com");
วิธีการตอบสนอง: โมฆะ sendredirect (url string), เปลี่ยนเส้นทางหน้าไปยังที่อยู่ URL ที่ระบุ
ตัวอย่าง: ใช้การตอบสนองเพื่อใช้ฟังก์ชันการเข้าสู่ระบบของผู้ใช้
login.html เป็นหน้าแบบฟอร์มการเข้าสู่ระบบ
login.jsp เป็นหน้าการประมวลผลข้อมูลเพื่อตรวจสอบว่าการเข้าสู่ระบบของผู้ใช้สำเร็จหรือไม่
Success.jsp เป็นหน้ากระโดดหลังจากเข้าสู่ระบบสำเร็จ
ซอร์สโค้ดของ login.html มีดังนี้:
<! doctype html public "-// w3c // dtd html 4.01 transitional // en"> <html> <head> <title> อินสแตนซ์ฟังก์ชั่นการเข้าสู่ระบบ </title> <meta http-equiv = "คำหลัก" http-equiv = "content-type" content = "text/html; charset = utf-8"> <!-<link rel = "stylesheet" type = "text/css" href = "./ styles.css">-> </head> type = "text" name = "name"> <br> รหัสผ่าน: <input type = "password" name = "pwd"> <br> <input type = "ส่ง" name = "subment" value = "เข้าสู่ระบบ"> <อินพุต type = "reset" name = "reset" value = "Reset"> </form>
ซอร์สโค้ดของ login.jsp มีดังนี้:
<%@ page language = "java" import = "java.util.*" contentType = "text/html; charset = utf-8"%> <%String Path = request.getContextPath (); string basepath = request.getScheme ()+": //" html public "-// w3c // dtd html 4.01 transitional // en"> <html> <head> <base href = "<%= basepath%>"> <title> อินสแตนซ์ฟังก์ชั่นการเข้าสู่ระบบ </title> <meta http-equiv = "pragma" content = "no-cache"> <meta http-equiv = "Expires" content = "0"> <meta http-equiv = "คำหลัก" content = "keyword1, keyword2, keyword3"> <meta http-equiv = "คำอธิบาย" href = "styles.css">-> </head> <body> <center> <h1> อินสแตนซ์ฟังก์ชั่นการเข้าสู่ระบบ </h1> <% request.setcharacterencoding ("UTF-8"); ชื่อสตริง = request.getParameter ("ชื่อ"); สตริง pwd = request.getParameter ("PWD"); if (name! = null && pwd! = null && name.equals ("guanlin") && pwd.equals ("123")) {//response.sendretirect("success.jsp "); %> <jsp: forward page = "success.jsp"> </jsp: forward> <%} else {out.println ("<font color = 'red'> ชื่อผู้ใช้หรือรหัสผ่านไม่ถูกต้องให้กลับไปที่หน้าเข้าสู่ระบบใน 5 วินาทีหากคุณไม่ต้องการรอโปรดคลิก <a href = ' Response.Setheader ("Refresh", "5; url = login.html"); } %> </enter> </body> </html>ซอร์สโค้ดของ Success.jsp มีดังนี้:
<%@ page language = "java" import = "java.util.*" contentType = "text/html; charset = utf-8"%> <%String Path = request.getContextPath (); string basepath = request.getScheme ()+": //" html public "-// w3c // dtd html 4.01 transitional // en"> <html> <head> <base href = "<%= basepath%>"> <title> อินสแตนซ์ฟังก์ชั่นการเข้าสู่ระบบ </title> <meta http-equiv = "pragma" content = "no-cache"> <meta http-equiv = "Expires" content = "0"> <meta http-equiv = "คำหลัก" content = "keyword1, keyword2, keyword3"> <meta http-equiv = "คำอธิบาย" href = "styles.css">-> </head> <body> <center> <h1 style = "green"> เข้าสู่ระบบได้สำเร็จ! </h1> <% request.Setcharacterencoding ("UTF-8"); ชื่อสตริง = request.getParameter ("ชื่อ"); สตริง pwd = request.getParameter ("PWD"); %> ชื่อผู้ใช้ที่เข้าสู่ระบบคือ: < %= ชื่อ %> <br> รหัสผ่านสำหรับการเข้าสู่ระบบคือ: < %= pwd %> </enter> </body> </html>ฉันหวังว่าบทความนี้จะเป็นประโยชน์กับการเขียนโปรแกรม Java ของทุกคน