`
ootabc
  • 浏览: 108248 次
  • 性别: Icon_minigender_1
  • 来自: 湖南
社区版块
存档分类
最新评论

自写控制层小框架

    博客分类:
  • java
阅读更多
我是一个java菜鸟!近来在学习java,也想写点小东西。希望各位大侠给我指正编码中的问题。谢谢了!
package dj.dnsp.servlet;

import java.io.IOException;
import java.io.InputStream;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.Properties;

public class propertiesUtil {

	public static propertiesUtil pUtil= null;
	public static Properties ps = null;
	public static HashMap sourceMap = new HashMap();

	private InputStream  in =  null;

	public propertiesUtil(){
		in = this.getClass().getResourceAsStream("properties.properties");
		System.out.println("开始 properties");
	}

	public static propertiesUtil getPropertiesUtil(){
		if(null == pUtil){
			pUtil =  new propertiesUtil();
		}
		return pUtil;
	}

	public  static Properties getProperties(){
		if(null == ps){
			ps = new Properties();
		}
		try {
			ps.load(propertiesUtil.getPropertiesUtil().in);
		} catch (IOException e) {
			e.printStackTrace();
		}
		return ps;	
	}

	public static HashMap getMap(){
		//得到所有的主键信息(这里的主键信息主要是简化的主键,也是信息的关键)
		Enumeration   en =  propertiesUtil.getProperties().keys();
		while(en.hasMoreElements()){
			String key = (String)en.nextElement();
			String message = propertiesUtil.getProperties().getProperty(key);
			sourceMap.put(key, message); 	
		}
		return sourceMap;
	}

}


package dj.dnsp.servlet;

import java.io.IOException;
import java.util.Properties;

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

public class servlet extends HttpServlet {

	protected void doGet(HttpServletRequest rep, HttpServletResponse res) throws ServletException, IOException {
		
		action at = null;
		
		String actionName = (String)rep.getParameter("actionName");
		System.out.println(actionName);
//		Properties ps = propertiesUtil.getProperties();		
//		String action = ps.getProperty(actionName);
		String action = (String)propertiesUtil.getMap().get(actionName);
		
		System.out.println(action);
		
		try {
			at = (action)Class.forName(action).newInstance();
		} catch (InstantiationException e) {
			e.printStackTrace();
		} catch (IllegalAccessException e) {
			e.printStackTrace();
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		}
		System.out.println("star");
		at.service(rep, res);
		
	}

	protected void doPost(HttpServletRequest rep, HttpServletResponse res) throws ServletException, IOException {
		
		doGet(rep, res);
	}

}



package dj.dnsp.servlet;

import java.io.IOException;

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

public abstract class action extends HttpServlet {

	public abstract  void service(HttpServletRequest rep, HttpServletResponse res) throws ServletException, IOException ;
	
	public abstract  void forward(HttpServletRequest rep,HttpServletResponse res)throws ServletException,IOException;
}

package dj.dnsp.servlet;

import java.io.IOException;
import java.io.PrintWriter;

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

public class loginAction extends action{

	public void service(HttpServletRequest rep, HttpServletResponse res) throws ServletException, IOException {
		
		PrintWriter out = res.getWriter();
		System.out.println("-------start-----------");
		out.println("<html>");
		out.println("<head><title>MVCTest</title></head>");
		out.println("<body><h1>SUCCESS</h1></body>");
		out.println("</html>");
		System.out.println("---------over----------");
	}


	public void forward(HttpServletRequest rep, HttpServletResponse res) throws ServletException, IOException {		
	}

}

<%@ 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 'index.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="action?actionName=login" method = "post">
   <table>
   <tr>
   <td>
     <input type ="text" name = "name" id = "name">
   </td>
   </tr>
     <tr>
   <td>
     <input type ="submit" name = "n" id = "n" value="提交">
   </td>
   </tr>  
   </table>
   </form>
  </body>
</html>
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics