初识jsp与servlet数据传输方式

时间:2021-10-13 13:41:15
1、servlet传到jsp
servlet端
request.setAttribute( "name" , "user" );
request .setAttribute( "password" , "123" );
//传递到main.jsp页面
request.getRequestDispatcher( "main.jsp" ).forward(request, response);
jsp页面
<%= request.getAttribute( "name" ) %>
<%= request.getAttribute( "password" ) %>

2、jsp传到servlet
  • 通过超链接传值
<a href="FirstServ?type=one&name=name">链接</a>//在jsp中发送type的值
String type = request.getParameter("type");//在servlet接受type的值
String type = request.getParameter("name");
  • 表单传值
//jsp
< form action = "main" method = "post" >
名称: < input type = "text" name = "name" >
密码: < input type = "text" name = "password" >
< input type = "submit" >

//servlet
String name = request.getParameter( "name" );
String pa = request. getParameter ( "password" );