This article describes the usage of response objects in Java. Share it for your reference, as follows:
The <jsp:forward> action element is used to end the execution of the current page on the server side when running and move from the current page to the specified page.
Use the setHeader() method of the response object to set the automatic refresh time interval of the page. The statement that implements reloading this page every 60 seconds is:
The code copy is as follows: response.setHeader("refresh",60);
After 3 seconds, the statement of the browser loading the new page //www.VeVB.COM is:
The code copy is as follows: response.setHeader("refresh","3;URL=//www.VeVB.COM");
The method of response: void sendRedirect(String url), redirects the page to the specified URL address.
Example: Use response to implement user login function
login.html is the login form page
login.jsp is an information processing page to verify whether the user login is successful.
success.jsp is the jump page after login is successful.
The source code of login.html is as follows:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"><html> <head> <title>Login function instance</title> <meta http-equiv="keywords" content="keyword1,keyword2,keyword3"> <meta http-equiv="description" content="this is my page"> <meta http-equiv="content-type" content="text/html; charset=UTF-8"> <!--<link rel="stylesheet" type="text/css" href="./styles.css">--> </head> <body> <center> <h1>Login interface</h1> <form action="login.jsp" method="post"> Name: <input type="text" name="name"><br> Password: <input type="password" name="pwd"><br> <input type="submit" name="submit" value="Login"> <input type="reset" name="reset" value="Reset"> </form> </center> </body></html>
The source code of login.jsp is as follows:
<%@ page language="java" import="java.util.*" contentType="text/html;charset=UTF-8"%><%String path = request.getContextPath();String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";%><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"><html> <head> <base href="<%=basePath%>"> <title>Login function instance</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> <center> <h1>Login function instance</h1> <% request.setCharacterEncoding("UTF-8"); String name = request.getParameter("name"); String pwd = request.getParameter("pwd"); if(name != null && pwd != null && name.equals("guanlin") && pwd.equals("123")) { //response.sendRedirect("success.jsp"); %> <jsp:forward page="success.jsp"></jsp:forward> <%}else { out.println("<font color='red'>Username or password is incorrect, return to the login page in 5 seconds. If you don't want to wait, please click <a href='response/login.html'>Return to login</a></font>"); response.setHeader("refresh","5;url = login.html"); } %> </center> </body></html>The source code of success.jsp is as follows:
<%@ page language="java" import="java.util.*" contentType="text/html;charset=UTF-8"%><%String path = request.getContextPath();String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";%><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"><html> <head> <base href="<%=basePath%>"> <title>Login function instance</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> <center> <h1 style="green">Login successfully!</h1> <% request.setCharacterEncoding("UTF-8"); String name = request.getParameter("name"); String pwd = request.getParameter("pwd"); %> The logged-in user name is: <%=name %><br> The password for logging in is: <%=pwd %> </center> </body></html>I hope this article will be helpful to everyone's Java programming.