使用自定义MVC实现登陆

15 篇文章 0 订阅

一)登陆界面

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
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>
    <form action="login.do" method="post">
    	<input type="text" name="uname" /><br/>
    	<input type="password" name="upwd" /><br/>
    	<input type="submit" value="登陆" />
    </form>
  </body>
</html>
二)根据登陆界面表单,定义一个与表单里面name同名的属性

       在写loginForm之前写一个TotalForm的类,下面会解释到为什么用它

package com.bihua.entity;

public class LoginForm extends TotalForm{
	private String uname;
	private String upwd;
	public LoginForm() {
	}


	public LoginForm(String uname, String upwd) {
		this.uname = uname;
		this.upwd = upwd;
	}


	public String getUname() {
		return uname;
	}


	public void setUname(String uname) {
		this.uname = uname;
	}


	public String getUpwd() {
		return upwd;
	}


	public void setUpwd(String upwd) {
		this.upwd = upwd;
	}
}

三)定一个接口类TotalDO

package com.bihua.servletdo;

import com.bihua.entity.TotalForm;

public interface TotalDO {
	public Jump excute(TotalForm totalForm);
}
四)在写业务逻辑之前,定义一个跳转去哪里的类Jump类

package com.bihua.servletdo;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class Jump {
	private String url;//跳到哪个页面
	private boolean b;//是否为重定向
	public Jump() {
	}
	public Jump(String url, boolean b) {
		this.url = url;
		this.b = b;
	}
	public String getUrl() {
		return url;
	}
	public void setUrl(String url) {
		this.url = url;
	}
	public boolean isB() {
		return b;
	}
	public void setB(boolean b) {
		this.b = b;
	}
	@Override
	public String toString() {
		return "Jump [b=" + b + ", url=" + url + "]";
	}
	
	public void jumpPage(HttpServletRequest req, HttpServletResponse resp){
		try {
			//如果正确,进行重定向
			if(b){
				resp.sendRedirect(url);
			}else{
				//否则用转发
				req.getRequestDispatcher(url).forward(req, resp);
			}
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (ServletException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
}

五)写一个登陆的DO,相当于业务逻辑LoginDO

package com.bihua.servletdo;

import com.bihua.entity.LoginForm;
import com.bihua.entity.TotalForm;

public class LoginDO implements TotalDO{

	/**
	 * 虽然方法的参数里传的是TotalForm,但是根据业务逻辑中判断的值实质为loginForm
	 */
	public Jump excute(TotalForm totalForm) {
		//强转成LoginForm
		LoginForm loginForm=(LoginForm)totalForm;
		//根据拿到的值进行判断,当然可以升级为数据库判断
		if("admin".equals(loginForm.getUname())&&"123".equals(loginForm.getUpwd())){
			//跳到需要的界面
			return new Jump("index.jsp", true);
		}
		return new Jump("defeat.jsp",true);
	}

}
六)业务逻辑TotalServlet,这里是自定义MVC的重要的一步,每一行都有注释

package com.zking.servlet;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.Enumeration;
import java.util.Properties;

import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.zking.entity.TotalForm;
import com.zking.servletdo.Jump;
import com.zking.servletdo.TotalDO;

public class TotalServlet extends HttpServlet{
	@Override
	public void init(ServletConfig config) throws ServletException {
		try {
			//定义Properties用来获得config.properties中的业务逻辑的值
			Properties properties=new Properties();
			//定义Properties用来保存loginForm表单实体类的值
			Properties propertiesObject=new Properties();
			//获取项目的根目录
			String serverPath=config.getServletContext().getRealPath("/");
			//读取config.properties的值
			FileInputStream fis=new FileInputStream(serverPath+"WEB-INF/config.properties");
			//加载读取的内容
			properties.load(fis);
			//把Properties均放入ServletContext中
			config.getServletContext().setAttribute("properties", properties);
			config.getServletContext().setAttribute("propertiesObject", propertiesObject);
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		
	}
	
	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		try {
			//获取请求的路径
			String uri=req.getRequestURI();
			//进行截取,最终截取到config.properties中的名字
			uri=uri.substring(uri.lastIndexOf("/")+1,uri.lastIndexOf("."));
			//获取ServletContext中的properties
			Properties properties=(Properties) req.getSession().getServletContext().getAttribute("properties");
			//根据config.properties中的名字拿到全限定名
			String className=properties.getProperty(uri);
			
			//获取ServletContext中的propertiesObject
			Properties propertiesObject=(Properties) req.getSession().getServletContext().getAttribute("propertiesObject");
			//根据全限定名拿到对象,进行强转
			TotalDO totalDO=(TotalDO) propertiesObject.get(className);
			
			//进行判断,不用重复创建对象
			if(totalDO==null){
				//用类加载器根据全限定名新建出totalDO的对象
				totalDO=(TotalDO) Class.forName(className).newInstance();
				//把对象放入到propertiesObject中
				propertiesObject.put(className, totalDO);
			}
			
			//定义表单实体类的名字
			String formUri=uri+"Form";
			//根据命名拿到config.properties中的全限定名
			String classNameForm=properties.getProperty(formUri);
			
			//用类加载器根据全限定名新建出totalForm的对象
			Class clazzForm=Class.forName(classNameForm);
			//转成totalForm的对象
			TotalForm totalForm=(TotalForm) clazzForm.newInstance();
			
			//获取所有的表单的值
			Enumeration enumeration=req.getParameterNames();
			//根据是否还有更多的值进行循环
			while(enumeration.hasMoreElements()){
				//获得每一个集合中的每一个值
				String paramName=(String) enumeration.nextElement();
				//根据值拿到内容
				String paramValue=req.getParameter(paramName);
				
				//获得方法名
				String methodName="set"+paramName.substring(0,1).toUpperCase()+paramName.substring(1);
				//获得set方法,把所有的值存放到totalForm里面
				Method method=clazzForm.getDeclaredMethod(methodName, String.class);
				//设置方法可用
				method.setAccessible(true);
				//执行方法
				method.invoke(totalForm, paramValue);
			}
			
			//执行LoginDO的值
			Jump jump=totalDO.excute(totalForm);
			//执行出跳转页面的方法
			jump.jumpPage(req, resp);
		} catch (InstantiationException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IllegalAccessException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (SecurityException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (NoSuchMethodException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IllegalArgumentException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (InvocationTargetException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
	}

}


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值