• Servlet表单提交、前后端数据交换、页面跳转


    🌟有这么一个实验任务:

    (1)要求写一个html页面,其中包含表单,需要输入学号和密码,并且有提交和重置按钮

    (2)建立一个Student实体类来辅助数据的传输

    (3)在点击提交按钮后页面将携带着数据跳转到另一页面,并完成数据的展示

    显而易见的,这个实验包含了Servlet表单提交、前后端数据交换、页面跳转等操作。别急,我们按流程一步步的实现。


    🌟第一步:创建input.html页面中的表单

    1. <form action="./formServlet" method="post" >
    2. <label>学号label><input name="sno" id="sno"><br>
    3. 姓名<input name="name" id="name"><br>
    4. <input type="submit" value="提交" >
    5. <input type="reset" value="重置" ">
    6. form>

    对于其中的

    "/你要运行的servlet" method="post"

    (1)action后面的参数代表:当我们按下提交按钮后程序会执行 " /你要运行的servlet " 应映射到的Servlet类

    (2)action后面跟的是./的原因:如果映射表中写的是/formServlet,action就一定要跟./

    因为指定了formServlet在war_explored目录下,使用./来表示查找当前目录下的servlet

    ​​​​​​​

    (3)method后面的参数一般是post或者get。post相对于更加安全,如果使用了get那么输入的信息会直接显示在src中

    (4)我们在最后两个input中指定了他们分别是submit/reset,所以他们已经对应有自己的原生处理操作

    🌟第二步:编写Student实体类

    1. public class Student {
    2. private String sno = null;
    3. private String name = null;
    4. public String getSno() {
    5. return sno;
    6. }
    7. public void setSno(String sno) {
    8. this.sno = sno;
    9. }
    10. public String getName() {
    11. return name;
    12. }
    13. public void setName(String name) {
    14. this.name = name;
    15. }
    16. }

    需要包含getter和setter方法,用来存取数据 

    🌟第三步 :编写input.html中启用的formServlet类

    别急,慢慢来,这一步是重点! 

    1. public class StudentServlet extends HttpServlet{
    2. @Override
    3. protected void doGet(HttpServletRequest request, HttpServletResponse response)
    4. throws ServletException, IOException {
    5. String name = request.getParameter("name");
    6. String sno = request.getParameter("sno");
    7. Student student = new Student();
    8. student.setName(name);
    9. student.setSno(sno);
    10. //把完成初始化的student对象保存在session中,使得前端可以拿到这个student对象
    11. request.getSession().setAttribute("student", student);
    12. //跳转到页面
    13. request.getRequestDispatcher("/showStudentInfo").forward(request, response);
    14. }
    15. @Override
    16. protected void doPost(HttpServletRequest request, HttpServletResponse response)
    17. throws ServletException, IOException {
    18. doGet( request, response);
    19. }
    20. }

    我们在处理表单的时候分为以下步骤:

    (1) 我们需要继承HttpServlet并重写其中的doPost和doGet方法,因为我们在表单中选择的提交method是post,所以我们需要使用doPost调用doGet来完成操作

    (2)通过String name = request.getParameter("name");来获取表单中输入名为name的数据,通过String sno = request.getParameter("sno");来获取表单中输入名为sno的数据

    (3)利用request.getSession().setAttribute("student", student);完成初始化的student对象保存在session中,使得前端可以拿到这个student对象

    (4)利用request.getRequestDispatcher("/showStudentInfo").forward(request, response);实现页面的跳转并且将request和reponse一起传过去实现数据的传输(因为student在session中,session在request中)

    (5)补充第四点,跳转到的页面是映射路径为("/showStudentInfo")的页面

    啊,那这里又有人要问啦,不是说formServlet类吗?你怎么编写了一个StudentServlet类!

    没错,我是编写的StudentServlet类,但是实际上html表单对应的 action:/formServlet 是路径名,程序看到你提交了表单之后会去映射表中找路径名对应的映射名再去对应的类,所以我们只需要将映射名写成formServlet就好了。

    🌟第四步:编写信息显示页面

    1. public class showStudentInfo extends HttpServlet {
    2. @Override
    3. protected void doPost(HttpServletRequest request, HttpServletResponse response)
    4. throws ServletException, IOException {
    5. Student student = (Student)request.getSession().getAttribute("student");
    6. response.setContentType("text/html;charset=gb2312");
    7. PrintWriter out = response.getWriter();
    8. out.println("");
    9. out.println( "学号"+student.getSno()+"
      "
      );
    10. out.println( "姓名"+student.getName()+"
      "
      );
    11. out.println("");
    12. }
    13. }

    我们从request中向下转型拿到了student然后使用PrintWriter实现页面的输出。 

    🌟第五步:web.xml的配置编写

    上面的命名你可能有疑惑,在这一步会解释命名的作用以及程序读取命名的流程。 

    其实我们实现了两个servlet,一个用来读取表单并跳转,另一个用来显示数据。所以我们总共会有两个servlet标签和两个servlet-mapping(映射)的标签。

    子标签中servlet-name的作用是让url-pattern找的到servlet-class,url-pattern才是前端表单action的查找对象。

    比如:当前端查找/formServlet,他找到了/formServlet之后发现他的servlet-name是formServlet。进而查找到formServlet对应的类是com.example.bookstore.StudentServlet,从而再去运行这个类。

    1. <servlet>
    2. <servlet-name>formServletservlet-name>
    3. <servlet-class>com.example.bookstore.StudentServletservlet-class>
    4. servlet>
    5. <servlet-mapping>
    6. <servlet-name>formServletservlet-name>
    7. <url-pattern>/formServleturl-pattern>
    8. servlet-mapping>
    9. <servlet>
    10. <servlet-name>showStudentInfoservlet-name>
    11. <servlet-class>com.example.bookstore.showStudentInfoservlet-class>
    12. servlet>
    13. <servlet-mapping>
    14. <servlet-name>showStudentInfoservlet-name>
    15. <url-pattern>/showStudentInfourl-pattern>
    16. servlet-mapping>

      End Of The Paper   

  • 相关阅读:
    torch.zeros
    每日一记项目,拼图小游戏
    nn.PairwiseDistance 和 torch.cdist 和 MSELoss 计算距离
    JSP表达式(EL)
    基于蒙特卡洛法的规模化电动车有序充放电及负荷预测(Python&Matlab实现)
    Flutter安装或者更新包没反应
    劲爆,Java 协程终于来了
    算法leetcode|剑指 Offer 27. 二叉树的镜像|226. 翻转二叉树(rust很强)
    【无标题】接口测试遇到的典型bug纪录
    Flink 读写 Ceph S3入门学习总结
  • 原文地址:https://blog.csdn.net/m0_56190554/article/details/127765594