Last active
August 15, 2018 03:06
-
-
Save mrlin0518/de0977b7e591bb9c3ad0a4277d75a42d to your computer and use it in GitHub Desktop.
0809Web課堂練習
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package model; | |
import java.sql.Connection; | |
import java.sql.DriverManager; | |
import java.sql.ResultSet; | |
import java.sql.Statement; | |
import java.util.ArrayList; | |
import java.util.List; | |
import vo.User; | |
public class Login { | |
public List<User> queryUsers() throws Exception { | |
Class.forName("org.apache.derby.jdbc.ClientDriver");//註冊Derby資料庫 | |
Connection conn = DriverManager.getConnection("jdbc:derby://localhost:1527/sample",//Derby連線 | |
"app", | |
"app"); | |
Statement stmt = conn.createStatement();//取得statent | |
ResultSet rs = stmt.executeQuery("SELECT username , password FROM APP.USERS ");//Statement取得ResultSet | |
List<User> list = new ArrayList<>(); | |
while (rs.next()) { | |
String username = rs.getString("username"); | |
String password = rs.getString("password"); | |
User user = new User(username, password); | |
list.add(user); | |
} | |
return list; | |
} | |
public boolean check(User user) { | |
List<User> list = null; | |
try { | |
list = queryUsers(); | |
} catch (Exception e) { | |
} | |
for (User u : list) { | |
if (u.getName().equals(user.getName()) && u.getPassword().equals(user.getPassword())) { | |
return true; | |
} | |
} | |
return false; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!DOCTYPE html> | |
<!-- | |
To change this license header, choose License Headers in Project Properties. | |
To change this template file, choose Tools | Templates | |
and open the template in the editor. | |
--> | |
<html> | |
<head> | |
<title>MyLogin</title> | |
<meta charset="UTF-8"> | |
<meta name="viewport" content="width=device-width, initial-scale=1.0"> | |
<link rel="stylesheet" href="https://unpkg.com/purecss@1.0.0/build/pure-min.css"> | |
</head> | |
<body style="padding: 20px"> | |
<form class="pure-form" name="MyBMI" action="../servlet/MyLoginServlet" method="POST"><p /> | |
<fieldset><!-- placeholder提示使用者需輸入之內容 --> | |
<legend>Login</legend> | |
Username <input type="text" name="username" placeholder="Enter Account" /><p> | |
PassWord: <input type="password" name="password" placeholder="Enter Password" /><p> | |
<button type="submit" class="pure-button pure-button-primary">送出</button> | |
</fieldset> | |
</form> | |
</body> | |
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package servlet; | |
import java.io.IOException; | |
import java.io.PrintWriter; | |
import java.util.List; | |
import javax.servlet.RequestDispatcher; | |
import javax.servlet.ServletConfig; | |
import javax.servlet.ServletException; | |
import javax.servlet.annotation.WebInitParam; | |
import javax.servlet.annotation.WebServlet; | |
import javax.servlet.http.HttpServlet; | |
import javax.servlet.http.HttpServletRequest; | |
import javax.servlet.http.HttpServletResponse; | |
import model.Login; | |
import vo.User; | |
@WebServlet("/servlet/MyLoginServlet") | |
public class MyLoginServlet extends HttpServlet { | |
@Override | |
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { | |
req.setCharacterEncoding("UTF-8"); | |
resp.setCharacterEncoding("UTF-8"); | |
resp.setContentType("text/html;chartest=UTF-8"); | |
Login login = new Login(); | |
String username = req.getParameter("username"); | |
String password = req.getParameter("password"); | |
User user = new User(username, password); | |
boolean success = login.check(user); | |
//RequestDispatcher 練習 | |
if (success) { | |
List<User> list = null; | |
try { | |
list = login.queryUsers(); | |
} catch (Exception e) { | |
} | |
RequestDispatcher rd = getServletContext().getRequestDispatcher("/jsp/LoginSuccess.jsp");//分派至/jsp/ViewBMI.jsp | |
req.setAttribute("user", user); //設定Request的Attribute | |
req.setAttribute("list", list); //設定Request的Attribute | |
rd.forward(req, resp); //將值傳遞 | |
} else { | |
resp.sendRedirect("../form/MyLogin.html"); | |
} | |
/* | |
PrintWriter out = resp.getWriter(); | |
out.println("<html>"); | |
out.println("success: "+success); | |
out.println("</html>"); | |
*/ | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package vo; | |
public class User { | |
private String name; | |
private String password; | |
public User() { | |
} | |
public User(String name, String password) { | |
this.name = name; | |
this.password = password; | |
} | |
public String getName() { | |
return name; | |
} | |
public void setName(String name) { | |
this.name = name; | |
} | |
public String getPassword() { | |
return password; | |
} | |
public void setPassword(String password) { | |
this.password = password; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<%@page contentType="text/html" pageEncoding="UTF-8"%> | |
<!DOCTYPE html> | |
<html> | |
<head> | |
<title>MyBMI</title> | |
<meta charset="UTF-8"> | |
<meta name="viewport" content="width=device-width, initial-scale=1.0"> | |
<link rel="stylesheet" href="https://unpkg.com/purecss@1.0.0/build/pure-min.css"> | |
</head> | |
<body style="padding: 20px"> | |
<form><p /> | |
<fieldset> | |
<legend>BMI</legend> | |
BMI: <%=request.getAttribute("bmi") %> <p><!--使用jps接取JAVA的值--> | |
Sex: ${requestScope.sex}   ${requestScope.bmi}<p> | |
Comment: ${requestScope.result} | |
</fieldset> | |
</form> | |
<img src="../images/bmi.jpg" /> | |
</body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment