Intellij idea/Servlet学习之-在Servlet中实现页面重定向

文章内容:实验效果展示+Servlet类的实现代码+Web.xml中Servlet的配置代码+JSP代码(对该效果的实现说明也在代码中有注释

第一部分:实现效果展示

登录窗口(用户名:mr;密码:123456):


点击登录,如果输入密码正确出现下面窗口


如果输入密码不正确出现下面窗口:




第二部分:创建名为RedirectServlet的Servlet类,代码如下:

/**
 * Created by Administrator on 2017/7/28.
 * 功能:在Servlet中实现页面重定向
 * 实现技术:使用HttpServletResponse对象的sendRedirect()方法,该方法和页面转发的forward()方法有本质区别。
 * *********forward()方法会将当前正在处理的请求转发到其他web组件(Servlet/JSP/HTML)。
 * 注意:SendRedirect()方法不会转发请求,只是简单的页面跳转。
 */
import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;


public class RedirectServlet extends HttpServlet{
    public RedirectServlet() {
        super();
    }

    public void destroy() {
        super.destroy(); // Just puts "destroy" string in log
        // Put your code here
    }

    public void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {

        this.doPost(request, response);
    }

    public void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        request.setCharacterEncoding("UTF-8");    //设置请求数据的字符编码格式
        String name = request.getParameter("name"); //获得请求表单中的用户名
        String pwd = request.getParameter("pwd");  //获得请求表单中的密码
        if((name!=null&&!name.equals(""))&&(pwd!=null&&!pwd.equals(""))){
            if(name.equals("mr")&&pwd.equals("123456")){
                //使用RequestDispatcher对象将页面请求转发到success.jsp页
                request.getRequestDispatcher("success.jsp").forward(request, response);
            }else{
                //使用sendRedirect()方法将页面重定向到error.jsp
                response.sendRedirect("error.jsp");
            }
        }
    }
    public void init() throws ServletException {
        // Put your code here
    }
}


第三部分:在Web.xml中写入Servlet类的配置代码,写在<web-app>标签里面,代码如下:

<servlet>
    <servlet-name>RedirectServlet</servlet-name>
    <!--自定义的servlet名-->
    <servlet-class>RedirectServlet</servlet-class>
    <!--具体的类路径-->
</servlet>

<servlet-mapping>
    <servlet-name>RedirectServlet</servlet-name>
    <!--自定义的servlet名,与上面的name一致-->
    <url-pattern>/forward</url-pattern>
    <!--对应网页地址中具体的servlet名-->
    <!--<url-pattern>/WebClient/ForwardServlet</url-pattern>-->
</servlet-mapping>

第四部分:新建success.jsp和error.jsp。然后在index.jsp中加入代码。

success.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 'success.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">
    -->
    <style type="text/css">
        table{
            font-size:12px;
            font-family: 隶书;
            color:gray;
            border: 1px green solid;
        }
        input{
            font-size:12px;
            font-family: 隶书;
            color:gray;
        }
    </style>
</head>

<body>
<table align="center">
    <tr>
        <td>
            <font color="green">
                恭喜您【<%=request.getParameter("name")%>】,登录成功!
            </font>
        </td>
    </tr>
</table>

</body>
</html>

error.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 'error.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">
    -->
    <style type="text/css">
        table{
            font-size:15px;
            font-family: 隶书;
            color:gray;
            border: 1px green solid;
        }
        input{
            font-size:12px;
            font-family: 隶书;
            color:gray;
        }
    </style>
</head>

<body>
<table align="center">
    <tr>
        <td>

            <font color="red">
                登录失败!用户名或密码错误!
            </font>
        </td>
    </tr>
</table>
</body>
</html>



index.jsp代码如下:

<%@ page pageEncoding="UTF-8" import="java.util.*" language="java" %>
<% String path=request.getContextPath();%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
  <title>用户登录</title>
</head>
<body>
<center>
  <p>用户登录</p>
</center>
<form action="forward" method="post">
  <table border="1" width="250px;" align="center">
    <tr>
      <td width="75px">用户名:</td>
      <td><input type="text" name="name"></td>
    </tr>
    <tr>
      <td width="75px">&nbsp;&nbsp;码:</td>
      <td><input name="pwd" type="password"></td>
    </tr>
    <tr>
      <td colspan="2">
        <input type="submit" value="登录"/>&nbsp;&nbsp;
      </td>
    </tr>
  </table>
</form>
</body>
</html>


学无止境!



















©️2020 CSDN 皮肤主题: 大白 设计师:CSDN官方博客 返回首页