设为首页 加入收藏

TOP

程序开发:MVC设计模式与应用(二)
2015-02-04 13:52:53 来源: 作者: 【 】 浏览:72
Tags:程序开发 MVC 设计模式 应用
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()%>


<%
? ? ? ? }
? ? }
%>

? ? 用户ID:

? ? 密  码:

? ?
? ?



数据库文件.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的操作结果进行跳转


首页 上一页 1 2 下一页 尾页 2/2/2
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
分享到: 
上一篇AJAX开发技术入门基础知识 下一篇定时cronjob调用Java程序

评论

帐  号: 密码: (新用户注册)
验 证 码:
表  情:
内  容: