1.首先建立一個(gè)html頁(yè)收集輸入,傻子都會(huì),傳遞參數(shù)為color
2.然后Servlet 收集參數(shù),并以參數(shù)通過(guò)JavaBean建立一個(gè)實(shí)例 Bean object =new Bean(color);
3.接著通過(guò)JavaBean的get方法返回color, Servlet 進(jìn)行判斷.//equals() method. object.getColor();
4.servlet 判斷后轉(zhuǎn)到相應(yīng)的jsp頁(yè)面
String addr=...
核心方法: reqest.setAttribute("Instance", object);
RequestDispatcher dispatcher=request.getRequestDispatcher(addr); dispatcher .forward(request, response);
5.jsp 頁(yè)面根據(jù)前面建立的實(shí)例讀取相應(yīng)的String. <jsp:useBean id="Instance" type="where is bean and name" scope="request"/> <jsp:getProperty name="Instance" property="String of getXXX() ‘s XXX ,here is String"/>
這個(gè)程序開始是按書上抄的,后來(lái)完全是自己寫的
這下面的代碼還不是很理解,到底建立了幾個(gè)對(duì)象?那個(gè)是實(shí)例?特別是setAttribute("Red", color); 里是用前面的替換后邊的嗎?
String tempColor=request.getParameter("color"); ColorBean color=new ColorBean(tempColor);
request.setAttribute("Red", color); dispatcher.forward(request, response);
現(xiàn)在可以實(shí)現(xiàn)mvc架構(gòu)了,但是,其中對(duì)象的產(chǎn)生過(guò)程還是不理解,對(duì)象啊,對(duì)象......
下邊是源代碼:
其中 Input.jsp 為收集顏色(string)頁(yè)面 WantColor.java 為servlet ColorBean.java 為JavaBean 其它的是三個(gè)jsp 頁(yè)面用來(lái)連接javaBean 顯示顏色
越來(lái)覺(jué)得老外的書經(jīng)典了,為什么我不是美國(guó)人呢? *********** //input.jsp <%@ page contentType="text/html;charset=GB2312"%> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=GB2312"> <title>請(qǐng)輸入你想要的顏色</title> </head> <body> <form action="./servlet/WantColors" type=post> <input type=text name=color> <input type=submit value=submit> </form> </body> </html> ************** //wantColors.java package moonsoft.test.j2ee.wantColors; import javax.servlet.*; import javax.servlet.http.*; import java.io.PrintWriter; import java.io.IOException; public class WantColors extends HttpServlet { private static final String CONTENT_TYPE = "text/html; charset=Big5"; public void init(ServletConfig config) throws ServletException { super.init(config); } public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { String tempColor=request.getParameter("color"); ColorBean color=new ColorBean(tempColor); if((color.getColor()==null)&&(color.getColor()).equals("")) { addr="../input.jsp"; response.sendRedirect(""); } else if(color.getColor().equals("red")) { request.setAttribute("Red", color); addr="../red.jsp"; } else if(color.getColor().equals("green")) { request.setAttribute("Green",color ); addr="../green.jsp"; } else { request.setAttribute("Blue",color ); addr="../blue.jsp"; } RequestDispatcher dispatcher=request.getRequestDispatcher(addr); dispatcher.forward(request, response); /*response.setContentType(CONTENT_TYPE); PrintWriter out = response.getWriter(); out.println("<html>"); out.println("<head><title>WantColors</title></head>"); out.println("<body>"); out.println("<p>The servlet has received a GET. This is the reply.</p>"); out.println("</body></html>"); out.close();*/ } public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doGet(request, response); /*response.setContentType(CONTENT_TYPE); PrintWriter out = response.getWriter(); out.println("<html>"); out.println("<head><title>WantColors</title></head>"); out.println("<body>"); out.println("<p>The servlet has received a POST. This is the reply.</p>"); out.println("</body></html>"); out.close();*/ } private String addr; } *********************** //ColorBean.java package moonsoft.test.j2ee.wantColors; public class ColorBean { public ColorBean(String color) { this.color=color; } public String getString() { String s="The color is :/t"; return s+color; } public String getColor() { return color; } public void setColor(String color) { this.color=color; } private String color; } ************** red.jsp <%@ page contentType="text/html;charset=Big5"%> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=Big5"> <title>red</title> </head> <body bgcolor="#FF0000"> <font color="#ffffff" size=6> <jsp:useBean id="Red" type="moonsoft.test.j2ee.wantColors.ColorBean" scope="request"/> <jsp:getProperty name="Red" property="String"/> </font> </body> </html> 那兩個(gè)綠色和藍(lán)色的copy 一下,然后改一下值就好了 |
|
來(lái)自: 學(xué)習(xí)妄長(zhǎng)生 > 《JSP》