Java Web實(shí)現(xiàn)的基本MVC實(shí)例分析
本文實(shí)例講述了Java Web實(shí)現(xiàn)的基本MVC。分享給大家供大家參考。具體如下:
login.jsp——視圖部分的輸入文件
success.jsp——視圖部分的輸出文件
failure.jsp——視圖部分的輸出文件
LoginBean.java——模型部分
LoginServlet.java——控制器部分
web.xml——web應(yīng)用的配置文件
下面分別介紹:
1、login.jsp
該功能的輸入文件,用戶首先訪問這個(gè)文件。主要用于輸入用戶名和口令。
代碼如下:
<%@ page contentType="text/html;charset=gb2312"%> <script language="JavaScript"> function isValidate(form) { // 得到用戶輸入的信息 username = form.username.value; userpass = form.userpass.value; // 判斷用戶名長度 if(!minLength(username,6)) { alert("用戶名長度小于6位!"); form.username.focus(); return false; } if(!maxLength(username,8)) { alert("用戶名長度大于8位!"); form.username.focus(); return false; } // 判斷口令長度 if(!minLength(userpass,6)) { alert("口令長度小于6位!"); form.userpass.focus(); return false; } if(!maxLength(userpass,8)) { alert("口令長度大于8位!"); form.userpass.focus(); return false; } return true; } // 驗(yàn)證是否滿足最小長度 function minLength(str,length) { if(str.length>=length) return true; else return false; } // 判斷是否滿足最大長度 function maxLength(str,length) { if(str.length<=length) return true; else return false; } </script> <html> <head> <title>用戶登陸</title> </head> <body> <h2>用戶登錄</h2> <form name="form1" action="login" method="post" onsubmit="return isValidate(form1)"> 用戶名:<input type="text" name="username"> <br> 口令:<input type="password" name="userpass"><br> <input type="reset" value="重置"> <input type="submit" value="提交"><br> </form> </body> </html>
代碼中提供了客戶端驗(yàn)證功能(用戶名和口令的長度為6-8位)。驗(yàn)證通過之后會(huì)把請求提交給控制器Servlet。
2、success.jsp
登錄成功之后會(huì)跳轉(zhuǎn)到這個(gè)界面,界面的代碼如下:
<%@ page contentType="text/html;charset=gb2312"%> <html> <head> <title>登錄成功</title> </head> <body> <h2>${sessionScope.username}您好,歡迎登錄網(wǎng)上書店!</h2> </body> </html>
代碼中使用表達(dá)式語言把登錄后的用戶信息顯示在街面上。
3、failure.jsp
登錄失敗后會(huì)跳轉(zhuǎn)到這個(gè)界面,界面的代碼如下:
<%@ page contentType="text/html;charset=gb2312"%> <html> <head> <title>登錄失敗</title> </head> <body> <h2>用戶名或者口令不正確,請<a href="login.jsp">重新登錄!</a></h2> </body> </html>
代碼中提供了一個(gè)超鏈接,能夠鏈接到登錄界面。
4、LoginBean.java
完成登錄功能,這里假設(shè)用戶名和口令相等表示登錄成功。
package beans; public class LoginBean { public boolean validate(String username,String userpass){ return username.equals(userpass); } }
5、LoginServlet.java
該文件完成控制,主要功能可以描述如下:
①. 從login.jsp獲取用戶輸入的用戶名和口令;
②. 創(chuàng)建LoginBean的對象,調(diào)用LoginBean的方法validate;
③. 根據(jù)方法返回的結(jié)果,選擇success.jsp或者failure.jsp對用戶響應(yīng)。
完整的代碼如下:
package servlets; import java.io.IOException; import java.io.PrintWriter; import javax.servlet.*; import javax.servlet.http.*; import beans.*; public class LoginServlet extends HttpServlet { public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doPost(request,response); } public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // 獲取用戶輸入的用戶ID和口令 String username = request.getParameter("username"); String userpass = request.getParameter("userpass"); // 創(chuàng)建模型對象 LoginBean loginBean = new LoginBean(); // 調(diào)用業(yè)務(wù)方法進(jìn)行驗(yàn)證 boolean b = loginBean.validate(username,userpass); // 要轉(zhuǎn)向的文件 String forward; // 如果登陸成功,把用戶名寫入session中,并且轉(zhuǎn)向success.jsp, // 否則轉(zhuǎn)向failure.jsp if(b){ // 獲取session HttpSession session = (HttpSession)request.getSession(true); // 把用戶名保存到session中 session.setAttribute("username",username); // 目標(biāo)轉(zhuǎn)向文件是success.jsp forward = "success.jsp"; }else{ // 目標(biāo)轉(zhuǎn)向文件是failure.jsp forward = "failure.jsp"; } // 獲取Dispatcher對象 RequestDispatcher dispatcher = request.getRequestDispatcher(forward); // 完成跳轉(zhuǎn) dispatcher.forward(request,response); } }
代碼中把登錄用戶的用戶信息保存在了session中,在實(shí)際應(yīng)用中同樣也是這樣處理的。
6、web.xml
主要代碼是Servlet的配置,代碼如下:
<?xml version="1.0" encoding="UTF-8"?> <web-app version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"> <servlet> <description>This is the description of my J2EE component</description> <display-name>This is the display name of my J2EE component</display-name> <servlet-name>LoginServlet</servlet-name> <servlet-class>servlets.LoginServlet</servlet-class> </servlet> <servlet-mapping> <servlet-name>LoginServlet</servlet-name> <url-pattern>login</url-pattern> </servlet-mapping> </web-app>
希望本文所述對大家的JSP程序設(shè)計(jì)有所幫助。
相關(guān)文章
jsp實(shí)現(xiàn)點(diǎn)擊help打開chm文件
有個(gè)javaweb項(xiàng)目,需要在portal上面點(diǎn)擊help即可打開“幫助.chm”文件,下面與大家分享下jsp如何打開chm文件2014-09-09JSP開發(fā)之Spring方法注入之替換方法實(shí)現(xiàn)
這篇文章主要介紹了JSP開發(fā)之Spring方法注入之替換方法實(shí)現(xiàn)的相關(guān)資料,需要的朋友可以參考下2017-07-07使用maven+eclipse搭建struts2開發(fā)環(huán)境
Struts 2是Apache基金會(huì)的明星級產(chǎn)品,提供了對MVC的一個(gè)清晰的實(shí)現(xiàn),下面就為大家介紹一下使用maven+eclipse搭建struts2開發(fā)環(huán)境的方法2014-01-01Apache+Servlet+Jsp環(huán)境設(shè)置(下)
Apache+Servlet+Jsp環(huán)境設(shè)置(下)...2006-10-10