zoukankan      html  css  js  c++  java
  • jsp和servlet之间传数据

    一、jsp与java文件传递数据可以使用Servlet类来传递,jsp将数据存入到request对象中,Servlet类获取这个request对象,并将数据取出。
      示例代码如下:
      JSP代码:

    <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
    <html>
      <head>
        <title>Demo</title>
      </head>
      <body>
          <form action="/demoServlet" method="post">
              <input type="text" name="name"/>
              <input type="submit" value="提交"/>
          </form>
      </body>
    </html>

      Servlet代码:

    public class DemoServlet extends HttpServlet {
        public void doGet(HttpServletRequest request, HttpServletResponse response)
                throws ServletException, IOException {
            String name = request.getParameter("name");//获取jsp页面输入的参数
            System.out.println(name);
        }
        public void doPost(HttpServletRequest request, HttpServletResponse response)
                throws ServletException, IOException {
            doGet(request, response);
        }
    }

      表单提交之后,调用Servlet类的方法,通过request对象获取jsp页面传入的参数值,实现数据的传递。

    二、jsp接收servlet传过来的数据

    =request.getAttribute("a")
  • 相关阅读:
    【设计模式】策略模式
    【设计模式】模板方法模式
    【C++】《Effective C++》第五章
    【C++】《Effective C++》第四章
    free命令详解(内存)
    top命令详解(动态进程)
    ps命令详解(静态进程)
    SpringBoot_集成Redis
    SpringBoot_热部署插件
    SpringBoot_实现RESTfull API
  • 原文地址:https://www.cnblogs.com/nickup/p/8999859.html
Copyright © 2011-2022 走看看