五(2)、JSP——JavaBean例子

来源:互联网 发布:c语言阶乘怎么写 编辑:程序博客网 时间:2024/06/13 19:00

      • JavaBean例子
        • 一创建项目DeMologin项目
        • 二创建loginjsp
        • 三在src中创建compo包和User类
        • 四在src中创建comdao包和UserDao类
        • 五创建dologinjsp
        • 六创建login_successjsp
        • 七创建login_failurejsp

JavaBean例子

一、创建项目DeMologin项目

这里写图片描述

二、创建login.jsp

<body>   <form action="dologin.jsp" method="post">  用户名:<input type="text" name="username">  <br>  密码:<input type="password" name="password"><br>   <input type="submit" name="登入">   </form></body>

三、在src中创建com.po包和User类

package com.po;public class User {    private String username;    private String password;    public User(){    }    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;    }}

四、在src中创建com.dao包和UserDao类

package com.dao;import com.po.User;public class UserDao {    public boolean usersLogin(User u)    {        if("admin".equals(u.getUsername())&&"admin".equals(u.getPassword()))        {            return true;        }        else         {            return false;        }    }}

五、创建dologin.jsp

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%><%@ page import="com.po.*,com.dao.*"%><%    String path = request.getContextPath();    String basePath = request.getScheme() + "://"            + request.getServerName() + ":" + request.getServerPort()            + path + "/";%><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"><html>    <head>        <base href="<%=basePath%>">        <title>My JSP 'index.jsp' starting page</title>        <meta http-equiv="pragma" content="no-cache">        <meta http-equiv="cache-control" content="no-cache">        <meta http-equiv="expires" content="0">        <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">        <meta http-equiv="description" content="This is my page">        <!--    <link rel="stylesheet" type="text/css" href="styles.css">    -->    </head>    <body>        <%            request.setCharacterEncoding("utf-8");        %>        <jsp:useBean id="loginUser" class="com.po.User" scope="page"></jsp:useBean>        <jsp:useBean id="userDAO" class="com.dao.UserDao" scope="page"></jsp:useBean>        <jsp:setProperty property="*" name="loginUser" />        <%            if (userDAO.usersLogin(loginUser)) {                session.setAttribute("loginUser", loginUser.getUsername());                request.getRequestDispatcher("/login_success.jsp").forward(                        request, response);            } else {                response.sendRedirect("login_failure.jsp");            }        %>    </body></html>

六、创建login_success.jsp

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%><%    String path = request.getContextPath();    String basePath = request.getScheme() + "://"            + request.getServerName() + ":" + request.getServerPort()            + path + "/";%><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"><html>    <head>        <base href="<%=basePath%>">        <title>My JSP 'login_success.jsp' starting page</title>        <meta http-equiv="pragma" content="no-cache">        <meta http-equiv="cache-control" content="no-cache">        <meta http-equiv="expires" content="0">        <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">        <meta http-equiv="description" content="This is my page">        <!--    <link rel="stylesheet" type="text/css" href="styles.css">    -->    </head>    <body>        <%            String loginUser = " ";            if (session.getAttribute("loginUser") != null) {                loginUser = session.getAttribute("loginUser").toString();            }        %>        欢迎<%=loginUser%>登入成功        <br />        <a href="loginout.jsp">注销</a>    </body></html>

七、创建login_failure.jsp

    <body>        登入失败        <a href="login.jsp">返回</a>    </body>