標籤:串連資料庫 dispatch null dstat from 頁面 war users 範圍
為了方便修改和後續的封裝套路
首先使用者訪問的頁面從web.xml找到<welcome-file-list><welcome-file>index.html</welcome-file></welcome-file-list>歡迎頁面設定,這個可以設定有限訪問的頁面,這裡是index.html那麼接下來使用者可以通過index.html發出的親求如form表單提交的地址/manage/user/index映射到UserServlet去擷取資料<servlet><servlet-name>UserServlet</servlet-name><servlet-class>com.linyi.controller.UserServlet</servlet-class></servlet><servlet-mapping><servlet-name>UserServlet</servlet-name><url-pattern>/manage/user/index</url-pattern></servlet-mapping>然後UiserServlet繼承HttpServlt複寫doGet或doPost方法public class UserServlet extends HttpServlet {//IUserService iUserService = new UserServiceImpl();IUserService iUserService = BeanFactory.getInstance("userService",IUserService.class);@Overrideprotected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {List<User> users = iUserService.getUsers();req.setAttribute("users",users);this.getServletConfig().getServletContext().getRequestDispatcher("/users/index.jsp").forward(req,resp);}}這裡調用了UserServiceImpl擷取並轉寄到users/index.jsp頁面之後來到UserServiceImplIUserDao iUserDao = BeanFactory.getInstance("userDao",IUserDao.class);@Overridepublic List<User> getUsers() {return iUserDao.getAllUsers();}@Overridepublic void addUser(String name) throws UserException{int re = iUserDao.addUser(name);if(re!=0){throw new UserException("插入資料異常"+name);}}public void updateUser(int id,String name){iUserDao.updateUser(id,name);}@Overridepublic User selectUser(int id) throws UserException {//User user = iUserDao.selectUser(id);if(user == null){throw new UserException("未查到資料"+id);}else{return user;}}這裡調用了UserDaoImpl來擷取數值這個UserDao用來操作資料庫執行個體化出User這個封裝類,在依次拿到值後返回頁面public List<User> getAllUsers() {//串連資料庫List<User> users = new ArrayList<User>();//串連資料庫try {Class.forName("com.mysql.jdbc.Driver");Connection connection = DriverManager.getConnection("jdbc:mysql://localhost/student", "root", "root");String sql = "SELECT * FROM emp ";PreparedStatement prepareStatement = connection.prepareStatement(sql);ResultSet resultSet = prepareStatement.executeQuery(); while(resultSet.next()){int id = resultSet.getInt(1);String name = resultSet.getString(2);User user = new User(id,name);users.add(user);}} catch (Exception e) {e.printStackTrace();}return users;} @Overridepublic int addUser(String name) {try {Class.forName("com.mysql.jdbc.Driver");Connection connection = DriverManager.getConnection("jdbc:mysql://localhost/student", "root", "root");String sql = "INSERT INTO emp VALUES(‘"+name+"‘) ";PreparedStatement prepareStatement = connection.prepareStatement(sql);prepareStatement.execute();} catch (Exception e) {e.printStackTrace();return -1;}return 0;}public void updateUser(int id,String name) {try {Class.forName("com.mysql.jdbc.Driver");Connection connection = DriverManager.getConnection("jdbc:mysql://localhost/student", "root", "root");String sql = "UPDATE emp set name=‘"+name+"‘ where id="+id+"";PreparedStatement prepareStatement = connection.prepareStatement(sql);prepareStatement.execute();} catch (Exception e) {e.printStackTrace();}} @Overridepublic User selectUser(int id) {User user = null;try {Class.forName("com.mysql.jdbc.Driver");Connection connection = DriverManager.getConnection("jdbc:mysql://localhost/student", "root", "root");String sql = "SELECT* FROM emp WHERE `id`="+id;PreparedStatement prepareStatement = connection.prepareStatement(sql);ResultSet set = prepareStatement.executeQuery();while (set.next()){int id2 = set.getInt(1);String name = set.getString(2);user = new User(id2,name);}} catch (Exception e) {e.printStackTrace();}return user; requestScope.message 使用方式<html><head><title>Title</title></head><body>${requestScope.message}</body></html>注釋:requestScope是el運算式的隱含對象,和變數範圍差不多。 el運算式的隱含對象包括: pageScope,requestScope,sessionScope,applicationScope 例如:${message} el會依次到pageScope,requestScope,sessionScope,applicationScope中尋找,直到找到為止。 如果寫成${requestScope.message}的形式,將會縮小範圍只在requestScope中尋找message。表示變數的範圍,一共4種:pageScope:表示變數只能在本頁面使用。requestScope:表示變數能在本次請求中使用。sessionScope:表示變數能在本次會話中使用。applicationScope:表示變數能在整個應用程式中使用。 <link href="resources/plugins/bootstrap-3.3.0/css/bootstrap.min.css" rel="stylesheet"/>bottstrap.css包http://www.bootcss.com/一個整合了很多特效的工具<link href="resources/plugins/material-design-iconic-font-2.2.0/css/material-design-iconic-font.min.css" rel="stylesheet"/>用於表徵圖的包<link href="resources/plugins/waves-0.7.5/waves.min.css" rel="stylesheet"/>點擊按鈕或div可以顯示波浪效果的包<link href="resources/plugins/checkbix/css/checkbix.min.css" rel="stylesheet"/>選框工具特效的包 placeholder 用於設定文字框預設文字
required autofocus |
HTML5 <input> autocomplete、required、autofocus 屬性 input:focus |
1、 規定輸入欄位是否應該啟用自動完成功能 屬性值on off自動完成允許瀏覽器預測對欄位的輸入。當使用者在欄位開始鍵入時,瀏覽器基於之前鍵入過的值,應該顯示出在欄位中填寫的選項。注釋:autocomplete 屬性適用於 form,以及下面的 input 類型:text, search, url, telephone, email, password, datepickers, range 以及 color。2、required="required"規定必需在提交之前填寫輸入欄位。如果使用該屬性,則欄位是必填(或必選)的。注釋:required 屬性適用於以下 input類型:text, search, url, telephone, email, password, date pickers, number, checkbox, radio 以及 file。3、aotofocus 自動擷取焦點4、styleinput:focus{background-color:yellow;}/style當前input擷取焦點背景色變黃色 失去焦點變預設
java ee,包括js,html,jsp等等知識整合