1.浏览器?方式传递参数
/**
浏览器地址栏输入?方式传递参数
?test=123
*/
可以用${param.test}方式输出
2.页面内部设置参数setAttribute
/**
JSP页面中输入
<% request.setAttribute("test2", "1234");%>
传递参数
*/
可以在页面代码中输入以下方式输出:
${requestScope.test2}
<%=request.getAttribute("test2") %>
3.servlet设置request参数的访问:
import java.io.IOException; import javax.servlet.RequestDispatcher;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse; public class TestRequest extends HttpServlet{
void TestRequest(){
}
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
String test2=new String("aaaaa");
request.setAttribute("test2", test2); RequestDispatcher dispatch = request.getRequestDispatcher("/index.jsp"); /*设置跳转页面*/
dispatch.forward(request, response); /*转发request数据到目标页*/
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doGet(request, response);
}
}
servlet代码
/*JSP代码*/
<body>
This is my JSP page. <br>
${param.test2}
<%=request.getParameter("test2") %>
${requestScope.test2}
<%=request.getAttribute("test2") %>
</body>
/**
访问网址
http://localhost:8080/Test/TestRequest?test2=3333
后运行结果
*/
This is my JSP page.
3333 123456 123456
也就是说
${param.和<%=request.访问url传递的参数
${requestScope.和<%=request.访问的是servlet传递的参数
KEY:
关键 ${requestScope.*},是jsp内置对象,和request.getAttribute相似,都是取得现有的后台的数据