Use the sendRedirect() method in the request object to redirect (webpage jump) to another page.
Format: request.sendRedirect("*.jsp");
With forwarding
Example: The user enters the login name and password on the login interface, and jumps to different pages if the input is correct or incorrect.
Code:
login.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 'login.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="receive.jsp" method="post"> 姓名:<input type="text" name="rdname"><br> 密码:<input type="text" name="rdpasswd"><br> <input type="submit" value="确定"> </form> </body> </html
receive.jsp
##
<%@ page language="java" import="java.util.*" pageEncoding="ISO-8859-1"%> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <title>My JSP 'receive.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> <% String name=request.getParameter("rdname"); String passwd=request.getParameter("rdpasswd"); if(name.equals("abcd")&&passwd.equals("123456")){ %> <jsp:forward page="correct.jsp"/> <%}else{%> <% response.sendRedirect("http://sohu.com");}%> </body> </html>
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <title>My JSP 'correct.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> <% String name=request.getParameter("rdname"); %> 欢迎,<%=name %>成功登陆! </body> </html>
##
The above is the detailed content of How to redirect web pages in jsp. For more information, please follow other related articles on the PHP Chinese website!