Home > Web Front-end > JS Tutorial > body text

How to redirect web pages in jsp

一个新手
Release: 2017-09-26 10:17:43
Original
2137 people have browsed it

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
Copy after login

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>
Copy after login

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>
Copy after login
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!

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!