为了简化JSP页面,提高Java程序代码的重用性及灵活性,可以在JSP中采用HTML与Java程序相分离的策略,将Java代码单独封装成一个处理某种业务逻辑的类。然后在JSP中调用此类,此类就是一个JavaBean组件。
下面,以登录界面的实现为例展示JavaBean的优势所在
上图是实现本次任务的Java Web工程结构图,其中Users.java中封装了用户名和密码两个属性,UsersDAO.java文件实现输入的登录名和密码的正确性判断。
而WEB-INF文件下的login.jsp搭建登录页面,dologin.jsp判断和处理登录信息,login_success.jsp为登录成功页面,login_failure.jsp为登录失败页面。
UsersDAO.java
package com.dao; import com.po.Users; //用户的业务逻辑类 public class UsersDAO { //用户登录方法 public boolean usersLogin(Users u) { /* * 如果用户名输入“小健”,密码输入“123456”则认为输入正确,返回true * 否则,返回false */ if("小健".equals(u.getUsername())&&"123456".equals(u.getPassword())) { return true; } else { return false; } } }Users.java
package com.po; //用户类 public class Users { private String username; private String password; public Users() { } public String getUsername() { return username; } public void setUsername(String username) { this.username = username; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } }login.jsp
<pre name="code" class="html"><%@ page language="java" import="java.util.*" contentType="text/html; charset=utf-8"%> <% String path = request.getContextPath(); String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; %> <html> <head> <!-- Page title --> <title>登录界面</title> <!-- End of Page title --> <!-- Libraries --> <link type="text/css" href="css/login.css" rel="stylesheet" /> <link type="text/css" href="css/smoothness/jquery-ui-1.7.2.custom.html" rel="stylesheet" /> <script type="text/javascript" src="js/jquery-1.3.2.min.js"></script> <script type="text/javascript" src="js/easyTooltip.js"></script> <script type="text/javascript" src="js/jquery-ui-1.7.2.custom.min.js"></script> <!-- End of Libraries --> </head> <body> <div id="container"> <div class="logo"> <a href="#"><img src="images/logo.png" alt="" /></a> </div> <div id="box"> <form action="dologin.jsp" method="post"> <p class="main"> <label>用户名: </label> <input name="username" value="" /> <label>密码: </label> <input type="password" name="password" value=""> </p> <p class="space"> <input type="submit" value="登录" class="login" style="cursor: pointer;"/> </p> </form> </div> </div> </body> </html>
dologin.jsp
<%@ page language="java" import="java.util.*" contentType="text/html; charset=utf-8"%> <% request.setCharacterEncoding("utf-8"); %> <jsp:useBean id="loginUser" class="com.po.Users" scope="page"/> <jsp:useBean id="userDAO" class="com.dao.UsersDAO" scope="page"/> <jsp:setProperty property="*" name="loginUser"/> <% String path = request.getContextPath(); String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; //如果用户和密码都正确,则userLogin()方法返回true,跳转到登陆成功页面,否则跳转到登录失败页面 if(userDAO.usersLogin(loginUser)) { session.setAttribute("loginUser", loginUser.getUsername()); request.getRequestDispatcher("login_success.jsp").forward(request, response); } else { response.sendRedirect("login_failure.jsp"); } %>login_success.jsp
<%@ page language="java" import="java.util.*" contentType="text/html; charset=utf-8"%> <% String path = request.getContextPath(); String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; %> <html> <head> <!-- Page title --> <title>欢迎登录</title> <!-- End of Page title --> <!-- Libraries --> <link type="text/css" href="css/login.css" rel="stylesheet" /> <link type="text/css" href="css/smoothness/jquery-ui-1.7.2.custom.html" rel="stylesheet" /> <script type="text/javascript" src="js/jquery-1.3.2.min.js"></script> <script type="text/javascript" src="js/easyTooltip.js"></script> <script type="text/javascript" src="js/jquery-ui-1.7.2.custom.min.js"></script> <!-- End of Libraries --> </head> <body> <div id="container"> <div class="logo"> <a href="#"><img src="images/8.jpg" alt="" /></a> </div> <div id="box"> <% String loginUser = ""; if(session.getAttribute("loginUser")!=null) { loginUser = session.getAttribute("loginUser").toString(); } %> <br> <br> <Strong>欢迎您<font color="red"><%=loginUser%></font>,登录成功!</Strong> </div> </div> </body> </html>login_failure.jsp
<%@ page language="java" import="java.util.*" contentType="text/html; charset=utf-8"%> <% String path = request.getContextPath(); String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; %> <html> <head> <!-- Page title --> <title>登录失败</title> <!-- End of Page title --> <!-- Libraries --> <link type="text/css" href="css/login.css" rel="stylesheet" /> <link type="text/css" href="css/smoothness/jquery-ui-1.7.2.custom.html" rel="stylesheet" /> <script type="text/javascript" src="js/jquery-1.3.2.min.js"></script> <script type="text/javascript" src="js/easyTooltip.js"></script> <script type="text/javascript" src="js/jquery-ui-1.7.2.custom.min.js"></script> <!-- End of Libraries --> </head> <body> <div id="container"> <div class="logo"> <a href="#"><img src="images/9.jpg" alt="" /></a> </div> <br> <div id="box"><strong> 登录失败!请检查用户或者密码!<br><br> <a href="login.jsp">返回登录</strong></a> </div> </div> </body> </html>