网站的框架
网站的框架采用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 }