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中
3.使用Jakarta提供的BeanUtils包,此方法無需對JSP頁面做任何處理 Servlet中 Infoo i=new Infoo();
try ...{
BeanUtils.populate(i,request.getParameterMap());
} catch (Exception ex) ...{
ex.printStackTrace();
}