一、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")