简单的在线留言系统

本文分享了一个简单的在线留言系统的实现过程,包括用户注册、登录、留言、回复、编辑和删除等功能。通过JSP和Java类实现了数据封装、数据库操作,并提供了页面展示和交互。附带工程结构图和数据库表结构。
摘要由CSDN通过智能技术生成

学习Java Web有一段时间了,借此机会和大家分享一个小东西——在线留言系统,虽然low,小喷即可,大喷伤心,还是希望大家的指正和意见,话不多说,直接上:

第一步:

①.写一个注册页面,这一个页面主要是用户的输入,比较简单,代码如下:

regist.jsp
<%@ page language="java" contentType="text/html; charset=GBK"
    pageEncoding="GBK"%>
<!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=GBK">
<title>注册页面</title>
</head>
<body>
<form method = "POST" action = "registdo.jsp">
用户名:<input type = "text" name= "username">
密码:<input type = "password" name = "userpass">
<input type = "submit" value = "注册"> 
<input type = "reset" value = "重置"> 
</form>
</body>
</html>

效果如图:
这里写图片描述

②.下面这个页面主要负责用来处理业务逻辑以及显示注册的结果,代码如下:

registdo.jsp
<%@page import="com.szx.jnmc.UserDao"%>
<%@page import="java.sql.ResultSet"%>
<%@page import="com.szx.jnmc.DBOper"%>
<%@page import="java.io.PrintWriter"%>
<%@ page language="java" contentType="text/html; charset=GBK"
    pageEncoding="GBK"%>
    <%@page import="java.util.List"%>
    <%@page import="com.szx.jnmc.User"%>
<!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=GBK">
<title>留言板</title>
</head>
<body>
<%
    request.setCharacterEncoding("GBK");
    response.setContentType("text/html;charset=gbk");
    /* PrintWriter out = response.getWriter();  */
    String username = request.getParameter("username");
    String userpass = request.getParameter("userpass");
    ServletContext ctx = this.getServletContext();
    String server = ctx.getInitParameter("server");
    String dbname = ctx.getInitParameter("dbname");
    String dbuser = ctx.getInitParameter("dbuser");
    String dbpwd = ctx.getInitParameter("dbpwd");
    UserDao dao = new UserDao();
    User user = new User();
    user.setUsername(username);
    user.setUserpass(userpass);
    try{
        dao.getConn(server, dbname, dbuser, dbpwd);
        if(dao.addUser(user)){
            out.println("注册成功");
            out.println("<br><a href = 'login.jsp'>返回登陆</a></br>");
        }else{
            out.println("注册失败");
            out.println("<br><a href = 'regist.jsp'>返回注册</a></br>");
        }
    }catch(Exception e){
        e.printStackTrace();
    }
%>
</body>
</html>

效果如图:
这里写图片描述

第二步:

①.这一步写个登录页面,主要用来接收用户输入的登录信息:代码如下:

login.jsp
<%@ page language="java" contentType="text/html; charset=GBK"
    pageEncoding="GBK"%>
<!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=GBK">
<title>留言板</title>
</head>
<body>
<form method = "POST" action = "logindo.jsp">
用户名:<input type = "text" name = "username">
密码:<input type = "password" name = "userpass">
<input type = "submit" value= "登录">
<input type = "reset"value = "重置">
</form>
</body>
</html>

效果如图
这里写图片描述

②.这一步是需要写登录的逻辑及验证,并显示登录的结果,代码如下

logindo.jsp
<%@page import="java.sql.ResultSet"%>
<%@ page language="java" contentType="text/html; charset=gbk"
    pageEncoding="gbk"%>
    <%@page import = "com.szx.jnmc.DBOper" %>
    <%@page import = "javax.servlet.http.Cookie" %>
    <%@page import = "javax.servlet.RequestDispatcher" %>
    <%@page import = "javax.servlet.http.HttpSession" %>
<!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=gbk">
<title>留言板</title>
</head>
<body>
<%
    request.setCharacterEncoding("GBK");
    response.setContentType("text/html;charset=GBK");

    String username = request.getParameter("username");
    String userpass = request.getParameter("userpass");
    ServletContext ctx = request.getServletContext();
    String server = ctx.getInitParameter("server");
    String dbname = ctx.getInitParameter("dbname");
    String dbuser = ctx.getInitParameter("dbuser");
    String dbpwd = ctx.getInitParameter("dbpwd");
    DBOper db = new DBOper();
    String sql = "SELECT * FROM user Where username = ? AND userpass = ?";
    try{
        db.getConn(server, dbname, dbuser, dbpwd);
        ResultSet rs = db.executeQuery(sql, new String[]{username,userpass});
        if(rs!= null && rs.next()){

            session.setAttribute("username", username);
            Cookie cookie  = new Cookie("username",username);
            cookie.setMaxAge(60*60*24*30);
            response.addCookie(cookie);
            RequestDispatcher dispatcher = request.getRequestDispatcher("userlist.jsp");
            dispatcher.forward(request, response);
        }else{
            out.println("登录失败!");
            out.println("<br><a href = 'login.jsp'>重新登录</a></br>");
        }
    }catch(Exception e){
        e.printStackTrace();
    }finally{
        db.closeAll();
    }
%>
</body>
</html>

效果如图
这里写图片描述

前台的注册及登录页面到此已完成,下面我们接着做后台的数据

第二步

①.我们需要User类封装用户的信息,代码如下:

User.Java
package com.szx.jnmc;

public class User {
    private int id;
    private String username;
    private String userpass;
    private String phone;   
    private String email;
    public int getId() {
        return id;
    }
    public void setId(int id) {
        this.id = id;
    }
    public String getUsername() {
        return username;
    }
    public void setUsername(String username) {
        this.username = username;
    }
    public String getUserpass() {
        return userpass;
    }
    public void setUserpass(String userpass) {
        this.userpass = userpass;
    }
    public String getPhone() {
        return phone;
    }
    public void setPhone(String phone) {
        this.phone = phone;
    }
    public String getEmail() {
        return email;
    }
    public void setEmail(String email) {
        this.email = email;
    }

}

②.有了User类,就需要进行数据库的访问,这时候我们需要一个连接数据库的基础类,所有与连接数据库的类都要 继承于此,代码如下:

DBOper.Java
    public Connection conn = null;
    public PreparedStatement ps = null;
    public ResultSet rs = null;
    /**
     * 连接数据库
     * @param server
     * @param dbname
     * @param dbuser
     * @param dbpwd
     * @return
     * @throws SQLException
     * @throws ClassNotFoundException
     */
    public Connection getConn(String server,String dbname,String dbuser,String dbpwd) throws SQLException, ClassNotFoundException{      
        String DRIVER = "com.mysql.jdbc.Driver";
        String URL = "jdbc:mysql://"
评论 8
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值