1.使用中间过渡的JSP,使用JSP:setProperty和JSP:forward标签
登陆页面没有什么特别,只是form的action属性设置成过渡页面savebean.jsp
savebean.jsp
<jsp:useBean id="info" scope="session" class="untitled2.Infoo">
</jsp:useBean>
<jsp:setProperty name="info" property="*"/>
<%
request.setAttribute("info",info);
%>
<jsp:forward page="/cookie"/>
其中/cookie为处理逻辑的Servlet
cookie中得到封装好的Bean,取其中的属性
Infoo i=(Infoo)request.getAttribute("info"); (Infoo为Bean类)
2.不使用中间过渡JSP
只需要加上
<%
request.setAttribute("info",info);
if(request.getParameter("action")!=null&&request.getParameter 
("action").equals("4"))...{
RequestDispatcher dis=request.getRequestDispatcher("/cookie");
dis.forward(request,response);
}
%>
form表单提交到自身,但加上action=4的参数,如果判断参数为4,则说明表单已
经提交,Bean封装成功,需要跳转到Servlet,此处要使用forward,不使用
SendRedirect跳转,是因为要把保存Bean信息的Request一同转到Servlet中
Infoo i=new Infoo();

try ...{
BeanUtils.populate(i,request.getParameterMap());

} catch (Exception ex) ...{
ex.printStackTrace();
}
经提交,Bean封装成功,需要跳转到Servlet,此处要使用forward,不使用
SendRedirect跳转,是因为要把保存Bean信息的Request一同转到Servlet中
3.使用Jakarta提供的BeanUtils包,此方法无需对JSP页面做任何处理
Servlet中
Infoo i=new Infoo();
try ...{
BeanUtils.populate(i,request.getParameterMap());
} catch (Exception ex) ...{
ex.printStackTrace();
} 
JSP与Servlet数据交互
本文介绍了三种在JSP和Servlet之间传递数据的方法:使用中间过渡JSP、直接在JSP中处理请求以及利用Jakarta BeanUtils包。通过实例展示了如何在实际应用中实现这些方法。
1111

被折叠的 条评论
为什么被折叠?



