利用jsp和Servlet实现自己的原生JavaEE MVC框架
2015-05-11 12:30
435 查看
MVC是什么我就不多说了,我们平时做JavaWeb项目时,大都会用SSH框架的不同组合,那能不能不用SSH框架来实现一个原生的MVC框架呢?
下面就让我们来实现一个自己的javaWeb MVC框架。
项目结构如下:
Controler的实现:
Action接口:
Action工厂:
用户登录业务Action:
web.xml:
视图View层:login.jsp
success.jsp:
测试:
下面就让我们来实现一个自己的javaWeb MVC框架。
项目结构如下:
Controler的实现:
package com.shu.controler; import java.io.IOException; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import com.shu.action.Action; import com.shu.action.ActionFactory; /** * ControlerServlet,控制器类,所有的以.action结尾的请求都会被改控制器拦截, *例如:http://localhost:8080/mvc/LoginAction.action * @author Administrator * */ public class ControlerServlet extends HttpServlet { /** * */ private static final long serialVersionUID = 4660044561652707132L; @Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { doPost(req, resp); } @Override protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { String pathName = req.getServletPath(); System.out.println("pathName:" + pathName); int index = pathName.indexOf("."); String actionName = pathName.substring(1,index); System.out.println("actionName:" + actionName); String actionClass = getInitParameter(actionName); System.out.println("actionClass:" + actionClass); Action action = ActionFactory.getAction(actionName); String viewUrl = action.execute(req, resp); if (viewUrl == null) { req.getRequestDispatcher("error.jsp").forward(req, resp); } else { req.getRequestDispatcher(viewUrl).forward(req, resp); } } }
Action接口:
package com.shu.action; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; /** * Action 接口,所有的业务action类都需要实现此接口 * execute方法返回view资源的引用字符串,比如“admin/login.jsp” * * @author Administrator * */ public interface Action { public String execute(HttpServletRequest request, HttpServletResponse response); }
Action工厂:
package com.shu.action; import com.shu.actions.LoginAction; /** * 该工厂根据传入的Action名字返回具体的业务action实例 * @author Administrator * */ public class ActionFactory { public static Action getAction(String actionName) { if (LoginAction.class.getSimpleName().equals(actionName)) { return new LoginAction(); } return null; } }
用户登录业务Action:
package com.shu.actions; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import com.shu.action.Action; /** * 处理用户登录的action类 * @author Administrator * */ public class LoginAction implements Action { @Override public String execute(HttpServletRequest request, HttpServletResponse response) { String username = request.getParameter("username"); String password = request.getParameter("password"); request.setAttribute("username", username); request.setAttribute("password", password); if (username != null && username.trim().length() != 0 && password != null && password.trim().length() != 0) { return "admin/success.jsp"; } return "admin/login.jsp"; } }
web.xml:
<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5"> <servlet> <servlet-name>controlerServlet</servlet-name> <servlet-class>com.shu.controler.ControlerServlet</servlet-class> <init-param> <param-name>LoginAction</param-name> <param-value>com.shu.actions.LoginAction</param-value> </init-param> </servlet> <servlet-mapping> <servlet-name>controlerServlet</servlet-name> <url-pattern>*.action</url-pattern> </servlet-mapping> </web-app>
视图View层:login.jsp
<%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"> <title>Login</title> </head> <body> <form action="<%=request.getContextPath() %>/LoginAction.action" method="post"> username:<input type="text" name="username"/><br> password:<input type="password" name="password"/><br> <input type="submit" name="Login"/><br> </form> </body> </html>
success.jsp:
<%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"> <title>Insert title here</title> </head> <body> <h1>Success!</h1> username:<%=request.getParameter("username") %><p> password:<%=request.getParameter("password") %><p> </body> </html>
测试:
相关文章推荐
- 利用jsp/servlet/Ajax,实现一个猜数字游戏!
- 这几天讲的jsp+servlet+javabean实现mvc,很多东西只是入门,自己要努力。。。。。Session的
- 实践---原生jsp+servlet+jdbc+mysql实现分页功能
- (1.2)利用Servlet+JSP+JavaBean实现MVC
- 利用Servlet和jsp实现客户端与服务器端的用户登录信息验证
- jsp利用pol通过servlet来实现Excel导入数据库
- jsp+servlet实现大型文件下载(tomcat服务器),将自己今天做的和大家一起分享
- 利用JSP+JS+CSS+Servlet实现用户登录,保存用户名密码功能
- JSP+Servlet技术实现分页 首页 下一页 每一页显示10条页码 下一页 尾页 第页/共页 (利用PageBean实现)
- 利用jsp和servlet,MySQL实现简易报表
- Servlet和Jsp实现原生的上传与下载文件
- Servlet和Jsp实现原生的上传与下载文件
- JSP 中Servlet的自己实现
- jsp转html静态化(二)继承HttpServletResponseWrapper 实现自己的response包装
- 用JSP+Javabean+Servlet实现一个投票系统
- JSP+Servlet + JDBC 实现简单的登录验证模块
- JSP应用的自我理解之三:JSP+Servlet实现表单验证
- servlet+jsp+ajax+json实现传递数据小案例
- jsp/servlet定时监控的两种实现方式 (2)
- JSP和Servlet实现文件上传