利用jsp和Servlet實現自己的原生JavaEE MVC架構

來源:互聯網
上載者:User

標籤:mvc servlet jsp

        MVC是什麼我就不多說了,我們平時做JavaWeb項目時,大都會用SSH架構的不同組合,那能不能不用SSH架構來實現一個原生的MVC架構呢?

下面就讓我們來實現一個自己的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;@Overrideprotected void doGet(HttpServletRequest req, HttpServletResponse resp)throws ServletException, IOException {doPost(req, resp);}@Overrideprotected 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 {@Overridepublic 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實現自己的原生JavaEE MVC架構

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.