写在前面:
github链接:https://github.com/Eleanoren/-/tree/master/News
小组成员:吕登名、刘婧祺
界面随手写的没有仔细美化,见谅
class 位置:
1. 登录界面
附上代码:
1 <%@ page contentType="text/html;charset=UTF-8" language="java" %> 2 <html> 3 <head> 4 <title>登 录</title> 5 </head> 6 <body> 7 <form method="post" name="frmlogin" action="Servlet/Servlet"> 8 <h1>用户登录</h1> 9 <table border=1> 10 <tr> 11 <td>账号</td> 12 <td> 13 <input type="text" name="id" value="账号" size="20" maxlength="20" 14 onfocus="if (this.value=='账号') this.value='';"/> 15 </td> 16 </tr> 17 <tr> 18 <td>密码</td> 19 <td> 20 <input type="password" name="password" value="密码" size="20" maxlength="20" 21 onfocus="if (this.value=='密码') this.value='';"/> 22 23 </td> 24 25 </tr> 26 <tr> 27 <input type="submit" name="Submit" value="提交" onclick="return Login()"/> 28 <input type="reset" name="Reset" value="重置"/> 29 </tr> 30 </table> 31 </form> 32 <script> 33 function Login() { 34 var sUserName = document.frmlogin.id.value; 35 var sPassword = document.frmlogin.password.value; 36 37 if ((sUserName == "") || (sUserName == "账号")) { 38 alert("请输入用户名!"); 39 return false; 40 } 41 if ((sPassword == "") || (sPassword == "密码")) { 42 alert("请输入密码!"); 43 return false; 44 } 45 } 46 </script> 47 </body> 48 </html>
2. 新闻显示界面
登录成功后,显示新闻内容:
附上代码:
<%@ page contentType="text/html;charset=UTF-8" language="java" %> <html> <head> <title>新闻</title> </head> <body> <% String id = (String) session.getAttribute("id"); String password = (String) session.getAttribute("password"); String title1=(String) session.getAttribute("title1"); String title2=(String) session.getAttribute("title2"); String title3=(String) session.getAttribute("title3"); String title4=(String) session.getAttribute("title4"); %> <div align="center"> <%=id %> 欢迎您,登陆成功!<br/> <font color="blue">新闻</font> <table border=1> <tr> <td> 账号: </td> <td> <%=id %> </td> </tr> <tr> <td> 标题: </td> <td> <%=title1 %> </td> </tr> <tr> <td> 标题: </td> <td> <%=title2 %> </td> </tr> <tr> <td> 标题: </td> <td> <%=title3 %> </td> </tr> <tr> <td> 标题: </td> <td> <%=title4 %> </td> </tr> </table> <a href="index.jsp">返回</a> </div> </body> </html>
3. 用 Servlet 编写的的业务控制层实现对数据的操作
package Servlet; import sql.JdbcCRUDByPreparedStatement; import javax.servlet.ServletException; import javax.servlet.annotation.WebServlet; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpSession; import java.io.IOException; import java.util.List; import java.util.Map; @WebServlet(name = "Servlet") public class Servlet extends HttpServlet { private static final long serialVersionUID=1; public Servlet(){} protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setContentType("text/html;charset=UTF-8"); request.setCharacterEncoding("UTF-8"); String id=request.getParameter("id"); String password=request.getParameter("password"); String result; if ((id == "") || (id == null) || (id.length() > 20)) { try { result="请输入的用户名(不能超过20个字符)"; request.setAttribute("massage",result); response.sendRedirect("/index.jsp"); }catch (Exception e) { e.printStackTrace(); } } if ((password == "") || (password == null) || (password.length() > 20)) { try { result = "请输入密码(不能超过20个字符)!"; request.setAttribute("message", result); response.sendRedirect("/login.jsp"); } catch (Exception e) { e.printStackTrace(); } } String strsql = "select * from userInfo where id='" + id + "' and password= '" + password + "'"; JdbcCRUDByPreparedStatement ps = new JdbcCRUDByPreparedStatement(); List<Map<String, Object>> list = ps.find(strsql); HttpSession session = request.getSession(); session.setAttribute("username", id); String k1, m1; if (!list.isEmpty()) { for (Map<String, Object> m : list) { for (String k : m.keySet()) { k1 = k; m1 = String.valueOf(m.get(k)); session.setAttribute(k1, m1); } } response.sendRedirect("/NewsView.jsp"); } else { session.setAttribute("message", "用户名或密码不匹配!"); } } protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { } }
4. MySQL 新建数据库和数据表
create database news; use news; create table information( id varchar(20) not null primary key, password varchar(16) not null, title varchar(40) not null, source varchar(30) not null, content varchar(1000) not null, time varchar(20) not null )ENGINE=InnoDB DEFAULT CHARSET=utf8 AUTO_INCREMENT=1; insert into information values('tom','123','标题1','标题2','标题3','标题4');
5. .JDBC连接类
JdbcUtils类:
package sql; import java.sql.*; public class JDBCunit { private static final String driver="com.mysql.jdbc.Driver"; private static final String url="jdbc:mysql://localhost:3306"; private static final String id="root"; private static final String password="123456"; static { try { Class.forName(driver); }catch (Exception e){ throw new ExceptionInInitializerError(e); } } public static Connection getConnection() throws SQLException { return DriverManager.getConnection(url, id, password); } public static void release(Connection conn, Statement st, ResultSet rs) { if (rs != null) { try { rs.close(); } catch (Exception e) { e.printStackTrace(); } } if (st != null) { try { st.close(); } catch (Exception e) { e.printStackTrace(); } } if (conn != null) { try { conn.close(); } catch (Exception e) { e.printStackTrace(); } } } }
JdbcCRUDByPreparedStatement类:
package sql; import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.ResultSetMetaData; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; public class JdbcCRUDByPreparedStatement { private static Connection conn = null; private static PreparedStatement st = null; private static ResultSet rs = null; public List<Map<String, Object>> find(String strSql) { List<Map<String, Object>> list = new ArrayList(); try { conn = JDBCunit.getConnection(); st = conn.prepareStatement(strSql); rs = st.executeQuery(); ResultSetMetaData rsmd = rs.getMetaData(); while (rs.next()) { Map<String, Object> map = new HashMap(); int columnCount = rsmd.getColumnCount(); for (int i = 0; i < columnCount; i++) { String columnName = rsmd.getColumnName(i + 1); map.put(columnName, rs.getObject(i + 1)); } list.add(map); } } catch (Exception e) { e.printStackTrace(); } finally { JDBCunit.release(conn, st, rs); return list; } } }
6. web.xml配置
<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd" version="4.0"> <welcome-file-list> <welcome-file>index.jsp</welcome-file> </welcome-file-list> <servlet> <servlet-name>Servlet</servlet-name> <servlet-class>Servlet.Servlet</servlet-class> </servlet> <servlet-mapping> <servlet-name>Servlet</servlet-name> <url-pattern>/Servlet</url-pattern> </servlet-mapping> </web-app>