学习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://"