• 请求转发与请求作用域


    创建input.jsp页面,通过表单输入学号、姓名后,单击登录按钮,控制转发到FirstServlet对其进行处理,然后通过请求对象的getRequestDispartcher()获得RequestDispartcher对象,将请求转发至SecondServlet,在SecondServlet中用输出流对象out输出该学生的学号和姓名。输出页面还包含一个超链接,单击该链接可以返回至input.jsp页面。

    1. <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
    2. <%
    3. String path = request.getContextPath();
    4. String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
    5. %>
    6. login.jsp
    7. DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
    8. ForwordJsp.jsp
    9. this is students form:
    10. 学号:

    11. 姓名:

    1. package com.demo;
    2. import java.io.IOException;
    3. import java.io.PrintWriter;
    4. import java.util.List;
    5. import java.util.regex.Pattern;
    6. import javax.servlet.ServletException;
    7. import javax.servlet.annotation.WebServlet;
    8. import javax.servlet.http.HttpServlet;
    9. import javax.servlet.http.HttpServletRequest;
    10. import javax.servlet.http.HttpServletResponse;
    11. @WebServlet("/getMethod")
    12. public class SecondServlet extends HttpServlet {
    13. private static final long serialVersionUID = 1L;
    14. public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    15. response.setContentType("text/html;charset=utf-8");
    16. PrintWriter out = response.getWriter();
    17. Student stu = (Student) request.getAttribute("stu");
    18. String id = stu.getId();
    19. String name = stu.getName();
    20. out.println("");
    21. out.println("");
    22. out.println(" result");
    23. out.println(" ");
    24. out.print(" 学号: "+id + "
      "
      );
    25. out.print(" 姓名: "+name + "
      "
      );
    26. out.print("");
    27. out.println(" ");
    28. out.println("");
    29. out.flush();
    30. out.close();
    31. }
    32. public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    33. this.doGet(request, response);
    34. }
    35. }
    1. package com.demo;
    2. import java.io.IOException;
    3. import java.io.PrintWriter;
    4. import java.util.Enumeration;
    5. import java.util.List;
    6. import javax.json.Json;
    7. import javax.json.JsonArray;
    8. import javax.json.JsonObject;
    9. import javax.servlet.ServletException;
    10. import javax.servlet.annotation.WebServlet;
    11. import javax.servlet.http.HttpServlet;
    12. import javax.servlet.http.HttpServletRequest;
    13. import javax.servlet.http.HttpServletResponse;
    14. @WebServlet("/login")
    15. public class FirstServlet extends HttpServlet {
    16. private static final long serialVersionUID = 1L;
    17. public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    18. this.doPost(request, response);
    19. }
    20. public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    21. response.setContentType("text/html;charset=UTF-8");
    22. PrintWriter out = response.getWriter();
    23. String id = request.getParameter("id");
    24. String name = request.getParameter("name");
    25. request.setAttribute("stu",new Student(id, name));
    26. System.out.println("成功 == "+ id +" == "+name);
    27. request.getRequestDispatcher("/SecondServlet.do").forward(request, response);
    28. out.flush();
    29. out.close();
    30. }
    31. }

  • 相关阅读:
    nginx核心板块来构建静态服务器三
    观察者模式和发布订阅模式的区别
    如何将不同类别信息发送到kafka的不同通道中
    expect自动化交互应用程序工具
    【若依(ruoyi)】设置主题样式
    Html5API(自定义属性、媒体元素、canvas画布)(一)
    Istio-PilotAgent源码分析
    Nginx入门到精通2-nginx框架
    JK触发器与模12计数器
    码云/GitHub Fork代码仓并提交PR代码
  • 原文地址:https://blog.csdn.net/m0_62110645/article/details/133530573