初學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.
*
* 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("");
out.println("");
out.println("");
/*out.println("");
//out.print("
用戶名");
out.println("
用戶名");
//out.print("
密碼");
out.println("
密碼");
out.println("
out.println("");*/
/*out.println(""+name+"");
out.println("");
out.println("");
out.println("");*/
}else
{
response.sendRedirect("Login");
}
out.flush();
out.close();
}
/*public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException
{
doPost(request,response);
}
*/ }