工程结构如下,其中,图上少了main.jsp界面。该界面为主界面,提供跳转到增,删,查,改,界面的超链接 部分功能未实现
开发环境如下:ubuntu 14.04 +myeclipse 2014 +tomcat 7+mysql+jdbc5.1.7
参考传智博客 韩顺平java ee教程。
数据库如下:
-- MySQL dump 10.13 Distrib 5.5.41, for debian-linux-gnu (x86_64)
--
-- Host: localhost Database: spdb1
-- ------------------------------------------------------
-- Server version 5.5.41-0ubuntu0.14.04.1
/*!40101 SET @OLD_CHARACTER_SET_CLIENT=@@CHARACTER_SET_CLIENT */;
/*!40101 SET @OLD_CHARACTER_SET_RESULTS=@@CHARACTER_SET_RESULTS */;
/*!40101 SET @OLD_COLLATION_CONNECTION=@@COLLATION_CONNECTION */;
/*!40101 SET NAMES utf8 */;
/*!40103 SET @OLD_TIME_ZONE=@@TIME_ZONE */;
/*!40103 SET TIME_ZONE='+00:00' */;
/*!40014 SET @OLD_UNIQUE_CHECKS=@@UNIQUE_CHECKS, UNIQUE_CHECKS=0 */;
/*!40014 SET @OLD_FOREIGN_KEY_CHECKS=@@FOREIGN_KEY_CHECKS, FOREIGN_KEY_CHECKS=0 */;
/*!40101 SET @OLD_SQL_MODE=@@SQL_MODE, SQL_MODE='NO_AUTO_VALUE_ON_ZERO' */;
/*!40111 SET @OLD_SQL_NOTES=@@SQL_NOTES, SQL_NOTES=0 */;
--
-- Table structure for table `users`
--
DROP TABLE IF EXISTS `users`;
/*!40101 SET @saved_cs_client = @@character_set_client */;
/*!40101 SET character_set_client = utf8 */;
CREATE TABLE `users` (
`userId` int(11) NOT NULL AUTO_INCREMENT,
`username` varchar(20) DEFAULT NULL,
`passwd` varchar(20) DEFAULT NULL,
`email` varchar(30) DEFAULT NULL,
`grade` int(11) DEFAULT NULL,
PRIMARY KEY (`userId`)
) ENGINE=InnoDB AUTO_INCREMENT=28 DEFAULT CHARSET=latin1;
/*!40101 SET character_set_client = @saved_cs_client */;
--
-- Dumping data for table `users`
--
LOCK TABLES `users` WRITE;
/*!40000 ALTER TABLE `users` DISABLE KEYS */;
INSERT INTO `users` VALUES (1,'admin','admin','admin@sohu.com',1),(2,'frank','frank','tester10@sohu.com',1),(6,'tester4','tester4','tester4@sohu.com',5),(7,'tester5','tester5','tester5@sohu.com',5),(9,'tester7','tester7','tester7@sohu.com',5),(10,'tester8','tester8','tester8@sohu.com',5),(11,'tester9','tester9','tester9@sohu.com',5),(12,'tester10','tester10','tester10@sohu.com',5),(13,'tester11','tester11','tester11@sohu.com',5),(14,'tester12','tester12','tester12@sohu.com',5),(15,'tester13','tester13','tester13@sohu.com',5),(16,'tester14','tester14','tester14@sohu.com',5),(19,'texter','texter','tester10@sohu.com',5),(20,'texter1','texter1','tester10@sohu.com',5),(21,'x','x','null',5),(22,'xiaoming','xiaoming','null',5),(23,'1','1','null',1),(24,'mm','mm','null',1),(25,'43','34','null',4),(26,'er','er','er',5),(27,'yvy','yvy','yvy@frank.com',1);
/*!40000 ALTER TABLE `users` ENABLE KEYS */;
UNLOCK TABLES;
/*!40103 SET TIME_ZONE=@OLD_TIME_ZONE */;
/*!40101 SET SQL_MODE=@OLD_SQL_MODE */;
/*!40014 SET FOREIGN_KEY_CHECKS=@OLD_FOREIGN_KEY_CHECKS */;
/*!40014 SET UNIQUE_CHECKS=@OLD_UNIQUE_CHECKS */;
/*!40101 SET CHARACTER_SET_CLIENT=@OLD_CHARACTER_SET_CLIENT */;
/*!40101 SET CHARACTER_SET_RESULTS=@OLD_CHARACTER_SET_RESULTS */;
/*!40101 SET COLLATION_CONNECTION=@OLD_COLLATION_CONNECTION */;
/*!40111 SET SQL_NOTES=@OLD_SQL_NOTES */;
-- Dump completed on 2015-02-13 21:51:49
controller:
LoginClServlet.java 负责用户登录的验证
package com.sp.controller;
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;
import com.sp.model.*;
public class LoginClServlet 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 {
//得到用户名和密码
String u=request.getParameter("username");
String p=request.getParameter("passname");
UserBeanCl ubc=new UserBeanCl();
//System.out.println("使用servlet");
if(ubc.checkUser(u, p))
{
ArrayList<UserBean> 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);
request.getRequestDispatcher("main.jsp").forward(request, response);
}
else
{
request.getRequestDispatcher("login.jsp").forward(request, response);
}
}
/**
* The doPost method of the servlet. <br>
*
* 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 {
this.doGet(request, response);
}
}
UsersClServlet.java 对用户的增删查改进行控制
package com.sp.controller;
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;
import com