1.首先建立一个html页收集输入,傻子都会,传递参数为color
2.然后Servlet 收集参数,并以参数通过JavaBean建立一个实例
Bean object =new Bean(color);
3.接着通过JavaBean的get方法返回color, Servlet 进行判断.//equals() method.
object.getColor();
4.servlet 判断后转到相应的jsp页面
String addr=...
核心方法:
reqest.setAttribute("Instance", object);
RequestDispatcher dispatcher=request.getRequestDispatcher(addr);
dispatcher .forward(request, response);
5.jsp 页面根据前面建立的实例读取相应的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"/>
这个程序开始是按书上抄的,后来完全是自己写的
这下面的代码还不是很理解,到底建立了几个对象?那个是实例?特别是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);
现在可以实现mvc架构了,但是,其中对象的产生过程还是不理解,对象啊,对象......
下边是源代码:
其中
Input.jsp 为收集颜色(string)页面
WantColor.java 为servlet
ColorBean.java 为JavaBean
其它的是三个jsp 页面用来连接javaBean 显示颜色
越来觉得老外的书经典了,为什么我不是美国人呢?
***********
//input.jsp
<%@ page contentType="text/html;charset=GB2312"%>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=GB2312">
<title>请输入你想要的颜色</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>
那两个绿色和蓝色的copy 一下,然后改一下值就好了