tch(Exception e){
? ? ? ? ? ? e.printStackTrace() ;
? ? ? ? }
? ? ? ? this.dao = new UserDAOImpl(dbc.getConnection()) ;
? ? }
? ? public boolean findLogin(User user) throws Exception{
? ? ? ? boolean flag = false ;
? ? ? ? try{
? ? ? ? ? ? flag = this.dao.findLogin(user) ;? ?
? ? ? ? }catch(Exception e){
? ? ? ? ? ? throw e ;
? ? ? ? }finally{
? ? ? ? ? ? this.dbc.close() ;
? ? ? ? }
? ? ? ? return flag ;
? ? }
}
LoginServlet.java
package com.mvc.linuxidc.servlet ;
import java.io.* ;
import java.util.* ;
import javax.servlet.* ;
import javax.servlet.http.* ;
import com.mvc.linuxidc.factory.DAOFactory;
import com.mvc.linuxidc.vo.User;
/**
?*
?* @author 偶my耶
?*? ? Servlet
?*/
public class LoginServlet extends HttpServlet {
? ?
? ? private static final long serialVersionUID = 1L;
? ? public void doGet(HttpServletRequest req,HttpServletResponse resp) throws ServletException,IOException{
? ? ? ? String path = "login.jsp" ;
? ? ? ? String userid = req.getParameter("userid") ;
? ? ? ? String userpass = req.getParameter("userpass") ;
? ? ? ? System.out.println(userid);
? ? ? ? System.out.println(userpass);
? ? ? ? List info = new ArrayList() ;? ?
? ? ? ? if(userid==null || "".equals(userid)){
? ? ? ? ? ? info.add("用户名不能为空!!!") ;
? ? ? ? }
? ? ? ? if(userpass==null || "".equals(userpass)){
? ? ? ? ? ? info.add("密码不能为空!!") ;
? ? ? ? }
? ? ? ? if(info.size()==0){? ?
? ? ? ? ? ? User user = new User() ;
? ? ? ? ? ? user.setUserid(userid) ;
? ? ? ? ? ? user.setPassword(userpass) ;
? ? ? ? ? ? try{
? ? ? ? ? ? ? ? if(DAOFactory.getIUserDAOInstance().findLogin(user)){
? ? ? ? ? ? ? ? ? ? info.add("欢迎" + user.getName() + "登陆") ;
? ? ? ? ? ? ? ? } else {
? ? ? ? ? ? ? ? ? ? info.add("请重新登录") ;
? ? ? ? ? ? ? ? }
? ? ? ? ? ? }catch(Exception e){
? ? ? ? ? ? ? ? e.printStackTrace() ;
? ? ? ? ? ? }
? ? ? ? }
? ? ? ? req.setAttribute("info",info) ;
? ? ? ? req.getRequestDispatcher(path).forward(req,resp) ;
? ? }
? ? public void doPost(HttpServletRequest req,HttpServletResponse resp) throws ServletException,IOException{
? ? ? ? this.doGet(req,resp) ;
? ? }
}
web.xml文件
? xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
? xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
? version="2.5">
? Welcome to Tomcat
?
? ? Welcome to Tomcat
?
? ?
? ? ? ? login
? ? ? ?
? ? ? ? ? ? com.mvc.linuxidc.servlet.LoginServlet
? ? ? ?
? ?
? ?
? ? ? ? login
? ? ? ? /LoginServlet
? ?
login.jsp文件
<%@ page contentType="text/html" pageEncoding="GBK"%>
<%@ page import="java.util.*"%>
<script language="java script">
? ? function validate(f){
? ? ? ? if(!(/^\w{5,15}$/.test(f.userid.value))){
? ? ? ? ? ? alert("用户ID必须是5~15位!") ;
? ? ? ? ? ? f.userid.focus() ;
? ? ? ? ? ? return false ;
? ? ? ? }
? ? ? ? if(!(/^\w{5,15}$/.test(f.userpass.value))){
? ? ? ? ? ? alert("密码必须是5~15位!") ;
? ? ? ? ? ? f.userpass.focus() ;
? ? ? ? ? ? return false ;
? ? ? ? }
? ? }
<%
? ? request.setCharacterEncoding("GBK") ;
%>
<%
? ? List info = (List) request.getAttribute("info") ;
? ? if(info != null){? ? // 有信息返回
? ? ? ? Iterator iter = info.iterator() ;
? ? ? ? while(iter.hasNext()){
%>
? ? ? ? ? ? <%=iter.next()%>
<%
? ? ? ? }
? ? }
%>
数据库文件.sql
?
/*======================= 创建user数据表 =======================*/
CREATE TABLE user(
? ? userid? ? ? ? ? ? VARCHAR(30)? ? ? ? PRIMARY KEY ,
? ? name? ? ? ? ? ? VARCHAR(30)? ? ? ? NOT NULL ,
? ? password? ? ? ? VARCHAR(32)? ? ? ? NOT NULL
) ;
/*======================= 插入测试数据 =======================*/
INSERT INTO user (userid,name,password) VALUES ('admin','administrator','admin') ;
效果图:


MVC设计模式的好处:
JSP只负责显示
DAO负责数据层操作
Servlet连接JSP和DAO,并根据JavaBean的操作结果进行跳转