文章详情

  • 游戏榜单
  • 软件榜单
关闭导航
热搜榜
热门下载
热门标签
php爱好者> php文档>jsp+javabean+servlet练习

jsp+javabean+servlet练习

时间:2010-07-03  来源:cj_gameboy

实现登录与注销界面:程序如下:

文件: demo1.rar
大小: 666KB
下载: 下载

1.index.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.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">

</head>

<body>
<form name="form1" action="UserServlet.do?method=login" method="post">
<table width="200" border="1">
    <tr>
        <td colspan="2">登录窗口</td>
    <tr>
        <td>用户名</td>
        <td><input type="text" name="username" size="10"></td>
    </tr>
    <tr>
        <td>密码</td>
        <td><input type="password" name="password" size="10"></td>
    </tr>
    <tr>
        <td colspan="2"><input type="submit" name="submit" value="登录"> <a
            href="register.jsp">注册新用户</a></td>
    </tr>
</table>
</form>

</body>
</html>


2.register.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 'register.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>
<form name="form1" action="UserServlet.do?method=register" method="post">
<table width="200" border="1">
    <tr>
        <td colspan="2">注册窗口</td>
    <tr>
        <td>用户名</td>
        <td><input type="text" name="username" size="10"></td>
    </tr>
    <tr>
        <td>密码</td>
        <td><input type="password" name="password1" size="10"></td>
    </tr>
    <tr>
        <td>确认密码</td>
        <td><input type="password" name="password2" size="10"></td>
    </tr>
    <tr>
        <td>Email</td>
        <td><input type="text" name="email" size="10"></td>
    </tr>
    <tr>
        <td colspan="2"><input type="submit" name="submit" value="登录"> <a
            href="login.jsp">返回</a></td>
    </tr>
</table>
</form>

</body>
</html>

3.welcome.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 'welcome.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">

</head>

<body>
<table width="100%">
    <tr>
        <td><img src="images/logo4.png"></td>
        <td><img src="images/logo2.png" height="90"></td>
    </tr>
    <tr>
        <td colspan="2">
        <hr>
        </td>
    </tr>
    <tr>
        <td>
        <table>
            <tr>
                <td><a href="welcome.jsp">Main</a></td>
            </tr>
            <tr>
                <td><a href="menu1.jsp">Menu1</a></td>
            </tr>
            <tr>
                <td><a href="menu2.jsp">Menu2</a></td>
            </tr>
            <tr>
                <td><a href="menu3.jsp">Menu3</a></td>
            </tr>
            <tr>
                <td><a href="menu4.jsp">Menu4</a></td>
            </tr>
            <tr>
                <td><a href="menu5.jsp">Menu5</a></td>
            </tr>
            <tr>
                <td><a href="menu6.jsp">Menu6</a></td>
            </tr>
            <tr>
                <td><a href="menu7.jsp">Menu7</a></td>
            </tr>
            <tr>
                <td><a href="menu8.jsp">Menu8</a></td>
            </tr>
        </table>
        </td>
        <td>
        <form name="form1" action="UserServlet.do?method=logout" method="post">
        <table width="200" border="1">
            <tr>
                <td colspan="2">登录成功</td>
            <tr>
                <td>欢迎你,</td>
                <td><%=(String) session.getAttribute("username")%></td>
            </tr>
            <tr>
                <td colspan="2"><input type="submit" name="submit" value="退出"></td>
            </tr>
        </table>
        </form>
        </td>
    </tr>
</table>

</body>
</html>

4. com.demo.servlet:UserServlet

package com.demo.servlet;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.demo.javabean.UserBean;

public class UserServlet extends HttpServlet {

    protected void doPost(HttpServletRequest request,
            HttpServletResponse response) throws ServletException, IOException {
        doGet(request, response);
    }
    
    protected void doGet(HttpServletRequest request,
            HttpServletResponse response) throws ServletException, IOException {
        String method = (String)request.getParameter("method");
        if(method==null) {
            PrintWriter out = response.getWriter();
            out.println("invalid request!");
        } else if(method.equals("login")) {
            Login(request, response);
        } else if(method.equals("logout")) {
            Logout(request, response);
        } else if(method.equals("register")) {
            Register(request, response);
        }
    }

    protected void Login(HttpServletRequest request,
            HttpServletResponse response) throws ServletException, IOException {
        // get parameters
        String username = request.getParameter("username");
        String password = request.getParameter("password");

        // check null
        if (username == null || password == null) {
            response.sendRedirect("index.jsp");
            return;
        }

        // validate
        UserBean userBean = new UserBean();
        boolean isValid = userBean.valid(username, password);

        if (isValid) {
            HttpSession session = request.getSession();
            session.setAttribute("username", username);
            response.sendRedirect("welcome.jsp");
            return;
        } else {
            response.sendRedirect("index.jsp");
            return;
        }
    }

    protected void Logout(HttpServletRequest request,
            HttpServletResponse response) throws ServletException, IOException {
        HttpSession session = request.getSession();
        session.removeAttribute("username");
        response.sendRedirect("index.jsp");
    }

    protected void Register(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        // get parameters
        String username = request.getParameter("username");
        String password1 = request.getParameter("password1");
        String password2 = request.getParameter("password2");
        String email = request.getParameter("email");

        // check null
        if (username == null || password1 == null || password2 == null || !password1.equals(password2)) {
            response.sendRedirect("register.jsp");
            return;
        }

        // validate
        UserBean userBean = new UserBean();
        boolean isExist = userBean.isExist(username);
        if(!isExist) {
            userBean.add(username, password1, email);
            response.sendRedirect("index.jsp");
            return;
        } else {
            response.sendRedirect("register.jsp");
            return;
        }
    }

}


5. com.demo.javabean:DBAccess.java

package com.demo.javabean;

public class UserBean {

    public boolean valid(String username, String password) {
        boolean isValid = false;
        DBAccess db = new DBAccess();
        if(db.createConn()) {
            String sql = "select * from user where username='"+username+"' and password='"+password+"'";
            db.query(sql);
            if(db.next()) {
                isValid = true;
            }
            db.closeRs();
            db.closeStm();
            db.closeConn();
        }
        return isValid;
    }
    
    public boolean isExist(String username) {
        boolean isExist = false;
        DBAccess db = new DBAccess();
        if(db.createConn()) {
            String sql = "select * from user where username='"+username+"'";
            db.query(sql);
            if(db.next()) {
                isExist = true;
            }
            db.closeRs();
            db.closeStm();
            db.closeConn();
        }
        return isExist;
    }
    
    public void add(String username, String password, String email) {
        DBAccess db = new DBAccess();
        if(db.createConn()) {
            String sql = "insert into user(username,password,email) values('"+username+"','"+password+"','"+email+"')";
            db.update(sql);
            db.closeStm();
            db.closeConn();
        }
    }
}

6. com.demo.javabean:UserBean.java

package com.demo.javabean;

public class UserBean {

    public boolean valid(String username, String password) {
        boolean isValid = false;
        DBAccess db = new DBAccess();
        if(db.createConn()) {
            String sql = "select * from user where username='"+username+"' and password='"+password+"'";
            db.query(sql);
            if(db.next()) {
                isValid = true;
            }
            db.closeRs();
            db.closeStm();
            db.closeConn();
        }
        return isValid;
    }
    
    public boolean isExist(String username) {
        boolean isExist = false;
        DBAccess db = new DBAccess();
        if(db.createConn()) {
            String sql = "select * from user where username='"+username+"'";
            db.query(sql);
            if(db.next()) {
                isExist = true;
            }
            db.closeRs();
            db.closeStm();
            db.closeConn();
        }
        return isExist;
    }
    
    public void add(String username, String password, String email) {
        DBAccess db = new DBAccess();
        if(db.createConn()) {
            String sql = "insert into user(username,password,email) values('"+username+"','"+password+"','"+email+"')";
            db.update(sql);
            db.closeStm();
            db.closeConn();
        }
    }
}


相关阅读 更多 +
排行榜 更多 +
辰域智控app

辰域智控app

系统工具 下载
网医联盟app

网医联盟app

运动健身 下载
汇丰汇选App

汇丰汇选App

金融理财 下载