码迷,mamicode.com
首页 > 其他好文 > 详细

UserManageSys

时间:2014-06-24 20:01:40      阅读:152      评论:0      收藏:0      [点我收藏+]

标签:java   mvc   j2ee   session   

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>
   <%
   	//接受用户名和,密码,完成对用户名的验证
   	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");
   	}
 */
 /*
 	//通过数据库验证账号密码
	//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:用户名字,就是账号
	 * @param passwd:通行密码
	 * @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:用户名字,就是账号
	 * @param passwd:通行密码
	 * @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;
	}

	//检查账号密码是否正确
	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())
			{
				//用户名存在,验证密码
				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
	{
		
		//得到账号密码
		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");	//密码
		
		System.out.println("账号u="+u+" 密码p="+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开发模式下的一个用户管理系统,增删改查这些基本功能都已经实现,没有贴出数据库部分,因为没必要啊!!bubuko.com,布布扣bubuko.com,布布扣










UserManageSys,布布扣,bubuko.com

UserManageSys

标签:java   mvc   j2ee   session   

原文地址:http://blog.csdn.net/cutter_point/article/details/33285163

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!