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")
驾云归来