注册页面:

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8" %>
<!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=UTF-8">
<title>Insert title here</title>
</head>
<body>
    <form action="chuli.jsp" method="post">
        <input type="hidden" name="sub_type" value="reg" />
        <input type="text" name="username" placeholder="输入用户名" />
        <input type="password" name="password" placeholder="输入密码" />
        <input type="password" name="password1" placeholder="确认密码" />
        <input type="submit" value="注册" /><a href="denglu.jsp">返回登陆</a>
    </form>
</body>
</html>

登录页面:

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!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=UTF-8">
<title>Insert title here</title>
</head>
<body>
    <form action="chuli.jsp" method="post">
        <input type="hidden" name="sub_type" value="log" />
        <input type="text" name="username" placeholder="输入用户名" />
        <input type="password" name="password" placeholder="输入密码" />
        <input type="submit" value="登录" /><a href="zhuce.jsp">注册新用户</a>
    </form>
</body>
</html>

主页-留言板页:

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8" import="com.util.User,java.util.*,com.test.*,com.util.*"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<%

    Object obj1 = session.getAttribute("currentUser");
    if (obj1 == null) {
        response.sendRedirect("denglu.jsp");
    }
%>
<%
request.setCharacterEncoding("utf-8");
response.setCharacterEncoding("utf-8");
%>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
    <form action="chuli.jsp" method="post" >
        <input type="hidden" name="sub_type" value="tex" />
        <textarea rows="4" cols="12" name="text"></textarea>
        <input type="submit" value="提交">
    </form>
<%
MethodDal m=new MethodDal();

List<Message> list=m.selectMessage();

if(list!=null){

    for(Message l: list ){
        out.print(l.getUname()+"  |  ");
        out.print(l.getDate()+"<br>");
        out.print(l.getSays()+"<hr>");
    }
}
%>
</body>
</html>

逻辑处理页面:

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8" import="com.util.User,com.util.Message,java.util.*,com.test.*" errorPage="error.jsp" %>
<%
request.setCharacterEncoding("utf-8");
response.setCharacterEncoding("utf-8");
%>
<%
    String sub_type = request.getParameter("sub_type");
    String username = request.getParameter("username");
    String password = request.getParameter("password");
    String password1 = request.getParameter("password1");
    MethodDal m=new MethodDal();
    Message says=new Message();

    if("reg".equals(sub_type)) {

        if (!password.equals(password1)) {
            out.print("两次输入的密码不一致 !");
        } else {
            String selectname=m.selectName(username);
            if (!selectname.equals("no")) {
                out.print("用户名已经存在 !");
            } else {
                User user = new User();
                user.setUsername(username);
                user.setPassword(password);

                m.insertData(user);
                out.print("注册成功 !");
            }
        }
        out.print("<br>");
        out.print("<a href='denglu.jsp'>跳转登陆</a>");
    }
    if("log".equals(sub_type)) {
        String selectname=m.selectName(username);
        application.setAttribute("name",username);
        String spwd=m.selectPwd(password);

        if(username.equalsIgnoreCase(selectname)&&password.equalsIgnoreCase(spwd)) {
            session.setAttribute("currentUser", 1);
            response.sendRedirect("Maintest.jsp");

        } else {
            out.print("用户名或密码不正确 !");
        }
        out.print("<br>");
        out.print("<a href='denglu.jsp'>跳转登陆</a>");
    }
    if("tex".equals(sub_type)){
        String say = request.getParameter("text");
        Object sobj=application.getAttribute("name");//获取用户名
        String sname=(String)sobj;
        Date d=new Date();

        System.out.println(sname);
        says.setUname(sname);
        says.setSays(say);
        says.setDate(new Date().toLocaleString());

        int i=m.insertSays(says);

        response.sendRedirect("Maintest.jsp");
    }

%>

JDBC设置工具类:

package com.util;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;

import com.util.JdbcConnectionUtil;

public class JdbcConnectionUtil {
    private static final String USERNAME = "test";
    private static final String PASSWORD = "test";
    private static final String URL = "jdbc:oracle:thin:@localhost:1521:xe";
    private static final String DRIVERCLASSNAME = "oracle.jdbc.OracleDriver";

    public static Connection getConnection(){
        Connection conn=null;
        try {
            Class.forName(DRIVERCLASSNAME);
            conn=DriverManager.getConnection(URL,USERNAME,PASSWORD);
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return conn;
    }
    public static void destroy(Connection conn){
        if(conn!=null){
            try {
                conn.close();
                conn=null;
            } catch (SQLException e) {
                e.printStackTrace();
            }

        }
    }
    public static void main(String[] args) {
        System.out.println(JdbcConnectionUtil.getConnection());
    }
}

数据库增删改查工具类:

package com.test;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import com.util.JdbcConnectionUtil;
import com.util.Message;
import com.util.User;

public class MethodDal {
    private Connection con;
    private PreparedStatement pste;
    private ResultSet rs;
    //向表中添加数据
    public int insertData(){
        init();
        int i=-1;
        String sql="insert into puser values('c','c')";

        try {
            pste=con.prepareStatement(sql);
            i=pste.executeUpdate();//针对于增删改数据吗,返回受影响的行数
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return i;
    }
    //向表中添加数据
    public int insertData(User user){
        init();
        int i=-1;
        String sql="insert into puser values(?,?)";
        try {
            pste=con.prepareStatement(sql);
            pste.setString(1,user.getUsername());
            pste.setString(2, user.getPassword());
            i=pste.executeUpdate();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return i;
    }
    //删除记录
    public int deleteDate(int id){

        return 0;
    }
    //查现有姓名
    public String selectName(String name){
        init();
        String sql="select * from puser p where p.pname=?";
        try {
            pste=con.prepareStatement(sql);
            pste.setString(1,name);
            rs = pste.executeQuery();
            while(rs.next()) {
                String pname=rs.getString("pname");
                if(pname!=null){
                    return pname;
                }
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return "no";
    }
    //根据姓名查密码
    public String selectPwd(String name){
        init();
        String sql="select * from puser p where p.pname=?";
        try {
            pste=con.prepareStatement(sql);
            pste.setString(1,name);
            rs = pste.executeQuery();
            while(rs.next()) {
                String pwd=rs.getString("ppassword");
                return pwd;
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return "wu";
    }
    //根据添加信息
    public int insertSays(Message m){
        init();
        int i=-1;
        String sql="insert into pmessage values(?,?,?)";
        try {
            pste=con.prepareStatement(sql);
            pste.setString(1,m.getUname());
            pste.setString(3,m.getDate());
            pste.setString(2,m.getSays());
            i=pste.executeUpdate();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return i;
    }

    //查询所有数据
     public List<Message> selectMessage() {
            String sql = "select pname,says,ptime from pmessage order by ptime desc";
            init();
            List<Message> list = new ArrayList<Message>();
            try {
                pste = con.prepareStatement(sql);
                rs = pste.executeQuery();
                while (rs.next()) {

                    Message au = new Message();
                    au.setUname(rs.getString(1));
                    au.setSays(rs.getString(2));
                    au.setDate(rs.getString(3));
                    list.add(au);
                }

            } catch (SQLException e) {
                e.printStackTrace();
            }
            return list;
        }
    public void init(){
        con=JdbcConnectionUtil.getConnection();
    }
}

工具类-留言信息:

package com.util;

public class Message {
        private String uname;
        private String date;
        private String says;

        public String getUname() {
            return uname;
        }
        public void setUname(String uname) {
            this.uname = uname;
        }
        public String getDate() {
            return date;
        }
        public void setDate(String date) {
            this.date = date;
        }
        public String getSays() {
            return says;
        }
        public void setSays(String says) {
            this.says = says;
        }
        @Override
        public String toString() {
            return "Says [uname=" + uname + ", date=" + date + ", says=" + says + "]";
        }
}

工具类-用户信息:

package com.util;

public class User {
    private String username;
    private String password;

    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;
    }
}

JavaWeb 例子 JDBC+JSP登陆注册留言板的更多相关文章

  1. JSP简易留言板

    写在前面 在上篇博文JSP内置对象中介绍JSP的9个内置对象的含义和常用方法,但都是比较理论的知识.今天为大家带来一个小应用,用application制作的简易留言板. 包括三个功能模块:留言提交.留 ...

  2. 使用jsp,tomcat实现用户登录注册留言的代码

    以下jsp中,未使用样式表对网页进行排版和表单的验证(每个jsp的表单填写的时候应该进行空值与空格的验证,防止提交时出错) 所有错误,链接到error.jsp <%@ page language ...

  3. jsp中运用application实现共享留言板功能

    jsp中application的知识点总结: 1.一个Web应用程序启动后,将会自动创建一个application对象,在整个应用程序的运行过程中只有这一个application对象,即所有访问该网站 ...

  4. jsp 用application对象制作留言板

    <%@ page contentType="text/html; charset=gb2312"%> <html> <body> <for ...

  5. javaweb 登陆注册页面

    视图的数据修改,表中也修改引用工具类用<%@ page import=""%> <%@ page import="java.util.Date" ...

  6. 用户登陆注册【JDBC版】

    前言 在讲解Web开发模式的时候,曾经写过XML版的用户登陆注册案例!现在在原有的项目上,使用数据库版来完成用户的登陆注册!如果不了解的朋友,可以看看我Web开发模式的博文! 本来使用的是XML文件作 ...

  7. 一、JDBC的概述 二、通过JDBC实现对数据的CRUD操作 三、封装JDBC访问数据的工具类 四、通过JDBC实现登陆和注册 五、防止SQL注入

    一.JDBC的概述###<1>概念 JDBC:java database connection ,java数据库连接技术 是java内部提供的一套操作数据库的接口(面向接口编程),实现对数 ...

  8. jsp留言板雏形

    编写一个简单的留言簿,实现添加留言和显示留言内容的功能 <%@ page language="java" contentType="text/html; chars ...

  9. PHP基础知识总结(四) 留言板例子 知识应用

    1.留言板显示页面:note.php <?php $host = "127.0.0.1"; $user = "root"; $pwd = "zs ...

随机推荐

  1. 程序、计算机程序、java初论

    一.程序? 程序一词来自生活,通常指完成某些事情的一种既定方式和过程,可以将程序看成对一系列动作的执行过程的描述. 例如:个人去银行取钱 1.带上存折/银行卡去银行 2.取号排队 3.将存折或储蓄卡递 ...

  2. Hibernate持久化对象的三种状态深入理解

    关于OID hibernate缓存是一个map,他会根据OID作为缓存对象的key,我们的映射文件中<id>标签指定的属性值会作为OID 持久化对象的三种状态 为了方便理解,Hiberna ...

  3. RecyclerView 加入一个滑动控件bug处理 GridView只显示第一行

    如果RecyclerView 多样式布局,比如要加入一个展示多个图看的需求.自然想到用gridview给嵌套一下. 想法当然是可以的,但是发现,嵌套出来的效果是,gridview只显示一行. 想想原因 ...

  4. params SqlParameter[] commandParameters(转)

    C#代码  ExecuteReader(string connectionString, CommandType commandType, string commandText, params Sql ...

  5. HTML基础下

    知识点一: HTML5的标准结构: <!DOCTYPE html> <html lang='en'> <head> <meat charset='utf-8' ...

  6. js监听浏览器离开页面操作

    序言 大家是否经常遇到在关闭网页的时候,会看到一个确定是否离开当前页面的提示框?想一些在线测试系统.信息录入系统等就经常会有这一些提示,避免用户有意或者无意中关掉了页面,导致数据丢失.这里面的实现过程 ...

  7. Python中的选择排序

    选择排序 选择排序(Selection sort)是一种简单直观的排序算法.它的工作原理如下.首先在未排序序列中找到最小(大)元素,存放到排序序列的起始位置,然后,再从剩余未排序元素中继续寻找最小(大 ...

  8. HDU4508--完全背包

    湫湫系列故事--减肥记I Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 65535/32768 K (Java/Others) Tot ...

  9. 基于UDP协议的socket编程

    UDP协议特点: 1.无连接.服务端与客户端传输数据之前不需要进行连接,且没有超时重发等机制,只是把数据通过网络发送出去.也正是因为此特点,所以基于UDP协议的socket的客户端在启动之前不需要先启 ...

  10. Netty4 学习笔记之三:粘包和拆包

    前言 在上一篇Netty 心跳 demo 中,了解了Netty中的客户端和服务端之间的心跳.这篇就来讲讲Netty中的粘包和拆包以及相应的处理. 名词解释 粘包: 会将消息粘粘起来发送.类似吃米饭,一 ...