• Servlet编程实例 续2


    -----------------siwuxie095

       

       

       

       

       

       

       

    Servlet 跳转之请求的重定向

       

       

    继续完善登录实例,如下:

       

    login.jsp 不变,修改 LoginServlet,新建两个 JSP 文件

       

       

       

       

       

    此时工程结构目录一览:

       

       

       

       

       

    login.jsp:

       

    <%@ page language="java" contentType="text/html; charset=UTF-8"

    pageEncoding="UTF-8"%>

    <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">

    <html>

    <head>

    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">

    <title>登录页面</title>

    </head>

    <body>

       

    <!-- 添加表单,url在部署描述符中进行配置,使用post方式来提交 -->

    <form action="<%= request.getContextPath() %>/loginServlet" method="get">

    userName<input type="text" name="uname" /><br/>

    password<input type="password" name="upwd" /><br/><br/>

    <input type="submit" value="Login" />

    <input type="reset" value="Reset" />

    </form>

       

    </body>

    </html>

       

       

       

    success.jsp:

       

    <%@ page language="java" contentType="text/html; charset=UTF-8"

    pageEncoding="UTF-8"%>

    <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">

    <html>

    <head>

    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">

    <title>登录成功提示页面</title>

    </head>

    <body>

       

    登录成功!<br/>

    您提交的信息为:<br/>

    用户名:<%= request.getParameter("uname") %><br/>

    密码:<%= request.getParameter("upwd") %><br/>

    <a href="login.jsp">返回登录页面</a>

       

    </body>

    </html>

       

       

       

    error.jsp:

       

    <%@ page language="java" contentType="text/html; charset=UTF-8"

    pageEncoding="UTF-8"%>

    <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">

    <html>

    <head>

    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">

    <title>登录失败提示页面</title>

    </head>

    <body>

       

    登录失败!<br/>

    您提交的信息为:<br/>

    用户名:<%= request.getParameter("uname") %><br/>

    密码:<%= request.getParameter("upwd") %><br/>

    <a href="login.jsp">返回登录页面</a>

       

    </body>

    </html>

       

       

       

    LoginServlet.java:

       

    package com.siwuxie095.servlet;

       

    import java.io.IOException;

       

    import javax.servlet.ServletException;

    import javax.servlet.http.HttpServlet;

    import javax.servlet.http.HttpServletRequest;

    import javax.servlet.http.HttpServletResponse;

       

    // LoginServlet 继承自 HttpServlet

    public class LoginServlet extends HttpServlet {

    /**

    * 用于序列化和反序列化的 ID

    */

    private static final long serialVersionUID = -7740192486028671728L;

       

    //覆盖父类 HttpServlet doGet() 方法

    @Override

    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

    System.out.println("===== doGet =====");

    // doGet() 方法里调用 doPost() 方法

    //这样,GET请求和POST请求可以共用一套处理逻辑

    doPost(req, resp);

    }

       

    //覆盖父类 HttpServlet doPost() 方法

    @Override

    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

    System.out.println("===== doPost =====");

    String userName=req.getParameter("uname");

    String password=req.getParameter("upwd");

    System.out.println("用户名:"+userName);

    System.out.println("密码:"+password);

    if (userName.equals("siwuxie095")&&password.equals("8888")) {

    //使用resp对象的sendRedirect()方法,进行页面的重定向

    //使用重定向方式进入提示页面无法获取到之前提交的表单数据

    resp.sendRedirect(req.getContextPath()+"/success.jsp");

    }else {

    resp.sendRedirect(req.getContextPath()+"/error.jsp");

    }

    }

    }

       

       

       

    在部署描述符 web.xml 中注册 servlet:

       

    <?xml version="1.0" encoding="UTF-8"?>

    <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" version="3.1">

    <display-name>MyServlet</display-name>

    <welcome-file-list>

    <welcome-file>index.html</welcome-file>

    <welcome-file>index.htm</welcome-file>

    <welcome-file>index.jsp</welcome-file>

    <welcome-file>default.html</welcome-file>

    <welcome-file>default.htm</welcome-file>

    <welcome-file>default.jsp</welcome-file>

    </welcome-file-list>

    <servlet>

    <servlet-name>LoginServlet</servlet-name>

    <servlet-class>com.siwuxie095.servlet.LoginServlet</servlet-class>

    </servlet>

    <servlet-mapping>

    <servlet-name>LoginServlet</servlet-name>

    <!-- url-pattern form 表单中录入的 action 的值 -->

    <url-pattern>/loginServlet</url-pattern>

    </servlet-mapping>

    </web-app>

       

       

    部署描述符 web.xml 在 WEB-INF 目录下,如果没有,手动创建即可

       

    选择工程 MyServlet,右键->Java EE Tools->Generate Deployment Descriptor Stub

       

       

       

    访问:localhost:8080/MyServlet/login.jsp,分别输入 siwuxie095 和 8888

       

       

       

       

    重定向到:localhost:8080/MyServlet/success.jsp,用户名和密码都为 null

       

    即 使用重定向方式进入提示页面无法获取到之前提交的表单数据

       

       

       

       

       

    整个过程分析如下:

       

    1)首先访问 login.jsp

       

       

       

       

    (2)分别输入 siwuxie095 和 8888 后,点击 Login,出现两次 请求 和 响应

       

       

       

       

    其中 loginServlet?uname=siwuxie095&upwd=8888

       

       

       

    其中 success.jsp

       

       

       

       

    3)根据 请求信息 响应信息 可知:

       

    首先浏览器向 LoginServlet 发出请求信息

       

    LoginServlet 接收到请求信息后进行处理,向浏览器发送响应信息

       

    该响应信息没有包含任何具体的数据,只是在响应头的 Location 字段

    包含需要重定向的地址信息:success.jsp

       

    浏览器接收到响应信息后,自动向 Location 字段指定的地址发送请求信息,

    请求信息发送完毕后会收到相应的响应信息

       

       

    在整个重定向的过程中,会涉及两次 请求 响应。在第二次

    请求中并没有携带表单信息,所以在 success.jsp 页面,无法

    获取到 用户名 密码,显示为 null

       

       

       

       

       

       

       

    【made by siwuxie095】

  • 相关阅读:
    数据绘图工具之Matplotlib
    数据分析
    scrapy-redis 实现分布式爬虫
    存储库之MongoDB
    pycharm解释器链接如何pymongo
    爬虫之request相关请求
    爬虫基本原理
    Xadmin
    当网页失去焦点时改变网页的title值
    gulp基础使用及进阶
  • 原文地址:https://www.cnblogs.com/siwuxie095/p/6714204.html
Copyright © 2020-2023  润新知