歡迎您光臨本站 註冊首頁

MVC模式之我的見解

←手機掃碼閱讀     火星人 @ 2014-03-10 , reply:0

初學j2ee的人很有可能喜歡使用jsp來完成其網站的所有功能,因為其簡單方便易學,但是這樣的網站維護升級十分困難,並且和asp,PHP做的網站沒有什麼區別,顯示不出java的優點.MVC模式可以說是一個經典的模式,它將視圖,模式,控制三者分開,資料庫處理,邏輯處理全部放到後台,客戶端只負責顯示,維護和升級十分方便,並且由於和XML結合的很好,資料庫的移植等都十分方便,只需要修改配置文件,而不必修改源程序再重新編譯.再MVC模式中,一般用一個javaBean來存放數據,以實現對數據的封裝,下面是一個例子 public class UserBean
{
private String name=null;
private String pwd=null;
private int id;
public UserBean(String un,String up,int ui)
{
this.name=un;
this.pwd=up;
this.id=ui;
}
public UserBean()
{
}
public String getName()
{
return name;
}
public void setName(String name)
{
this.name = name;
}
public String getPwd()
{
return pwd;
}
public void setPwd(String pwd)
{
this.pwd = pwd;
}
public int getId()
{
return id;
}
public void setId(int id)
{
this.id = id;
}
}
一個資料庫操作的文件實現對資料庫操作的封裝,注意防止SQL注入,例如
import java.sql.Connection; import java.sql.DriverManager;
import java.sql.PreparedStatement; import java.sql.ResultSet;
import java.sql.SQLException; import java.sql.Statement;
import java.util.ArrayList;
public class DBCon
{
private String DBDriver = "org.gjt.mm.mysql.Driver";
private String connStr ="jdbc:mysql://localhost/cdemo?user=root&password=243161193&useUnicode=true&characterEncoding=GBK";
private Connection conn = null;
private ResultSet rs = null;
private PreparedStatement ps=null;
public DBCon()
{
try
{
Class.forName(DBDriver);//sDBDriver);
}
catch(java.lang.ClassNotFoundException e)
{
System.err.println("conn(): " e.getMessage());
}
}
public boolean findUser(String name,String pwd)
{
boolean find=true;
try {
conn=DriverManager.getConnection(connStr);
//Statement stmt=conn.createStatement();
String sql="select*from fenye where name=? and pwd=?";
ps=conn.prepareStatement(sql);
ps.setString(1, name);
ps.setString(2, pwd);
rs=ps.executeQuery();
//rs=stmt.executeQuery(name);
if(rs.next())
find=true;


}
catch (SQLException e)
{

// TODO Auto-generated catch block
e.printStackTrace();
}finally
{
//關閉資源
close();
return find;
}
}
public ArrayList fenYe(int now,int size)
{
int start=(now-1)*size;
int end=now*size;
ArrayList al=new ArrayList();
try {
conn=DriverManager.getConnection(connStr);
//Statement stmt=conn.createStatement();
String sql="select*from fenye where id between ? and ?";
ps=conn.prepareStatement(sql);
ps.setInt(1, start);
ps.setInt(2, end);
rs=ps.executeQuery();
while(rs.next())
{
int id=rs.getInt(1);
String name=rs.getString(2);
String pwd=rs.getString(3);
UserBean ub=new UserBean(name,pwd,id);
al.add(ub);
}
}
catch (SQLException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}finally
{
//關閉資源
close();
return al;
}
}
public int getSize()
{
int size=0;
try {
conn=DriverManager.getConnection(connStr);
//Statement stmt=conn.createStatement();
String sql="select count(*) from fenye";
ps=conn.prepareStatement(sql);
rs=ps.executeQuery();
if(rs.next())
size=rs.getInt(1);
}
catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally
{
//關閉資源
close();
return size;
}
}
public void close()
{
if(conn!=null)
try {
conn.close();
conn=null;
}
catch (SQLException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
if(ps!=null)
try {
ps.close();
ps=null;
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
if(rs!=null)
{
try {
rs.close();
rs=null;
} catch (SQLException e) {
// TODO Auto-generated catch block

e.printStackTrace();
}
}
}


/*public static void main(String[] args)
{
DBCon d=new DBCon();
System.out.println(d.getSize());
ArrayList al=d.fenYe(1, 5);
if(al!=null)
{
for(int i=0;i<5;i )
{
System.out.println(((UserBean)al.get(i)).getName());
}
}
}
*/
}
一個文件用來實現邏輯處理,例如
import java.io.IOException; import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
public class LoginTackle extends HttpServlet
{
/**
* The doPost method of the servlet. <br> *
* This method is called when a form has its tag value method equals to post. *
* @param request the request send by the client to the server
* @param response the response send by the server to the client
* @throws ServletException if an error occurred
* @throws IOException if an error occurred
*/
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
DBCon dbconn=new DBCon();
String name,pwd = null;
response.setContentType("text/html;charset=GBK");
PrintWriter out = response.getWriter();
name=request.getParameter("username");
pwd=request.getParameter("userpwd");
HttpSession session= request.getSession(true);
//System.out.println(name " " pwd);
String week=request.getParameter("week");
///System.out.println(week);
if(week!=null)
{
Cookie cookie=new Cookie("username","lzyzsd");
cookie.setMaxAge(7*24*3600);
response.addCookie(cookie);
System.out.println("Add Cookie OK");
}
if(dbconn.findUser(name, pwd))//name.equalsIgnoreCase("lzyzsd")&&pwd.equalsIgnoreCase("24316"))
{
session.setAttribute("login","true");
session.setAttribute("username",name);
session.setAttribute("userpwd",pwd);
response.sendRedirect("Welcome?page=1");
/*out.println("<HTML>");
out.println("<BODY>");
out.println("<CENTER>");
/*out.println("<Form action= method=POST>");
//out.print("<p><h3>用戶名<h3>");
out.println("<p><h3>用戶名<h3><input type=TextField name=username /></p>");
//out.print("<p><h3>密碼<h3>");


out.println("<p><h3>密碼<h3><input type=TextField name=userpwd /></p>");
out.println("<input type=submit value=提交");
out.println("</Form>");*/
/*out.println("<h1>" name "</h1>");
out.println("</CENTER>");
out.println("</BODY>");
out.println("</HTML>");*/
}else
{
response.sendRedirect("Login");
}
out.flush();
out.close();
}
/*public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException
{
doPost(request,response);
}
*/ }


[火星人 ] MVC模式之我的見解已經有547次圍觀

http://coctec.com/docs/java/show-post-61918.html