網站的框架
網站的框架采用MV模式,即Model(模型)、View(視圖)。框架將界面層和模型層進行分離,結構更清晰,更加方便管理代碼。
下面貼出網站詳細的代碼:
1.Login.java
/** *登錄界面*/ package com.xidian; import javax.servlet.http.*; import java.io.*; public class Login extends HttpServlet { public void doGet(HttpServletRequest req,HttpServletResponse res){ try{ //中文亂碼 res.setContentType("text/html;charset=gbk"); PrintWriter pw=res.getWriter(); //返回登錄界面 pw.println("<html>"); pw.println("<body bgcolor=#CDD4FF >"); pw.println("<center><img src=images/1.jpg width=120px height=150px><hr>"); //得到error信息 String info=req.getParameter("info"); if(info!=null){ pw.println("<h4>請正確登錄!</h4>"); } pw.println("<h3>登錄界面<h3>"); pw.println("<form action=logincl method=post>"); pw.println("用戶名:<input type=text name=username><br>"); pw.println("密碼:<input type=password name=password><br>"); pw.println("<input type=checkbox name=keep value=2>保存Cookie<br>"); pw.println("<input type=submit name=login value=login><br>"); pw.println("</form>"); pw.println("</center><hr><img src=images/3.jpg width=120px height=150px>"); pw.println("<body>"); pw.println("<html>"); } catch(Exception ex){ ex.printStackTrace(); }
} public void doPost(HttpServletRequest req,HttpServletResponse res){ this.doGet(req,res); } }
2.LoginCl.java
1 //處理界面 2 package com.xidian; 3 import javax.servlet.http.*; 4 import java.io.*; 5 import java.sql.*; 6 7 public class Logincl extends HttpServlet { 8 9 //重寫init函數,init只會在啟動tomcat調用servlet時被執行 10 public void init(){ 11 12 try { 13 //只會被調用一次 14 System.out.println ("init被調用"); 15 16 //添加網頁訪問次數的功能 17 //創建一個FileReader 18 FileReader f=new FileReader("e://myDocument.txt"); 19 BufferedReader br=new BufferedReader(f); 20 //讀出一行數據 21 String numVal=br.readLine(); 22 //一定要關閉文件流 23 br.close(); 24 int times=Integer.parseInt(numVal); 25 //將times值放入到servletcontext 26 this.getServletContext().setAttribute("visitTimes",times+""); 27 28 } 29 catch (Exception ex) { 30 } 31 } 32 33 //重寫destroy函數,destroy函數在tomcat關閉的時候被調用,要用shutdown.bat關,不能直接叉掉tomcat窗口 34 public void destroy(){ 35 try { 36 System.out.println ("destroy被調用"); 37 38 //再將新的次數寫回去 39 FileWriter fw=new FileWriter("e://myDocument.txt"); 40 BufferedWriter bw=new BufferedWriter(fw); 41 bw.write(this.getServletContext().getAttribute("visitTimes").toString()); 42 bw.close(); 43 } 44 catch (Exception ex) { 45 } 46 } 47 48 public void doGet(HttpServletRequest req,HttpServletResponse res){ 49 Connection ct=null; 50 PreparedStatement sm=null; 51 ResultSet rs=null; 52 try{ 53 //接收用戶名和密碼 54 String u=req.getParameter("username"); 55 String p=req.getParameter("password"); 56 //調用UserBeanCl 57 UserBeanCl ubc=new UserBeanCl(); 58 //使用UserBean方法 59 if(ubc.checkUser(u,p)){ 60 //用戶合法 61 62 //設置Cookie值 63 String keep=req.getParameter("keep"); 64 if(keep!=null){ 65 //將用戶名和密碼保存在客戶端(cookie) 66 //創建Cookie 67 Cookie name=new Cookie ("myname",u); 68 Cookie pass=new Cookie("mypasswd",p); 69 //設置時間 70 name.setMaxAge(14*24*3600); 71 pass.setMaxAge(14*24*3600); 72 //回寫到客戶端 73 res.addCookie(name); 74 res.addCookie(pass); 75 } 76 77 //設置Session值 78 HttpSession hs=req.getSession(true); 79 //1.修改session的存在時間 80 //hs.setMaxInactiveInterval(0); 81 //2.向session添加屬性 82 hs.setAttribute("uname",u+""); 83 84 //每登錄一次在Session空間中添加計數器 85 String times=this.getServletContext().getAttribute("visitTimes").toString(); 86 this.getServletContext().setAttribute("visitTimes",(Integer.parseInt(times)+1)+""); 87 88 //跳轉到Main頁面 89 res.sendRedirect("Main"); 90 91 } else{ 92 //說明用戶不存在 93 res.sendRedirect("login");//要到的servlet的那個url 94 } 95 } 96 catch(Exception ex){ 97 98 ex.printStackTrace(); 99 }finally{ 100 try { 101 if(rs!=null){ 102 rs.close(); 103 } 104 if(sm!=null){ 105 sm.close(); 106 } 107 if(ct!=null){ 108 ct.close(); 109 } 110 } 111 catch (Exception ex) { 112 ex.printStackTrace(); 113 } 114 } 115 } 116 117 public void doPost(HttpServletRequest req,HttpServletResponse res){ 118 this.doGet(req,res); 119 } 120 121 } View Code3.Main.java
/** *登錄界面 * @version 1.00 2016/7/14 */ package com.xidian; import javax.servlet.http.*; import java.io.*; public class Main extends HttpServlet { public void doGet(HttpServletRequest req,HttpServletResponse res){ try{ //中文亂碼處理 res.setContentType("text/html;charset=gbk"); PrintWriter pw=res.getWriter(); //返回登錄界面 pw.println("<html>"); pw.println("<body bgcolor=#CDD4FF >"); pw.println("<center><img src=images/1.jpg width=120px height=150px><hr>"); pw.println("<h1>主界面</h1>"); pw.println("<a href=Welcome>管理用戶</a><br>"); pw.println("<a href=???>添加用戶</a><br>"); pw.println("<a href=???>查找用戶</a><br>"); pw.println("<a href=???>安全退出</a><br>"); pw.println("</center><hr><img src=images/3.jpg width=120px height=150px>"); pw.println("<body>"); pw.println("<html>"); } catch(Exception ex){ ex.printStackTrace(); } } public void doPost(HttpServletRequest req,HttpServletResponse res){ this.doGet(req,res); } } View Code4.Welcome.java
//歡迎界面 package com.xidian; import javax.servlet.http.*; import java.io.*; import java.sql.*; import java.util.*; public class Welcome extends HttpServlet { public void doGet(HttpServletRequest req,HttpServletResponse res){ Connection ct=null; PreparedStatement ps=null; ResultSet rs=null; try { //得到session HttpSession hs=req.getSession(true); String myName=(String)hs.getAttribute("uname"); String name=""; String passwd=""; //判斷非法登錄 if(myName==null){ //如果session中沒有用戶信息,再看看有沒有cookie // 從客戶端得到所有cookie信息 Cookie [] allCookies=req.getCookies(); //如果allCookie不為空 if(allCookies!=null){ //從中取出cookie for(int i=0;i<allCookies.length;i++){ //依次取出 Cookie temp=allCookies[i]; if(temp.getName().equals("myname")){ //得到cookie值 name=temp.getValue(); }else if(temp.getName().equals("mypasswd")){ passwd=temp.getValue(); } } if(!name.equals("")&&!passwd.equals("")){ //到logincl去驗證 res.sendRedirect("logincl?username="+name+"&password="+passwd); return; } } } //得到從logincl傳遞的用戶名 String u=req.getParameter("uname"); //得到logincl傳遞的密碼 String p=req.getParameter("upass"); //解決中文亂碼問題 res.setCharacterEncoding("gbk"); PrintWriter pw=res.getWriter(); pw.println("<html>"); // pw.println("Welcome "+u+" upass="+p); pw.println("<body bgcolor=#CDD4FF >"); pw.println("歡迎您:"+myName+"<img src=images/1.gif width=50px height=30px><center><img src=images/1.jpg width=120px height=150px><hr>"); pw.println("<h1>管理用戶</h1>"); pw.println("<br><a href=login>返回重新登錄</a>"); //===========分頁的功能======================== int pageSize=5;//一頁顯示幾條記錄 int pageNow=1; //希望顯示第幾頁 //動態接收pageNow String sPageNow=req.getParameter("pageNow"); if(sPageNow!=null){ //用戶不是第一次進入Welcome頁面 pageNow=Integer.parseInt(sPageNow); } //調用UserBeanCl UserBeanCl ubc=new UserBeanCl(); ArrayList al=ubc.getResultByPage(pageNow,pageSize); pw.println("<table border=1>"); pw.println("<tr bgcolor=pink><th>id</th><th>name</th><th>passwd</th><th>email</th><th>grade</th><th>修改用戶</th><th>刪除用戶</th></tr>"); //定義一個顏色數組 String [] mycol={"silver","pink"}; for(int i=0;i<al.size();i++){ UserBean ub=(UserBean)al.get(i); pw.println("<tr bgcolor="+mycol[i%2]+">"); pw.println("<td>"+ub.getUserId()+"</td>"); pw.println("<td>"+ub.getUserName()+"</td>"); pw.println("<td>"+ub.getPasswd()+"</td>"); pw.println("<td>"+ub.getMail()+"</td>"); pw.println("<td>"+ub.getGrade()+"</td>"); pw.println("<td><a href=Update?uId="+ub.getUserId()+"&uName="+ub.getUserName()+"&uPasswd="+ub.getPasswd()+"&uEmail="+ub.getMail()+"&uGrade="+ub.getGrade()+">修改用戶</td>"); pw.println("<td><a href=DelUserCl?userid="+ub.getUserId()+" onclick=\"return window.confirm('你確定要刪除用戶嗎?')\">刪除用戶</td>"); pw.println("</tr>"); } pw.println("</table>"); //顯示上頁 if(pageNow!=1) pw.println("<a href=Welcome?pageNow="+(pageNow-1)+">"+"上一頁"+"</a>"); //顯示超鏈接 for(int i=pageNow;i<=pageNow+4;i++){ pw.println("<a href=Welcome?pageNow="+i+">"+i+"</a>"); } //顯示下頁 int pageCount=ubc.getPageCount(); if(pageNow!=pageCount) pw.println("<a href=Welcome?pageNow="+(pageNow+1)+">"+"下一頁"+"</a>"); pw.println("該網頁被訪問了"+this.getServletContext().getAttribute("visitTimes").toString()+"次"+"<br>"); //指定跳轉到某夜 //這裡實際是一個表單 //你需要考慮的問題:輸入的頁數過大?,輸入的不是數字? pw.println("<form action=Welcome>"); pw.println("<input type=text name=pageNow> "); pw.println("<input type=submit name=go> "); pw.println("</form>"); pw.println("您的ip="+req.getRemoteAddr()+"<br>"); pw.println("您的機器名="+req.getRemoteHost()+"<br>"); pw.println("</center><hr><img src=images/3.jpg width=200px height=80px>"); pw.println("</html>"); } catch(Exception ex){ ex.printStackTrace(); } } public void doPost(HttpServletRequest req,HttpServletResponse res){ this.doGet(req,res); } } View Code5.DelUserCl.java.
/** 處理刪除某個用戶 */ package com.xidian; import javax.servlet.http.*; import java.io.*; public class DelUserCl extends HttpServlet { public void doGet(HttpServletRequest req,HttpServletResponse res){ try{ //中文亂碼 res.setContentType("text/html;charset=gbk"); //調用userBeanCl的刪除用戶的方法,完成刪除 UserBeanCl ubc=new UserBeanCl(); //接收從Welcome.java中傳遞的id String id=req.getParameter("userid"); if(ubc.delUser(id)){ //刪除成功 res.sendRedirect("Ok"); }else{ //刪除失敗 res.sendRedirect("Err"); } } catch(Exception ex){ ex.printStackTrace(); } } public void doPost(HttpServletRequest req,HttpServletResponse res){ this.doGet(req,res); } } View Code6.Update.java
/** * @(#)Login.java * *修改用戶界面 * @author * @version 1.00 2016/7/14 */ package com.xidian; import javax.servlet.http.*; import java.io.*; public class Update extends HttpServlet { public void doGet(HttpServletRequest req,HttpServletResponse res){ try{ //中文亂碼 res.setContentType("text/html;charset=gbk"); PrintWriter pw=res.getWriter(); //返回登錄界面 pw.println("<html>"); pw.println("<body bgcolor=#CDD4FF >"); pw.println("<center><img src=images/1.jpg width=120px height=150px><hr>"); pw.println("<h1>修改用戶界面</h1>"); pw.println("<form action=UpdateCl>"); pw.println("<table border=1>"); pw.println("<tr><td>id</td><td><input readonly type=text name=uId value="+req.getParameter("uId")+"></td></tr>"); pw.println("<tr><td>name</td><td><input readonly type=text value="+req.getParameter("uName")+"></td></tr>"); pw.println("<tr><td>passwd</td><td><input type=text name=newPasswd value="+req.getParameter("uPasswd")+"></td></tr>"); pw.println("<tr><td>email</td><td><input type=text name=newEmail value="+req.getParameter("uEmail")+"></td></tr>"); pw.println("<tr><td>grade</td><td><input type=text name=newGrade value="+req.getParameter("uGrade")+"></td></tr>"); pw.println("<tr><td colspan=2><input type=submit value=修改用戶></td></tr>"); pw.println("</table></form>"); pw.println("</center><hr><img src=images/3.jpg width=120px height=150px>"); pw.println("<body>"); pw.println("<html>"); } catch(Exception ex){ ex.printStackTrace(); } } public void doPost(HttpServletRequest req,HttpServletResponse res){ this.doGet(req,res); } } View Code7.UpdateCl.java
/** 處理修改某個用戶 */ package com.xidian; import javax.servlet.http.*; import java.io.*; public class UpdateCl extends HttpServlet { public void doGet(HttpServletRequest req,HttpServletResponse res){ try{ //中文亂碼 res.setContentType("text/html;charset=gbk"); UserBeanCl ubc=new UserBeanCl(); if(ubc.updateUser(req.getParameter("uId"),req.getParameter("newEmail"),req.getParameter("newPasswd"),req.getParameter("newGrade"))){ //刪除成功 res.sendRedirect("Ok"); }else{ //刪除失敗 res.sendRedirect("Err"); } } catch(Exception ex){ ex.printStackTrace(); } } public void doPost(HttpServletRequest req,HttpServletResponse res){ this.doGet(req,res); } } View Code8. Ok.java
/** 操作成功界面 */ package com.xidian; import javax.servlet.http.*; import java.io.*; public class Ok extends HttpServlet { public void doGet(HttpServletRequest req,HttpServletResponse res){ try{ //中文亂碼 res.setContentType("text/html;charset=gbk"); PrintWriter pw=res.getWriter(); //返回登錄界面 pw.println("<html>"); pw.println("<body bgcolor=#CDD4FF >"); pw.println("<center><img src=images/1.jpg width=120px height=150px><hr>"); pw.println("<h1>恭喜你,操作成功!</h1>"); pw.println("<a href=Main>返回主界面</a> <a href=Welcome>返回管理用戶</a>"); pw.println("</center><hr><img src=images/3.jpg width=120px height=150px>"); pw.println("<body>"); pw.println("<html>"); } catch(Exception ex){ ex.printStackTrace(); } } public void doPost(HttpServletRequest req,HttpServletResponse res){ this.doGet(req,res); } } View Code9.Err.java
/** 操作失敗界面 */ package com.xidian; import javax.servlet.http.*; import java.io.*; public class Err extends HttpServlet { public void doGet(HttpServletRequest req,HttpServletResponse res){ try{ //中文亂碼 res.setContentType("text/html;charset=gbk"); PrintWriter pw=res.getWriter(); //返回登錄界面 pw.println("<html>"); pw.println("<body bgcolor=#CDD4FF >"); pw.println("<center><img src=images/1.jpg width=120px height=150px><hr>"); pw.println("<h1>很遺憾,操作失敗!</h1>"); pw.println("</center><hr><img src=images/3.jpg width=120px height=150px>"); pw.println("<body>"); pw.println("<html>"); } catch(Exception ex){ ex.printStackTrace(); } } public void doPost(HttpServletRequest req,HttpServletResponse res){ this.doGet(req,res); } } View Code10.ConnDB.java
//從數據庫中得到連接 package com.xidian; import java.sql.*; import javax.sql.*; import javax.naming.*; public class ConnDB{ private Connection ct=null; public Connection getConn(){ /*try { //連接數據庫 Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver"); //得到連接 ct=DriverManager.getConnection("jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=test","sa","dba"); } catch (Exception ex) { ex.printStackTrace(); } */ try { //創建一個上下文環境 Context ctt=new javax.naming.InitialContext(); //通過con得到數據源 DataSource ds=(DataSource)ctt.lookup("java:comp/env/mydb"); ct=ds.getConnection(); System.out.println("使用連接池的方式"); } catch (Exception ex) { ex.printStackTrace(); } return ct; } } View Code11.UserBean.java
//這是一個UserBean<----->users表映射 //它的一個對象<----->users表的一條記錄對應 //數據 package com.xidian; public class UserBean{ private int userId; private String userName; private String passwd; private String email; private int grade; public void setUserId(int userId){ this.userId=userId; } public int getUserId(){ return this.userId; } public void setUserName(String userName){ this.userName=userName; } public String getUserName(){ return this.userName; } public void setPasswd(String passwd){ this.passwd=passwd; } public String getPasswd(){ return this.passwd; } public void setMail(String email){ this.email=email; } public String getMail(){ return this.email; } public void setGrade(int grade){ this.grade=grade; } public int getGrade(){ return this.grade; } } View Code12.UserBeanCl.java
//這是一個UserBean<----->users表映射 //它的一個對象<----->users表的一條記錄對應 //數據 package com.xidian; public class UserBean{ private int userId; private String userName; private String passwd; private String email; private int grade; public void setUserId(int userId){ this.userId=userId; } public int getUserId(){ return this.userId; } public void setUserName(String userName){ this.userName=userName; } public String getUserName(){ return this.userName; } public void setPasswd(String passwd){ this.passwd=passwd; } public String getPasswd(){ return this.passwd; } public void setMail(String email){ this.email=email; } public String getMail(){ return this.email; } public void setGrade(int grade){ this.grade=grade; } public int getGrade(){ return this.grade; } } View Code