小男孩‘自慰网亚洲一区二区,亚洲一级在线播放毛片,亚洲中文字幕av每天更新,黄aⅴ永久免费无码,91成人午夜在线精品,色网站免费在线观看,亚洲欧洲wwwww在线观看

分享

我終于用RequestDispatcher實(shí)現(xiàn)了MVC

 學(xué)習(xí)妄長(zhǎng)生 2019-11-22

1.首先建立一個(gè)html頁(yè)收集輸入,傻子都會(huì),傳遞參數(shù)為color

 

2.然后Servlet 收集參數(shù),并以參數(shù)通過(guò)JavaBean建立一個(gè)實(shí)例

Bean object =new Bean(color);

 

3.接著通過(guò)JavaBeanget方法返回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);
 RequestDispatcher dispatcher=request.getRequestDispatcher(addr);

 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 一下,然后改一下值就好了

    本站是提供個(gè)人知識(shí)管理的網(wǎng)絡(luò)存儲(chǔ)空間,所有內(nèi)容均由用戶發(fā)布,不代表本站觀點(diǎn)。請(qǐng)注意甄別內(nèi)容中的聯(lián)系方式、誘導(dǎo)購(gòu)買等信息,謹(jǐn)防詐騙。如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請(qǐng)點(diǎn)擊一鍵舉報(bào)。
    轉(zhuǎn)藏 分享 獻(xiàn)花(0

    0條評(píng)論

    發(fā)表

    請(qǐng)遵守用戶 評(píng)論公約

    類似文章 更多