javabean 使用 JavaBean+servlet的mvc
javabean 使用 javabean+servlet的mvc
先设置web-inf下面的web.xml设置路由了
<servlet>
<servlet-name>
login
</servlet-name>
<servlet-class>
javaweb.javabean
</servlet-class>
</servlet><servlet-mapping>
<servlet-name>login</servlet-name>
<url-pattern>/login.do</url-pattern>
</servlet-mapping>
login.jsp教程
<%@ page language="java" import="java.util.*" pageencoding="utf-8"%>
<html>
<center>
<form method=post action="wellcom.jsp">
username<input type=text name=username>
<br><br>
password<input type=password name=password>
<br><br>
<input type=submit value="注册">
</form>
</center>
</html>
定义javabean:
package javaweb;
public class javabean {
private string username;
private string password;
public void setusername(string username)
{
this.username=username;
}
public void setpassword(string password)
{
this.password=password;
}
public string getusername()
{
return username;
}
public string getpassword()
{
return password;
}
}
<%@ page language="java" import="java.util.*" pageencoding="utf-8"%>
<html>
<jsp:usebean id="hello" class="javaweb.javabean" scope="session" />
<jsp:setproperty name="hello" property="*" />
<%
//hello.username = "myname";
%>
your username is:<%= hello.getusername() %>
<br><br>
your password is:<jsp:getproperty name="hello" property="password"/>
<br><br></html>
jsp + javabean + servlet (俗称的mvc结构)
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
import com.huanlin.userinfobean;public class helloservlet2 extends https教程ervlet {
public void service(httpservletrequest request,
httpservletresponse response)
throws servletexception, ioexception {
response.setcontenttype("text/html; charset=gbk");
request.setcharacterencoding("gbk");//创建javabean的对象
javabean bean = new javabean();
bean.setusername( request.getparameter("username").tostrng());
httpsession session = request.getsession();
session.setattribute("userinfo", userinfo);// 前往指定的网页
response.sendredirect("welcom.jsp")
}
}
为了重用逻辑代码部分,外加内嵌页面java代码太多,不好管理,于是出现了jsp+javabean的两层架构。就是把逻辑代码部分提取出来,放在class里面,然后在jsp页面调用class;calss功能有点类似于webform的后台处理代码,但是本质上是完全不同的;webform的后台代码是可以直接控制asp教程x页面的服务器控件,而javabean是传输数据,或兼并处理数据,然后在jsp显示,jsp+javabean模式的两层分工很明确
补充:Jsp教程,J2EE/EJB/服务器