Home >Web Front-end >JS Tutorial >How to redirect web pages in jsp

How to redirect web pages in jsp

一个新手
一个新手Original
2017-09-26 10:17:432162browse

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 &#39;login.jsp&#39; 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 &#39;receive.jsp&#39; 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>

correct.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 &#39;correct.jsp&#39; 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>

Running result (correct):




## Running result (error):




##

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!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn