標籤:
剛剛做完的新聞發布系統,在這裡終結一下!
在做新聞發布系統時用的軟體有:
(1)Myeclipse
(2)Mysql
1.首先現在myeclipse中建立了一個名字叫news的項目
我先做的第一個功能是登陸功能
登陸功能:
需要串連資料庫,進行用戶端與伺服器進行資訊互動。
在開始寫登陸功能時,也遇到了許多問題,如串連不上資料庫,串連上資料庫了卻又讀取不了資料庫中的資訊。
在開始寫登陸功能是在jsp頁面中寫的,後來才知道這樣寫本不好,雖然很簡單,但如果項目越做越大,你就會發現在jsp頁面
中寫代碼,會有許多的重複的代碼。所以可以把那些重複的代碼寫到java代碼中,在jsp頁面中調用就可以了。
其次是頁面的美化:
開始時登陸頁面:
後來是登陸頁面:
區別就是在login.jsp中添加了HTML+CSS樣式,假如你想讓你的頁面變得漂亮一些,炫一些,
可以學一下HTML+CSS,和js指令碼。或者在網上找些寫好的樣式借鑒一下也是可以的,嘻嘻。
具體代碼:
1.login.jsp
<span style="font-size:18px;"><%@ page language="java" import="java.util.*" pageEncoding="utf-8"%><%String path = request.getContextPath();String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";%><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"><html> <head> <base href="<%=basePath%>"> <title>登入頁面</title> <meta http-equiv="pragma" content="no-cache"><meta http-equiv="cache-control" content="no-cache"><meta http-equiv="expires" content="0"> <meta http-equiv="keywords" content="keyword1,keyword2,keyword3"><meta http-equiv="description" content="This is my page"><script type="text/javascript">function chkData(){if(document.frm.uname.value==""){alert("請輸入使用者名稱。");document.frm.uname.focus();return false;}else if(document.frm.upass.value==""){alert("請輸入密碼資訊。");document.frm.upass.focus();return false;}else{return true;}}</script> </head> <body background="images/tupian.jpg"> <center> <p ><font size="+6" face="華文行楷" color="#000000">------登陸功能------</font></p><form name="frm" method="post" action="doLogin.jsp"><table border="15" width="45%" height="200"><tr><td><font size="+3" face="華文行楷" >使用者名稱:</font></td><td><input type="text" name="uname"/></td></tr><tr><td><font size="+3" face="華文行楷" >密 碼:</font></td><td><input type="password" name="upass"/></td></tr><tr><td colspan="2" align="center"><input type="submit" value="登 錄" onclick="return chkData();"/><input type="reset" value="重 置"/><input type="button" value="注 冊" onclick="javascript:window.location.href='g_zhuce.jsp'"></td></tr></table></form></center> </body></html></span>
2.操作頁面
假如登陸成功跳轉到index.jsp頁面,失敗跳轉到error.jsp頁面
doLogin.jsp
<%@ page language="java" import="java.util.*" pageEncoding="gbk"%><%@page import="java.sql.*"%><%@page import="gxs.bzu.com.JDBC.*"%><%request.setCharacterEncoding("gbk");String uname=request.getParameter("uname");String upass=request.getParameter("upass"); Login login =new Login();boolean flag = login.get_UserAndPassword(uname, upass);out.print(flag); if(flag){ response.sendRedirect("index.jsp");}else response.sendRedirect("error.jsp");%>
3.java實現也資料庫連接的代碼
Login.java
import java.sql.*;import java.util.*;public class Login { public boolean get_UserAndPassword(String name, String pass) { Connection conn = null; PreparedStatement pstmt = null; ResultSet rs = null; boolean flag=false; try { // 1.註冊驅動,前提必須載入驅動包 Class.forName("com.mysql.jdbc.Driver"); } catch (ClassNotFoundException e) { System.out.println("未發現驅動"); } try { conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/news", "root", "123456"); String sql = "select * from userinfo where uname=? and upass=?"; pstmt = conn.prepareStatement(sql);//先行編譯SQL語句 pstmt.setString(1,name); pstmt.setString(2,pass); rs=pstmt.executeQuery(); if(rs.next()){ flag=true; } } catch (SQLException e) { e.printStackTrace(); } finally { try { if (rs != null) rs.close(); if (pstmt != null) pstmt.close(); if (conn != null) conn.close(); } catch (SQLException e) { e.printStackTrace(); } } return flag; }}
Web開發之新聞發布系統詳解