JSP部分:
err.jsp
<%@ page language="java" import="java.util.*" pageEncoding="gb2312"%> <% String path = request.getContextPath(); String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; %> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <base href="<%=basePath%>"> <title>My JSP 'login.jsp' starting page</title> <meta http-equiv="pragma" content="no-cache"> <meta http-equiv="cache-control" content="no-cache"> <meta http-equiv="expires" content="0"> <meta http-equiv="keywords" content="keyword1,keyword2,keyword3"> <meta http-equiv="description" content="This is my page"> <!-- <link rel="stylesheet" type="text/css" href="styles.css"> --> </head> <body bgcolor="pink"> <center> <!-- 引入一张图片 --> <h1>操作失败!</h1> <a href="mian.jsp">返回主界面</a> <br> </center> </body> </html>
login.jsp
<%@ page language="java" import="java.util.*" pageEncoding="gb2312"%> <% String path = request.getContextPath(); String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; %> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <base href="<%=basePath%>"> <title>My JSP 'login.jsp' starting page</title> <meta http-equiv="pragma" content="no-cache"> <meta http-equiv="cache-control" content="no-cache"> <meta http-equiv="expires" content="0"> <meta http-equiv="keywords" content="keyword1,keyword2,keyword3"> <meta http-equiv="description" content="This is my page"> <!-- <link rel="stylesheet" type="text/css" href="styles.css"> --> </head> <body bgcolor="pink"> <center> <% //推断是否正常登陆 String err=request.getParameter("err"); if(err != null) { if(err.equals("1")) { out.println("<font color=yellow size=7>用户没有正常登陆,请登陆!!!</font><br>"); } } %> <!-- 引入一张图片 --> <img src="image/logo1.png"></img> <hr> 用户登录 <br> <!-- <form action="loginCl.jsp" method="post"> --> <form action="LoginClServlet" method="post"> 用户名: <input type="text" name="username"><br> 密 码: <input type="password" name="passwd"><br> <input type="submit" value="登录"> <input type="reset" value="重置"> <INPUT type="radio" name="P" value="A">学生 <INPUT type="radio" name="P" value="B">老师 </form> <hr/> </center> </body> </html>
loginCl.jsp这个事实上已经没实用了,仅仅只是是一个纪念的过程而已
<%@ page language="java" import="java.util.*,java.sql.*,com.xf.model.*" pageEncoding="gb2312"%> <% String path = request.getContextPath(); String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; %> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <base href="<%=basePath%>"> <title>My JSP 'loginCl.jsp' starting page</title> <meta http-equiv="pragma" content="no-cache"> <meta http-equiv="cache-control" content="no-cache"> <meta http-equiv="expires" content="0"> <meta http-equiv="keywords" content="keyword1,keyword2,keyword3"> <meta http-equiv="description" content="This is my page"> <!-- <link rel="stylesheet" type="text/css" href="styles.css"> --> </head> <body> <% //接受用户名和,password,完毕对用户名的验证 String u=request.getParameter("username"); String p=request.getParameter("passwd"); /* //验证,先简单验证一下,不到数据库上验证 if(u.equals("xiaofeng") && p.equals("123")) { //合法,跳转到wel.jsp //怎样把loginCl.jsp得到的数据传给下一个页面 //1.cookie2.sessio3.response.sendRedirect response.sendRedirect("wel.jsp?user="+u); } else { //不合法的用户 response.sendRedirect("login.jsp"); } */ /* //通过数据库验证账号password //1、载入驱动 Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver"); //2、得到链接 Connection ct=DriverManager.getConnection("jdbc:sqlserver://localhost:1433;databaseName=spdb1","sa",""); //3、创建Statement Statement sm=ct.createStatement(); //4、查询 ResultSet rs=sm.executeQuery("select passwd from users where username='"+u+"'"); //5、依据查到的结果来推断 if(rs.next()) { if(rs.getString(1).equals(p)) { //合法 response.sendRedirect("wel.jsp?user="+u); } else { response.sendRedirect("login.jsp?errNo=1"); } } else { //账号错误 response.sendRedirect("login.jsp?errNo=2"); } */ UserBeanCl ubc=new UserBeanCl(); if(ubc.checkUser(u,p)) { response.sendRedirect("wel.jsp?user="+u); } else { response.sendRedirect("login.jsp"); } %> </body> </html>
main_2.jsp
<%@ page language="java" import="java.util.*" pageEncoding="gb2312"%> <% String path = request.getContextPath(); String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; %> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <base href="<%=basePath%>"> <title>My JSP 'mian.jsp' starting page</title> <meta http-equiv="pragma" content="no-cache"> <meta http-equiv="cache-control" content="no-cache"> <meta http-equiv="expires" content="0"> <meta http-equiv="keywords" content="keyword1,keyword2,keyword3"> <meta http-equiv="description" content="This is my page"> <!-- <link rel="stylesheet" type="text/css" href="styles.css"> --> </head> <body bgcolor="pink"> <center> <!-- 引入界面!! --> <hr> 选择操作选项<hr> <a href="UserClServlet?pageNow=1&flag=fenyes">查看学习情况</a><br> <a href="addUser.jsp">加入用户</a><br> <hr> </center> </body> </html>
mian.jsp
<%@ page language="java" import="java.util.*" pageEncoding="gb2312"%> <% String path = request.getContextPath(); String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; %> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <base href="<%=basePath%>"> <title>My JSP 'mian.jsp' starting page</title> <meta http-equiv="pragma" content="no-cache"> <meta http-equiv="cache-control" content="no-cache"> <meta http-equiv="expires" content="0"> <meta http-equiv="keywords" content="keyword1,keyword2,keyword3"> <meta http-equiv="description" content="This is my page"> <!-- <link rel="stylesheet" type="text/css" href="styles.css"> --> </head> <body bgcolor="pink"> <center> <!-- 引入界面!! --> <hr> 选择操作选项<hr> <a href="UserClServlet?pageNow=1&flag=fenye">管理用户</a><br> <hr> </center> </body> </html>
suc.jsp
<%@ page language="java" import="java.util.*" pageEncoding="gb2312"%> <% String path = request.getContextPath(); String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; %> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <base href="<%=basePath%>"> <title>My JSP 'login.jsp' starting page</title> <meta http-equiv="pragma" content="no-cache"> <meta http-equiv="cache-control" content="no-cache"> <meta http-equiv="expires" content="0"> <meta http-equiv="keywords" content="keyword1,keyword2,keyword3"> <meta http-equiv="description" content="This is my page"> <!-- <link rel="stylesheet" type="text/css" href="styles.css"> --> </head> <body bgcolor="pink"> <center> <!-- 引入一张图片 --> <hr> <h1>操作成功!</h1> <a href="mian.jsp">返回主界面</a> <hr> </center> </body> </html>
updv_grade.jsp
<%@ page language="java" import="java.util.*" pageEncoding="gb2312"%> <% String path = request.getContextPath(); String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; %> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <base href="<%=basePath%>"> <title>My JSP 'updUser.jsp' starting page</title> <meta http-equiv="pragma" content="no-cache"> <meta http-equiv="cache-control" content="no-cache"> <meta http-equiv="expires" content="0"> <meta http-equiv="keywords" content="keyword1,keyword2,keyword3"> <meta http-equiv="description" content="This is my page"> <!-- <link rel="stylesheet" type="text/css" href="styles.css"> --> </head> <body bgcolor="pink"> <% long Sno=Long.parseLong(request.getParameter("Sno")); %> <center> <!-- 引入界面!! --> <h1>请输入用户信息</h1> <form action="UserClServlet?flag=updv_grade&Sno=<%=Sno %>" method="post"> <table border="1"> <tr> <td bgcolor="yellow">课程号</td> <td><input type="text" name="Cno" /></td> </tr> <tr> <td bgcolor="silver">课程名</td> <td><input type="text" name="Cname" /></td> </tr> <tr> <td bgcolor="yellow">任课老师</td> <td><input type="text" name="Tname" /></td> </tr> <tr> <td bgcolor="silver">分数</td> <td><input type="text" name="Grade" /></td> </tr> <tr> <td> <input type="submit" value="改动用户" /> </td> <td><input type="reset" value="重置"/></td> </tr> </table> </form> <hr> </center> </body> </html>
wel.jsp
<%@ page language="java" import="java.util.*" pageEncoding="gb2312"%> <% String path = request.getContextPath(); String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; %> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <base href="<%=basePath%>"> <title>My JSP 'updUser.jsp' starting page</title> <meta http-equiv="pragma" content="no-cache"> <meta http-equiv="cache-control" content="no-cache"> <meta http-equiv="expires" content="0"> <meta http-equiv="keywords" content="keyword1,keyword2,keyword3"> <meta http-equiv="description" content="This is my page"> <!-- <link rel="stylesheet" type="text/css" href="styles.css"> --> </head> <body bgcolor="pink"> <% long Sno=Long.parseLong(request.getParameter("Sno")); %> <center> <!-- 引入界面!! --> <h1>请输入用户信息</h1> <form action="UserClServlet?flag=updv_grade&Sno=<%=Sno %>" method="post"> <table border="1"> <tr> <td bgcolor="yellow">课程号</td> <td><input type="text" name="Cno" /></td> </tr> <tr> <td bgcolor="silver">课程名</td> <td><input type="text" name="Cname" /></td> </tr> <tr> <td bgcolor="yellow">任课老师</td> <td><input type="text" name="Tname" /></td> </tr> <tr> <td bgcolor="silver">分数</td> <td><input type="text" name="Grade" /></td> </tr> <tr> <td> <input type="submit" value="改动用户" /> </td> <td><input type="reset" value="重置"/></td> </tr> </table> </form> <hr> </center> </body> </html>
wel_2.jsp
<%@ page language="java" import="java.util.*,java.sql.*,com.xf.model.*" pageEncoding="gb2312"%> <% String path = request.getContextPath(); String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; %> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <base href="<%=basePath%>"> <title>My JSP 'wel.jsp' starting page</title> <meta http-equiv="pragma" content="no-cache"> <meta http-equiv="cache-control" content="no-cache"> <meta http-equiv="expires" content="0"> <meta http-equiv="keywords" content="keyword1,keyword2,keyword3"> <meta http-equiv="description" content="This is my page"> <!-- <link rel="stylesheet" type="text/css" href="styles.css"> --> <script type="text/javascript"> <!-- function abc() { return window.confirm("确定要改动?"); } --> </script> </head> <body bgcolor="pink"> <% //假设用户正常登陆,则保存了session的值 String u=(String)session.getAttribute("myName"); //假设非法登陆,jsp内置对象,response,request,out,session if(u == null) { response.sendRedirect("login.jsp?err=1"); return; } %> <center> admin: <%=u %><br> <a href="login.jsp">返回又一次登录</a> <a href="mian.jsp">返回主界面</a> <hr> <h1>用户信息列表</h1> <% /* //定义四个变量 int pageNow=1; //默认从第一页開始 int pageSize=3; //一共的页数 int rowCount=0; //这个从数据库查询 int pageCount=0; //通过rowCount和pageSize求得 String s_pageNow=request.getParameter("pageNow"); if(s_pageNow != null) { //从超链接处得到值 pageNow=Integer.parseInt(s_pageNow); } //1.载入驱动 Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver"); //2.得到链接 Connection ct=DriverManager.getConnection("jdbc:sqlserver://localhost:1433;databaseName=spdb1","sa",""); //3.创建statement Statement sm=ct.createStatement(); //4.到数据库中查询 ResultSet rs=sm.executeQuery("select count(*) from users"); //5.对查询结果进行推断 //这里注意一定要next,不然GG思密达 if(rs.next()) { rowCount=rs.getInt(1); } //计算pageCount,算法非常多 if(rowCount%pageSize == 0) { pageCount=rowCount/pageSize; } else { pageCount=rowCount/pageSize+1; } //查出要显示出来的记录 rs=sm.executeQuery("select top "+pageSize+" * from users where userId not in (select top " +pageSize*(pageNow-1)+" userId from users)"); */ //调用UserBeanCl的方法(创建一个UserBeanCl的实例然后调用他的方法),完毕分页显示 // UserBeanCl ubc=new UserBeanCl(); // ArrayList a1=ubc.getUsersByPage(pageNow); //要显示的用户信息从request中取 ArrayList a1=(ArrayList)request.getAttribute("result"); //显示出来 %> <table border="1"> <tr bgcolor="pink"><td>学号</td><td>课程号</td><<td>课程名</td><td>任课老师</td><td>分数</td></tr> <% //while(rs.next()) // String [] color={"yellow","pink","yello"}; for(int i=0 ; i<a1.size() ; ++i) { //重arraylist中取出UserBean v_gradeBean vb=(v_gradeBean)a1.get(i); %> <tr bgcolor="yellow"><td><%=vb.getSno() %></td><td><%=vb.getCno() %></td><td><%=vb.getCname() %></td> <td><%=vb.getTname() %></td><td><%=vb.getGrade() %></td> <% } %> </table> <% //首页 out.println("<a href=UserClServlet?flag=fenyes&pageNow="+1+">[首页]</a>"); //String s_pageNow=(String)request.getAttribute("pageNow"); int pageNow=Integer.parseInt((String)request.getAttribute("pageNow")); //上一页 if(pageNow != 1) { out.println("<a href=UserClServlet?flag=fenyes&pageNow="+(pageNow-1)+">[上一页]</a>"); } //得到pageCount // int pageCount=ubc.getPageCount(); String s_pageCount=(String)request.getAttribute("pageCount"); int pageCount=Integer.parseInt(s_pageCount); //显示超链接 for(int i=1 ; i<=pageCount ; ++i) { out.println("<a href=UserClServlet?flag=fenyes&pageNow="+i+">["+i+"]</a>"); } //下一页 if(pageNow != pageCount) { out.println("<a href=UserClServlet?flag=fenyes&pageNow="+(pageNow+1)+">[下一页]</a>"); } //尾页 out.println("<a href=UserClServlet?flag=fenyes&pageNow="+pageCount+">[尾页]</a>"); %> <br> </center> </body> </html>
javaBean部分
ConnDB.java
/** * 功能:得到数据库的链接 * 时间:2014年6月4日10:47:57 * 作者:cutter_point */ package com.xf.model; import java.sql.*; public class ConnDB { private Connection ct=null; public Connection getConn() { try { //1、载入驱动 Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver"); //2、得到链接 ct=DriverManager.getConnection("jdbc:sqlserver://localhost:1433;databaseName=spdb1","sa",""); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } return ct; } }
Tools.java
public class Tools { //提供一个方法,转换中文乱码 public static String getNewString(String input) { String result=""; try { result=new String(input.getBytes("iso-8859-1"),"gb2312"); } catch (Exception e) { // TODO: handle exception e.printStackTrace(); } return result; } }
UserBean.java
/** * 功能:这是一个javabean,相应users表,代表数据 * 时间:2014年6月4日10:39:33 * 作者:cutter_point */ package com.xf.model; public class UserBean { private int userId; private String username; private String passwd; private String email; private int grade; public int getUserId() { return userId; } public void setUserId(int userId) { this.userId = userId; } public String getUsername() { return username; } public void setUsername(String username) { this.username = username; } public String getPasswd() { return passwd; } public void setPasswd(String passwd) { this.passwd = passwd; } public String getEmail() { return email; } public void setEmail(String email) { this.email = email; } public int getGrade() { return grade; } public void setGrade(int grade) { this.grade = grade; } }
UserBeanCl.java
/** * 功能:这是一个处理类,也叫做bo,主要封装对users表的各种操作 * 每个UserBean对象放到ArrayList里面去,更早关闭和数据库链接 * 时间:2014年6月4日10:43:48 */ package com.xf.model; import java.io.InputStream; import java.io.Reader; import java.math.BigDecimal; import java.net.URL; import java.sql.*; import java.sql.Date; import java.util.*; import com.xf.model.*; public class UserBeanCl { private Statement sm=null; private ResultSet rs=null; private Connection ct=null; private int pageSize=3; //每页大小 private int rowCount=0; //行数总计 private int pageCount=0; //通过rowCount和pageSize求得 //改动信息 /** * @author xiaofeng(没错,我就是肖锋) * @param userName:username字,就是账号 * @param passwd:通行password * @return boolean: 假设true那就加入成功,假设false那就不成功 */ public boolean updUser(long Sno, String Cno, String Cname, String Tname, int Grade) { boolean b=false; try { //创建连接,的数据库 ct=new ConnDB().getConn(); //对要操作的数据库声明 sm=ct.createStatement(); //运行语句 String sql="update v_grade set Cno='"+Cno+"', Cname='"+Cname+"', Tname='"+Tname+"', Grade="+Grade+" where Sno="+Sno+""; int a=sm.executeUpdate(sql); if(a == 1) { //改动成功 b=true; } } catch (Exception e) { // TODO: handle exception e.printStackTrace(); } finally { this.close(); } return b; } //加入信息 //删除信息处理 /** * @author xiaofeng(没错,我就是肖锋) * @param userName:username字,就是账号 * @param passwd:通行password * @param email:电子邮件 * @param grade:用户等级 * @return boolean: 假设true那就加入成功,假设false那就不成功 */ public boolean delv_gradeBySno(long Sno) { boolean b=false; try { //得到链接 ct=new ConnDB().getConn(); //声明要运行SQL sm=ct.createStatement(); //运行语句,删除语句返回影响的行数 int a=sm.executeUpdate("delete from v_grade where Sno='"+Sno+"'"); if(a == 1) { //删除成功 b=true; } } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } finally { this.close(); } return b; } public void close() { try { if(rs != null) { rs.close(); rs=null; } if(sm != null) { sm.close(); sm=null; } if(ct != null) { ct.close(); ct=null; } } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } } //得到pageCount,页数总计 public int getPageCount() { try { //得到链接 ct=new ConnDB().getConn(); //声明,创建一个sm sm=ct.createStatement(); //4.到数据库中查询 ResultSet rs=sm.executeQuery("select count(*) from v_grade"); //这里注意一定要next,不然GG思密达 if(rs.next()) { rowCount=rs.getInt(1); } //计算pageCount的结果 if(rowCount%pageSize == 0) { pageCount=rowCount/pageSize; } else { pageCount=rowCount/pageSize+1; } } catch (Exception e) { // TODO: handle exception e.printStackTrace(); } finally { this.close(); } return pageCount; } //得到用户须要显示地信息(分页) public ArrayList getUsersByPage(int pageNow) { ArrayList a1=new ArrayList(); try { //得到链接 ct=new ConnDB().getConn(); //创建statement sm=ct.createStatement(); //查询出须要显示地信息,rs用来得到数据查询的结果 rs=sm.executeQuery("select top "+pageSize+"* from v_grade where Sno not in ( select top "+pageSize*(pageNow-1)+" Sno from v_grade )"); //吧得到的信息存放到ArrayList上面去 while(rs.next()) { v_gradeBean vb=new v_gradeBean(); vb.setSno(rs.getLong(1)); vb.setCno(rs.getString(2)); vb.setCname(rs.getString(3)); vb.setTname(rs.getString(4)); vb.setGrade(rs.getInt(5)); //将ub放到arraylist里面去 a1.add(vb); } } catch (Exception e) { e.printStackTrace(); // TODO: handle exception } finally { //关闭资源 this.close(); } return a1; } //检查账号password是否正确 public boolean checkUser(String u, String p) { boolean b=false; try { System.out.println("???"); //到数据库中去验证 ct=new ConnDB().getConn(); //3、创建statement sm=ct.createStatement(); //4、进行查询,把查询第一个结果放到rs中 rs=sm.executeQuery("select passwd from users where username='"+u+"'"); //依据结果来推断是否是要寻找的数据 if(rs.next()) { //username存在,验证password if(rs.getString(1).equals(p)) { b=true; } } } catch (Exception e) { e.printStackTrace(); // TODO: handle exception } finally { this.close(); } return b; } }
v_gradeBean.java
/** * 功能:存放v_grade视图的属性 */ package com.xf.model; public class v_gradeBean { private long Sno; private String Cno; private String Cname; private String Tname; private int Grade; public long getSno() { return Sno; } public void setSno(long sno) { Sno = sno; } public String getCno() { return Cno; } public void setCno(String cno) { Cno = cno; } public String getCname() { return Cname; } public void setCname(String cname) { Cname = cname; } public String getTname() { return Tname; } public void setTname(String tname) { Tname = tname; } public int getGrade() { return Grade; } public void setGrade(int grade) { Grade = grade; } }
Servlet处理器部分
LoginClServlet.java
/** * 功能:这是一个控制器,完毕对用户身份的验证,本身是不会去完毕业务逻辑的 * 它主要是去调用model完毕对数据的处理 * 时间:2014年6月5日12:56:46 * 作者:cutter_point */ package com.xf.controller; import com.xf.model.*; import java.io.IOException; import java.io.PrintWriter; import java.util.*; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; public class LoginClServlet extends HttpServlet { public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { this.doPost(request, response); } public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { //得到账号password String u=request.getParameter("username"); //账号 String s1=request.getParameter("P"); //单选框 // u=new String(u.getBytes("iso-8859-1"),"gb2312"); //转化中文乱码 u=Tools.getNewString(u); String p=request.getParameter("passwd"); //password System.out.println("账号u="+u+" passwordp="+p); //调用模型对他进行验证 UserBeanCl ubc=new UserBeanCl(); if(ubc.checkUser(u, p)) { System.out.println("这是使用的Servlet完毕验证!!"); //合法 //跳转 //response.sendRedirect("wel.jsp"); //在进入wel.jsp之前把数据都准备好 //首先显示的是默认的第一页 ArrayList al=ubc.getUsersByPage(1); int pageCount=ubc.getPageCount(); request.setAttribute("result", al); request.setAttribute("pageCount", pageCount+""); request.setAttribute("pageNow", "1"); //存放session request.getSession().setAttribute("myName", u); //可是上面方法效率不高,高效率的是 if(s1.equals("B")) { request.getRequestDispatcher("mian.jsp?user="+u).forward(request, response); } else { request.getRequestDispatcher("main_2.jsp?user="+u).forward(request, response); } } else { //不合法 // response.sendRedirect("login.jsp"); //理由同上 request.getRequestDispatcher("login.jsp").forward(request, response); } } }
UserClServlet.java
/** * 功能:这个控制器将处理用户的分页显示,用户的删除改动查询,加入 * 时间:2014年6月5日13:47:03 * 作者:cutter_point */ package com.xf.controller; import com.xf.model.*; import java.io.IOException; import java.io.PrintWriter; import java.util.ArrayList; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; public class UserClServlet extends HttpServlet { /** * The doGet method of the servlet. <br> * * This method is called when a form has its tag value method equals to get. * * @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 doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { //得到用户希望显示的pageNow,和flag System.out.println("使用UserClServlet!"); String s_pageNow=request.getParameter("pageNow"); String flag=request.getParameter("flag"); //使用UserBeanCl的方法调用得到pageNow if(flag.equals("fenye")) { try { int pageNow=Integer.parseInt(s_pageNow); //调用UserBeanCl UserBeanCl ubc=new UserBeanCl(); //在进入wel.jsp之前把数据都准备好 ArrayList al=ubc.getUsersByPage(pageNow); int pageCount=ubc.getPageCount(); request.setAttribute("result", al); request.setAttribute("pageCount", pageCount+""); request.setAttribute("pageNow", pageNow+""); // request.setAttribute("pageNow", pageNow); 不行必须在后面加引號,不然没法用,由于别人 //默认吧pageNow看成string了,结果传过去了,以为是String类型,实际是int类型,结果就出错了 System.out.println("分页是使用了UserClServlet!!"); //从新跳转回wel.jsp里面去 request.getRequestDispatcher("wel.jsp").forward(request, response); } catch (Exception e) { // TODO: handle exception e.printStackTrace(); } } //删除用户信息 else if(flag.equals("delUser")) { UserBeanCl ubc=new UserBeanCl(); //吧id得到 long Sno=Long.parseLong(request.getParameter("Sno")); //调用删除的方法 if(ubc.delv_gradeBySno(Sno)) { //删除成功 request.getRequestDispatcher("suc.jsp").forward(request, response); } else { //删除失败 request.getRequestDispatcher("err.jsp").forward(request, response); } } else if(flag.equals("updv_grade")) { long Sno=Long.parseLong(request.getParameter("Sno")); String Cno=request.getParameter("Cno"); String Cname=request.getParameter("Cname"); String Tname=request.getParameter("Tname"); int Grade=Integer.parseInt(request.getParameter("Grade")); UserBeanCl ubc=new UserBeanCl(); //一个推断是否改动了的函数 if(ubc.updUser(Sno, Cno, Cname, Tname, Grade)) { request.getRequestDispatcher("suc.jsp").forward(request, response); } else { //加入失败 request.getRequestDispatcher("err.jsp").forward(request, response); } } else if(flag.equals("fenyes")) //学生登录 { try { int pageNow=Integer.parseInt(s_pageNow); //调用UserBeanCl UserBeanCl ubc=new UserBeanCl(); //在进入wel.jsp之前把数据都准备好 ArrayList al=ubc.getUsersByPage(pageNow); int pageCount=ubc.getPageCount(); request.setAttribute("result", al); request.setAttribute("pageCount", pageCount+""); request.setAttribute("pageNow", pageNow+""); // request.setAttribute("pageNow", pageNow); 不行必须在后面加引號,不然没法用,由于别人 //默认吧pageNow看成string了,结果传过去了,以为是String类型,实际是int类型,结果就出错了 System.out.println("分页是使用了UserClServlet!!"); //从新跳转回wel.jsp里面去 request.getRequestDispatcher("wel_2.jsp").forward(request, response); } catch (Exception e) { // TODO: handle exception e.printStackTrace(); } } } public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException{ this.doGet(request, response); } }
PS:mvc开发模式下的一个用户管理系统,增删改查这些基本功能都已经实现,没有贴出数据库部分,由于不是必需啊!!