java MVC 公共留言板,简易
登录
<%
if(request.getAttribute("err")!=null){
out.println(request.getAttribute("err") + "
");
}
%>
用户登录
没有账号?注册
register.jsp(注册)
注册
<%
if(request.getAttribute("err")!=null){
out.println(request.getAttribute("err") + "
");
}
%>
show.jsp(当前用户信息)
Java Web
<%
try{
String name = (String)session.getAttribute("name");
%>
<%
if(name==null){
%>
登录
注册
<%}else{ %>
<%=name %>你好!
退出
<%} %>
<%
}catch(Exception e){
e.printStackTrace();
}
%>
首页
用户信息 查看日历 公共留言 好友列表 私人留言
<%
String name = (String)session.getAttribute("name");
String pass = (String)session.getAttribute("pass");
%>
查看用户信息
更新用户信息
index.jsp(导航栏)
JSP Page
Java Web
<%
try{
String name = (String)session.getAttribute("name");
%>
<%
if(name==null){
%>
登录
注册
<%}else{ %>
<%=name %>你好!
退出
<%} %>
<%
}catch(Exception e){
e.printStackTrace();
}
%>
首页
用户信息 查看日历 公共留言 好友列表 私人留言
这是首页哦!
<%
if(request.getAttribute("err")!=null){
out.println(request.getAttribute("err") + "
");
}
%>
update.jsp(更新用户)
Java Web
<%
try{
String username = (String)session.getAttribute("name");
%>
<%
if(username==null){
%>
登录
注册
<%}else{ %>
<%=username %>你好!
退出
<%} %>
<%
}catch(Exception e){
e.printStackTrace();
}
%>
首页
用户信息 查看日历 公共留言 好友列表 私人留言
update
<%
String name = (String)session.getAttribute("name");
String pass = (String)session.getAttribute("pass");
%>
<%
if(request.getAttribute("err")!=null){
out.println(request.getAttribute("err") + "
");
}
%>
Message.jsp(留言)
Java Web
<%
try{
String name = (String)session.getAttribute("name");
%>
<%
if(name==null){
%>
登录
注册
<%}else{ %>
<%=name %>你好!
退出
<%} %>
<%
}catch(Exception e){
e.printStackTrace();
}
%>
首页
用户信息 查看日历 公共留言 好友列表 私人留言
JSP Page
<%
if(request.getAttribute("err")!=null){
out.println(request.getAttribute("err") + "
");
}
%>
MessageShow.jsp(留言展示)
<%
ResultSet rs = (ResultSet)request.getAttribute("r");
%>
Java Web
<%
try{
String name = (String)session.getAttribute("name");
%>
<%
if(name==null){
%>
登录
注册
<%}else{ %>
<%=name %>你好!
退出
<%} %>
<%
}catch(Exception e){
e.printStackTrace();
}
%>
首页
用户信息 查看日历 公共留言 好友列表 私人留言
JSP Page
留言内容
ID 标题 作者 内容
<%
while(rs.next()){
out.print("");
for(int i=1;i<=4;i++){
out.print(""+rs.getString(i)+" ");
}
out.print(" ");
}
%>
去留言
<%
try{
if(rs!=null){
rs.close();
}
}catch(Exception e){
System.out.println("at show.jsp");
System.out.println(e);
}
%>
Calendar.jsp(日历)
查看日历
Java Web
<%
try{
String name = (String)session.getAttribute("name");
%>
<%
if(name==null){
%>
登录
注册
<%}else{ %>
<%=name %>你好!
退出
<%} %>
<%
}catch(Exception e){
e.printStackTrace();
}
%>
首页
用户信息 查看日历 公共留言 好友列表 私人留言
dao包下
CalendarBean.java(日历控制类)
public class CalendarBean {
private String calendar=null;
private int year=2005;
private int month=0;
/**
* @return the calendar
*/
public String getCalendar() {
StringBuffer buffer=new StringBuffer();
Calendar calend=Calendar.getInstance();
//将日历翻到year年month-1月1日,注意0表示一个月
//一次类推11表示12月
calend.set(year, month-1,1);
//获取1日是星期几(get方法返回的值是一表示星期日,值是7表示的是星期六
int week=calend.get(Calendar.DAY_OF_WEEK)-1;
int day=0;
if(month==1||month==3||month==5||month==7||month==8||month==10||month==12){
day=31;
}
if(month==4||month==6||month==9||month==11){
day=30;
}
if(month==2){//判断闰年
if(year%4==0&&year%100!=0||year%400==0){
day=29;
}else{
day=28;
}
}
String a[]=new String[42]; //存放号码的一维数组
for(int i=0;i<42;i++){
a[i]=" ";
}
for(int i=week,n=1;i");
buffer.append("");
String[] xingqi= {"星期日","星期一","星期二","星期三","星期四","星期五","星期六"};
for(int k=0;k<7;k++){
buffer.append(""+xingqi[k]+" ");
}
buffer.append(" ");
for(int k=0;k<42;k=k+7){
buffer.append("");
for(int j=k;j<7+k;j++){
buffer.append(""+a[j]+" ");
}
buffer.append(" ");
}
buffer.append("");
calendar=new String(buffer);
return calendar;
}
/**
* @param calendar the calendar to set
*/
public void setCalendar(String calendar) {
this.calendar = calendar;
}
/**
* @return the year
*/
public int getYear() {
return year;
}
/**
* @param year the year to set
*/
public void setYear(int year) {
this.year = year;
}
/**
* @return the month
*/
public int getMonth() {
return month;
}
/**
* @param month the month to set
*/
public void setMonth(int month) {
this.month = month;
}
}
DbDao(数据库底层类)
public class DbDao {//数据库底层
private Connection conn;
private String driver;
private String url;
private String username;
private String pass;
public DbDao(){
}
public DbDao(String driver, String url, String username, String pass) {
this.driver = driver;
this.url = url;
this.username = username;
this.pass = pass;
}
public Connection getConn() {
return conn;
}
public void setConn(Connection conn) {
this.conn = conn;
}
public String getDriver() {
return driver;
}
public void setDriver(String driver) {
this.driver = driver;
}
public String getUrl() {
return url;
}
public void setUrl(String url) {
this.url = url;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPass() {
return pass;
}
public void setPass(String pass) {
this.pass = pass;
}
public Connection getConnection() throws Exception {//连接对象
if(conn == null){
Class.forName(this.driver);
conn = (Connection) DriverManager.getConnection(url, username, this.pass);
}
return conn;
}
public boolean insert(String sql, Object... args) throws Exception{//插入
PreparedStatement pstmt = (PreparedStatement) getConnection().prepareStatement(sql);
for(int i = 0; i < args.length; i++) {
pstmt.setObject(i + 1, args[i]);
}
if(pstmt.executeUpdate() != 1)
return false;
return true;
}
public boolean update(String sql, Object... args) throws Exception{//更新
PreparedStatement pstmt = (PreparedStatement) getConnection().prepareStatement(sql);
for(int i = 0; i < args.length; i++) {
pstmt.setObject(i + 1, args[i]);
}
if(pstmt.executeUpdate() != 1)
return false;
return true;
}
public ResultSet query(String sql, Object...args) throws Exception{//查询结果集
PreparedStatement pstmt = (PreparedStatement) getConnection().prepareStatement(sql);
for(int i=0; i< args.length; i++) {
pstmt.setObject(i+1, args[i]);
}
return (ResultSet) pstmt.executeQuery();
}
public void modify(String sql, Object...args) throws Exception{
PreparedStatement pstmt = (PreparedStatement)getConnection().prepareStatement(sql);
for(int i = 0; i< args.length; i++) {
pstmt.setObject(i+1, args[i]);
}
pstmt.executeUpdate();
pstmt.close();
}
public boolean delete(String sql, Object... args) throws Exception{//删除
PreparedStatement pstmt = (PreparedStatement) getConnection().prepareStatement(sql);
for(int i = 0; i < args.length; i++) {
pstmt.setObject(i + 1, args[i]);
}
if(pstmt.executeUpdate() != 1)
return false;
return true;
}
public void closeConn() throws Exception{//关闭
if(conn != null && !conn.isClosed())
conn.close();
}
}
service包下
Delete.java(删除留言)
request.setCharacterEncoding("utf-8");
String errMsg = "";
RequestDispatcher rd;
//int id = Integer.parseInt("id");
String id = request.getParameter("id");
if(id.length()==0){
errMsg += "注册失败,请检查用户名和密码非空,并确定密码一致";
}else{
try{
DbDao dd = new DbDao("com.mysql.jdbc.Driver", "jdbc:mysql://localhost:3306/test?characterEncoding=utf8",
"root", "123456");
boolean deleduser = dd.delete("delete from mess where id=?", id);
if(!deleduser){
errMsg += "删除出现错误";
}
HttpSession session = request.getSession(true);
session.setAttribute("name",id );
//获取转发对象
rd = request.getRequestDispatcher("/Message.jsp");
// 转发请求
rd.forward(request, response);
}catch(Exception e){
e.printStackTrace();
}
}
// 如果出错,转发到重新注册
if(errMsg != null && !errMsg.equals("")){
rd = request.getRequestDispatcher("/Message.jsp");
request.setAttribute("err", errMsg);
rd.forward(request, response);
}
}
GetData.java(获取所有留言)
String drv = "com.mysql.jdbc.Driver"; //数据库驱动类
String url = "jdbc:mysql://localhost:3306/test?characterEncoding=utf8";
String usr="root";
String pwd="123456";
//String sql = "select password from user where id=2";
String sql = "select * from mess order by id";
Connection conn =null;
Statement stm = null;
ResultSet rs = null;
try{
Class.forName(drv);
conn =DriverManager.getConnection(url, usr,pwd);
stm = conn.createStatement();
rs = stm.executeQuery(sql);
request.setAttribute("r", rs);
request.getRequestDispatcher("MessageShow.jsp").forward(request, response);
}catch(Exception e){
System.out.print("at GetData");
System.out.print(e);
}finally{
try{
if(rs!=null){
rs.close();
}
if(stm!=null){
stm.close();
}
if(conn!=null){
conn.close();
}
}catch(Exception ex){
System.out.print(ex);
}
}
}
InsertMessage.java(插入留言)
request.setCharacterEncoding("utf-8");
String errMsg = "";
RequestDispatcher rd;
String title = request.getParameter("title");
String author = request.getParameter("author");
String content = request.getParameter("content");
if((title.length()==0)|| (author.length()==0)||(content.length()==0))
errMsg += "提交失败!请注意填写";
else {
try {
DbDao dd = new DbDao("com.mysql.jdbc.Driver", "jdbc:mysql://localhost:3306/test?characterEncoding=utf8",
"root", "123456");
ResultSet rs = dd.query("select author from mess where title= ?", title);
if(rs.next()) {
errMsg += "标题已存在,请重新设置";
}
{
boolean addUser = dd.insert("insert into mess(title, author,content) values(?, ?,?)" ,title,author,content);
if(!addUser) {
errMsg += "留言出错!";
}
// 注册成功,转发到welcome.jsp
HttpSession session = request.getSession(true);
//session.setAttribute("name", author);
//获取转发对象
rd = request.getRequestDispatcher("index.jsp");
// 转发请求
rd.forward(request, response);
}
}catch(Exception e) {
e.printStackTrace();
}
}
// 如果出错,转发到重新注册
if(errMsg != null && !errMsg.equals("")){
rd = request.getRequestDispatcher("Message.jsp");
request.setAttribute("err", errMsg);
rd.forward(request, response);
}
}
LoginServlet (登录)
request.setCharacterEncoding("utf-8");
String errMsg = "";
RequestDispatcher rd;
String username = request.getParameter("username");
String pass = request.getParameter("pass");
try {
DbDao dd = new DbDao("com.mysql.jdbc.Driver", "jdbc:mysql://localhost:3306/test?characterEncoding=utf8",
"root", "123456");
// 查询结果集
ResultSet rs = dd.query("select pass from user where username = ?", username);
if(rs.next()){
//用户名和密码匹配
if(rs.getString("pass").equals(pass)) {
//获取session对象
//dd.setUsername(rs.getString("username"));
HttpSession session = request.getSession(true);
//session.setAttribute("uname",dd.getUsername());
session.setAttribute("name", username);
session.setAttribute("pass", pass);
//获取转发对象
rd = request.getRequestDispatcher("/show.jsp");
// 转发请求
rd.forward(request, response);
} else {
errMsg += "您的用户名密码不匹配,请重新输入";
}
} else {
errMsg += "您的用户名不存在,请先注册";
}
}catch(Exception e) {
e.printStackTrace();
}
// 如果出错,转发到重新登陆
if(errMsg != null && !errMsg.equals("")){
rd = request.getRequestDispatcher("/login.jsp");
request.setAttribute("err", errMsg);
rd.forward(request, response);
RegisterServlet(注册)
request.setCharacterEncoding("utf-8");
String errMsg = "";
RequestDispatcher rd;
String username = request.getParameter("username");
String pass = request.getParameter("pass");
String conPass = request.getParameter("conPass");
if((username.length()==0)|| (pass.length()==0)||!pass.equals(conPass))
errMsg += "注册失败,请检查用户名和密码非空,并确定密码一致";
else {
try {
DbDao dd = new DbDao("com.mysql.jdbc.Driver", "jdbc:mysql://localhost:3306/test?characterEncoding=utf8",
"root", "123456");
ResultSet rs = dd.query("select pass from user where username = ?", username);
if(rs.next()) {
errMsg += "用户已经存在,请重新设置用户名";
}
else {
boolean addUser = dd.insert("insert into user(username, pass) values(?, ?)",username, pass);
if(!addUser) {
errMsg += "注册用户出现错误";
}
// 注册成功,转发到welcome.jsp
HttpSession session = request.getSession(true);
session.setAttribute("name", username);
//获取转发对象
rd = request.getRequestDispatcher("/index.jsp");
// 转发请求
rd.forward(request, response);
}
}catch(Exception e) {
e.printStackTrace();
}
}
// 如果出错,转发到重新注册
if(errMsg != null && !errMsg.equals("")){
rd = request.getRequestDispatcher("/register.jsp");
request.setAttribute("err", errMsg);
rd.forward(request, response);
UpdateServlet(更新)exit(退出导航)略