开发环境与技术:
Eclipse、TomCat, Servlet、JSP,Mysql
本信息发布平台提供了不同类型信息和新闻的发布功能,能够满足各种不同用户的需求,如企业信息、娱乐信息和校园之家等;系统将用户分为:普通用户,会员用户和系统管理员。普通用户能在本系统中进行信息浏览,阅读,信息搜索。每条信息的标题被做成一个链接,用户点击它们就能跳转页面进行信息阅读;信息阅读页面,每条信息的详细信息将被取出,包括内容、标题等;会员用户能对信息进行被评论,而普通会员不具备该功能。系统管理员可以进行信息分类管理、添加信息、修改信息、信息审核和删除信息等,同时系统管理员能完成用户管理如包括会员用户管理、添加用户、删除用户和更改账号。
1.后台的验证
String username = Filter.escapeHTMLTags(request.getParameter(“username”).trim());
String password = Filter.escapeHTMLTags(request.getParameter(“password”).trim());
String reg_type = Filter.escapeHTMLTags(request.getParameter(“reg_type”).trim());
String lastip = request.getRemoteAddr();
int flag = memberBean.memberLogin(username, password, reg_type);
if(flag == Constant.SUCCESS){//登陆成功
String info = memberBean.getLastTimeIP(username);
int flag2 = memberBean.upmemberLogin(username, lastip);
if(flag2 == Constant.SUCCESS){
session.setAttribute(“member”, username);
session.setAttribute(“type”, reg_type);
session.setAttribute(“info”, info);
request.getRequestDispatcher(“member/index.jsp”).forward(request, response);
}
else{
session.setAttribute(“member”, username);
session.setAttribute(“type”, reg_type);
session.setAttribute(“info”, info);
session.setAttribute(“message”, “登陆成功,登陆信息更新失败!”);
request.getRequestDispatcher(“member/index.jsp”).forward(request, response);
}
}
else if(flag == Constant.NAME_ERROR){//用户名错误
request.setAttribute(“reg_user”, username);
request.setAttribute(“message”, “该用户名不存在!”);
request.getRequestDispatcher(“login.jsp”).forward(request, response);
}
else if(flag == Constant.PASSWORD_ERROR){//密码错误
request.setAttribute(“reg_user”, username);
request.setAttribute(“message”, “密码错误!”);
request.getRequestDispatcher(“login.jsp”).forward(request, response);
}
2.查看留言页面的显示
具体代码如下:
request.setCharacterEncoding(Constant.CHARACTERENCODING);
response.setContentType(Constant.CONTENTTYPE);
//nikename, pic, email, qq, weburl, blogurl, expressions, content
String sysdir = new SystemBean().getDir();
HttpSession session = request.getSession();
String method = request.getParameter(“method”).trim();
GuestBookBean guestBookBean = new GuestBookBean();
if(method.equals(“add”)){//发表留言
String member=(String)session.getAttribute(“member”);
String nikename = Filter.escapeHTMLTags(request.getParameter(“nikename”).trim());
String face =“images/nobody.gif”;
String email = Filter.escapeHTMLTags(request.getParameter(“email”).trim());
String qq = Filter.escapeHTMLTags(request.getParameter(“qq”).trim());
String weburl = Filter.escapeHTMLTags(request.getParameter(“weburl”).trim());
String blogurl = Filter.escapeHTMLTags(request.getParameter(“blogurl”).trim());
String expressions= “images/face/”+Filter.escapeHTMLTags(request.getParameter(“face”).trim())+".gif";
String content = Filter.escapeHTMLTags(request.getParameter(“content”).trim());
String ip = request.getRemoteAddr();
if(nikename.equals("")){
nikename=“游客”;
}
if(member!=null){
String sex = guestBookBean.getSex(member);
if(sex.trim().equals(“m”))
face=“images/boy.gif”;
else if(sex.trim().equals(“f”))
face=“images/girl.gif”;
else
face=“images/nobody.gif”;
}
int guestbook=1;
int flag = guestBookBean.addGuestBook(nikename, face, email, qq, weburl, blogurl, expressions, content, ip,guestbook);
if(flag == 1){
request.setAttribute("message", "谢谢您的留言,请等候管理员回复!");
request.getRequestDispatcher("guestbook.jsp").forward(request, response);
}
else{
request.setAttribute("message", "系统维护中,请稍后再试!");
request.getRequestDispatcher("guestbook.jsp").forward(request, response); }
3.后台插入数据库的代码如下:
<%! public String transch(String str){ try{ byte[] lb=str.getBytes("iso-8859-1"); str=new String(lb,"gb2312"); return str;} catch(Exception e){return str;} } %> <% String huifu=transch(request.getParameter("huifu")); String isbn=transch(request.getParameter("isbn")); String timu=transch(request.getParameter("timu")); String liuyan=transch(request.getParameter("liuyan")); Connection con=null; Statement sql=null; ResultSet rs=null; try{Class.forName("sun.jdbc.odbc.JdbcOdbcDriver"); } catch(ClassNotFoundException event){} try {con=DriverManager.getConnection("jdbc:odbc:wzxw","",""); sql=con.createStatement(); String condition="INSERT INTO huifu VALUES('"+isbn+"','"+timu+"','"+liuyan+"','"+huifu+"')"; int i=sql.executeUpdate(condition); if(i>0){ %> <%}else{%> <%} con.close(); } catch(SQLException event) { } %>4.查看个人信息的代码如下:
<% Object obj = session.getAttribute("user"); String user = obj.toString(); Connection con; Statement sql; ResultSet rs; try{Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");} catch(ClassNotFoundException e){} try { con=DriverManager.getConnection("jdbc:odbc:wzxw","",""); sql=con.createStatement(); rs=sql.executeQuery("SELECT * FROM user where user='"+user+"'"); if(rs.next()){%>用户编号 |
}
catch(SQLException e) {
out.print(e);
}
%>
5.后台保存的代码如下:
<%! public String transch(String str){ try{ byte[] lb=str.getBytes("iso-8859-1"); str=new String(lb,"gb2312"); return str;} catch(Exception e){return str;} } %> <% String userid=request.getParameter("userid"); String user=transch(request.getParameter("user")); String password=transch(request.getParameter("password")); String sex=transch(request.getParameter("sex")); String age=transch(request.getParameter("age")); String adds=transch(request.getParameter("adds")); String email=transch(request.getParameter("email"));Connection con=null;
Statement sql=null;
ResultSet rs=null;
try{Class.forName(“sun.jdbc.odbc.JdbcOdbcDriver”); }
catch(ClassNotFoundException event){}
try {con=DriverManager.getConnection(“jdbc:odbc:wzxw”,"","");
sql=con.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_UPDATABLE);
String condition=“update user set user=’”+user+"’,password=’"+password+"’,age=’"+age+"’,adds=’"+adds+"’ ,email=’"+email+"’ where userid=’"+userid+"’ ";
int i=sql.executeUpdate(condition);
if(i>0){out.print(“修改成功 请返回” );
%>
<%}else{out.print(“修改失败 请返回” );%>
<%}
con.close();
}
catch(SQLException event) { }
%>
6.后台数据库中数据的删除
<% String isbn=request.getParameter("isbn"); Connection con; Statement sql; ResultSet rs; try{Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");} catch(ClassNotFoundException e){} try { con=DriverManager.getConnection("jdbc:odbc:wzxw","",""); sql=con.createStatement(); int i=sql.executeUpdate("delete FROM xinwen where isbn='"+isbn+"'"); con.close(); %> <%} catch(SQLException e1) {} %>6.后台对公告管理的代码
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType(Constant.CONTENTTYPE);
request.setCharacterEncoding(Constant.CHARACTERENCODING);
String sysdir = new SystemBean().getDir();
HttpSession session = request.getSession();
try{
String username2 = (String)session.getAttribute("user");
if(username2 == null){
request.getRequestDispatcher("error.jsp").forward(request, response);
}
else{
AfficheBean afficheBean = new AfficheBean();
String method = request.getParameter("method").trim();
if(method.equals("addAffiche")){//增加公告
String title = Filter.escapeHTMLTags(request.getParameter("title").trim());
String content = Filter.escapeHTMLTags(request.getParameter("content").trim());
String adder = username2;
String ifhide = Filter.escapeHTMLTags(request.getParameter("ifhide").trim());
int flag = afficheBean.addAffiche(title, content, adder, ifhide);
if(flag == Constant.SUCCESS){
request.setAttribute("message", "操作成功!");
request.getRequestDispatcher(sysdir+"/affiche/index.jsp").forward(request, response);
}
else{
request.setAttribute("message", "系统维护中请稍后再试!");
request.getRequestDispatcher(sysdir+"/affiche/index.jsp").forward(request, response);
}
}
else if(method.equals("editAffiche")){//修改公告
String id=Filter.escapeHTMLTags(request.getParameter("id").trim());
String title = Filter.escapeHTMLTags(request.getParameter("title").trim());
String content = Filter.escapeHTMLTags(request.getParameter("content").trim());
String adder = username2;
String ifhide = Filter.escapeHTMLTags(request.getParameter("ifhide").trim());
int flag = afficheBean.updateAffiche(Integer.parseInt(id), title, content, adder, ifhide);
if(flag == Constant.SUCCESS){
request.setAttribute("message", "操作成功!");
request.getRequestDispatcher(sysdir+"/affiche/index.jsp").forward(request, response);
}
else{
request.setAttribute("message", "系统维护中请稍后再试!");
request.getRequestDispatcher(sysdir+"/affiche/index.jsp").forward(request, response);
}
}
else if(method.equals("hideAffiche")){
String id = Filter.escapeHTMLTags(request.getParameter("id").trim());
int flag = afficheBean.hideAffiche(Integer.parseInt(id));
if(flag == Constant.SUCCESS){
request.getRequestDispatcher(sysdir+"/affiche/index.jsp").forward(request, response);
}
else{
request.setAttribute("message", "系统维护中,请稍后再试!");
request.getRequestDispatcher(sysdir+"/affiche/index.jsp").forward(request, response);
}
}
else if(method.equals("delaffiche")){//删除公告
String check[] = request.getParameterValues("checkit");
if(check == null){
request.setAttribute("message", "请选择要删除的记录!");
request.getRequestDispatcher(sysdir+"/affiche/index.jsp").forward(request, response);
}
else{
int id[]= new int[check.length];
for(int i = 0;i
欢迎大家留言讨论!