jsp邮箱登录页面及response对象的转发和重定向

jsp邮箱登录页面;

 

首先我们新建一个jsp,写邮箱登录页面;

<%@ page language="java" import="java.util.*" pageEncoding="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>My JSP 'index.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="reg"method="post">
        邮箱:<input type="text" name="email" /><br />
        密码:<input type="password" name="pwd" /><br /> 
            <input type="submit" value="登录" />
    </form>
</body>
</html>

效果如下;

jsp邮箱登录页面及response对象的转发和重定向

 

接下来我们试着用getParameter方法获取到我们输入的值,新建reg.jsp,在reg中显示出来;

<%@ page language="java" import="java.util.*" pageEncoding="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>My JSP 'reg.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 email = request.getParameter("email");
   out.println("你的邮箱是:"+email);
   String pwd = request.getParameter("pwd");
   out.println("你的密码是:"+pwd);
   %>
    <br>
  </body>
</html>

效果如下;

jsp邮箱登录页面及response对象的转发和重定向

 response对象的转发和重定向;

转发(forward):直接转发;客户端和浏览器只发出一次请求,Servlet、HTML、jsp或其他信息资源,由第一个信息资源响应请求,在请求对象request中,保存的对象对于每个信息资源是共享的。

重定向(redirect):简介转发;实际是两次请求,服务器端在响应第一次请求的时候,让浏览器再向另一个URL发出请求,从而打到转发的目的。

jsp邮箱登录页面及response对象的转发和重定向

 

还是很难理解的,我们用一段代码来间接的感受一下,在刚才的邮箱登录代码的基础上改一下

index.jsp代码;

<%@ page language="java" import="java.util.*" pageEncoding="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>My JSP 'index.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>
        <% 
        //定义一个msg用来接收错误信息
        String msg = "";
        msg = (String)request.getAttribute("msg");
        if(msg==null){
        msg = "";
        }
        %>    
    <form action="reg.jsp"method="post">
        邮箱:<input type="text" name="name" /><br />
        密码:<input type="password" name="pwd" /><br /> 
        <%= msg %>
            <input type="submit" value="登录" />
    </form>
</body>
</html>

reg.jsp代码

失败就是转发;成功则是重定向。

<%@ page language="java" import="java.util.*" pageEncoding="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>My JSP 'reg.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 pwd = request.getParameter("pwd");
        String name = request.getParameter("name");
    //因为这里只为示范就不连接数据库了
    //直接定值
    if("lisi".equals(name) && "123456".equals(pwd)){
    //如果正确则跳到成功页面succes.jsp
    //重定向
    response.sendRedirect("succes.jsp");
    }else{
    //如果失败则跳转到登录页面,并且输出错误信息
    //转发
    request.setAttribute("msg", "密码错误!");
    request.getRequestDispatcher("index.jsp").forward(request, response);
    }
    
     %>
    <br>
  </body>
</html>

 

succes.jsp代码

<%@ page language="java" import="java.util.*" pageEncoding="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>My JSP 'email.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>
    <h1 style="color: green;text-align: center;" >登录成功!</h1>
  </body>
</html>

下面是效果图;

首先是登录页面的效果图;

jsp邮箱登录页面及response对象的转发和重定向

 

这里的域名是http://localhost:8080/Text/index.jsp

接下来是登录成功的截图;

jsp邮箱登录页面及response对象的转发和重定向

这里的域名为http://localhost:8080/Text/succes.jsp

接下来是登录失败的截图;

jsp邮箱登录页面及response对象的转发和重定向

可是这里的域名却为http://localhost:8080/Text/reg.jsp

但是我们的代码确实是写的跳到index.jsp,可是却显示的是reg.jsp

  //因为这里只为示范就不连接数据库了
    //直接定值
    if("lisi".equals(name) && "123456".equals(pwd)){
    //如果正确则跳到成功页面succes.jsp
    //重定向
    response.sendRedirect("succes.jsp");
    }else{
    //如果失败则跳转到登录页面,并且输出错误信息
    //转发
    request.setAttribute("msg", "密码错误!");
    request.getRequestDispatcher("index.jsp").forward(request, response);

这就像A找B借钱,可B没有,所以B找C借,但是A不知道B找C借钱。那么A还钱只会还给B不会还给C,再由B还给C。

所以这里等于reg.jsp转发了index.jsp的页面内容但是域名还是自己的;类似于我们的qq转发的说说类似;

如果还有不解,可找作者私聊