本文概述
前端控制器模式表示, 如果你想提供集中的请求处理机制, 以便所有请求将由一个处理程序处理。”该处理程序可以对请求进行身份验证或授权或记录或跟踪, 然后将请求传递给相应的处理程序。
用法
- 当你要控制页面流向和导航时。
- 当你要访问和管理数据模型时。
- 当你要处理业务时。
好处
- 它减少了JSP页面中的代码重复, 特别是在几种资源需要相同处理的情况下。
- 它更有效地维护和控制Web应用程序。
- 一个两层体系结构的Web应用程序, 推荐的方法是前端控制器来处理用户请求。
用于前端控制器模式的UML
前控制器模式的实现
步骤1
创建一个Login.html网页。
<html>
<head>
<title>
Login page
</title>
</head>
<body style="color:green;">
<h1 style="font-family:Comic Sans Ms;text-align="center";font-size:20pt;
color:#00FF00;>
Login Page
</h1>
<form method="POST" action="FrontControllerServlet" onsubmit="return checkForm(this);">
<p>Username: <input type="text" name="username"></p>
<p>Password: <input type="password" name="pwd1"></p>
<p>Confirm Password: <input type="password" name="pwd2"></p>
<p><input type="submit" value="Login"></p>
</form>
<script type="text/javascript">
function checkForm(form)
{
if(form.username.value == "") {
alert("Error: Username cannot be blank!");
form.username.focus();
return false;
}
re = /^\w+$/;
if(!re.test(form.username.value)) {
alert("Error: Username must contain only letters, numbers and underscores!");
form.username.focus();
return false;
}
if(form.pwd1.value != "" && form.pwd1.value == form.pwd2.value) {
if(form.pwd1.value.length < 6) {
alert("Error: Password must contain at least six characters!");
form.pwd1.focus();
return false;
}
if(form.pwd1.value == form.username.value) {
alert("Error: Password must be different from Username!");
form.pwd1.focus();
return false;
}
re = /[0-9]/;
if(!re.test(form.pwd1.value)) {
alert("Error: password must contain at least one number (0-9)!");
form.pwd1.focus();
return false;
}
re = /[a-z]/;
if(!re.test(form.pwd1.value)) {
alert("Error: password must contain at least one lowercase letter (a-z)!");
form.pwd1.focus();
return false;
}
re = /[A-Z]/;
if(!re.test(form.pwd1.value)) {
alert("Error: password must contain at least one uppercase letter (A-Z)!");
form.pwd1.focus();
return false;
}
} else {
alert("Error: Please check that you've entered and confirmed your password!");
form.pwd1.focus();
return false;
}
return true;
}
</script>
</body>
</html>
第2步
创建一个FrontServletServlet.java类, 它是一个servlet, 它也可能是一个JSP页面。
package controller;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
* Servlet implementation class FrontControllerServlet
*/
@WebServlet("/FrontControllerServlet")
public class FrontControllerServlet extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
response.setContentType("text/html");
PrintWriter out = response.getWriter();
String username=request.getParameter("username");
String password=request.getParameter("pwd2");
if (password.equals("Ashwani1987")) {
RequestDispatcher rd=request.getRequestDispatcher("/Success.jsp");
rd.forward(request, response);
} else {
RequestDispatcher rd=request.getRequestDispatcher("/Error.jsp");
rd.forward(request, response);
}
}
}
第三步
创建一个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>Welcome Page</title>
</head>
<body style="background-color: gray;">
<% String name=request.getParameter("username"); %>
<b>Welcome, </b> <% out.print(name); %>
</body>
</html>
步骤4
创建一个Error.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 style="background-color: olive;">
<b>You are have entered wrong username or password!!</b><br>
<a href="Login.html">Back To Home Page</a>
</body>
</html>
第5步
创建一个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_3_0.xsd" version="3.0">
<display-name>Front Controller Example</display-name>
<welcome-file-list>
<welcome-file>Login.html</welcome-file>
</welcome-file-list>
</web-app>
输出:
评论前必须登录!
注册