---
# System prepended metadata

title: JSP/Servlet learn 30 Day

---

# JSP/Servlet learn 30 Day

___
### Day 1
___
 1. Install Tomcat
 2. Install Eclipse IDE
 3. Create Servlet.java
 **(編輯 java package 跟 Class name 按到下一步要把自動產生 web.xml檔打勾**
 ![](https://i.imgur.com/4uLdzTX.png)
** @WebServlet("/LoginServlet") 是對應的URL**
 
 4.新增 Server Runtime
 
**project左鍵-> Bulid Path-> Configure Bulid Path-> 選Libraries-> Classpath 欄按 Add Library->Server Runtime-> 選剛下載的Tomcat-> Finish**

5. 設定web.xml(在WEB-INF 把web.xml設定方式加進來,調整成只要url有/,後面不管接什麼就直接呼叫Servlet)
  
          <servlet>
            <servlet-name>LoginServlet</servlet-name>
            <servlet-class>com.servlet.LoginServlet</servlet-class>
          </servlet>

          <servlet-mapping>
            <servlet-name>LoginServlet</servlet-name>
            <url-pattern>/</url-pattern>
          </servlet-mapping>

6. 新增 login.jsp and index2.jsp(index2是因為預設是index會直接讀取到所以改名子）

        <form action="LoginServlet" method="post">
        <div >
            <table>
            <tr>
            <td>
                <h1>User Name:</h1><input type="text" name="name"  />
            </td>
            </tr>
            <tr>
            <td>
                <h2>Password:</h2><input type="password" name="mypassword" >
                <input type="submit" value="submit">
            </td>
            </tr>
            </table>
        </div>
        </form>
___

# JSP 連接到 Mariadb

1.連接到DB的.jsp
```
<%@ page language="java" contentType="text/html; charset=UTF-8"  
pageEncoding="UTF-8"%>  
<%@ page import="java.sql.*" %>  
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dt⋯⋯">  
<html>  
<head>  
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">  
    <title>Select_page_Basic</title>  
    </head>  
    <body>  
        <%  
        Connection con = null;
        try {
        Class.forName("org.mariadb.jdbc.Driver");
        String url = "jdbc:mariadb://localhost:3306/emp2";//這邊/emp2 是我的內部資料庫名稱
        String user = "root";
        String password = "root";
        con = DriverManager.getConnection( url, user, password);
        out.println("DB LINK OK!!");
        
        }catch (ClassNotFoundException e){//這邊是我隨便加的可能會有錯(但目前沒報錯)
        	System.out.println("無法連接");
        }

        String s_Sql = "select * from emp2";
        
        Statement s = con.createStatement();
        
        ResultSet rs = s.executeQuery (s_Sql);

        out.println("<br>MariaDB 10<br>DB - NAME : emp2<br>Table - NAME : emp2<br>");

        ResultSetMetaData rsmd = rs.getMetaData();
        int numberOfColumns = rsmd.getColumnCount();
		%>
        <table border="1" bordercolor="blue">

        <tr> 
         <%for (int i = 1; i <= numberOfColumns; i++){%>
     	         <th><%=rsmd.getColumnName(i)%></th>
     	<%}%>      
        </tr>
        
        <% while (rs.next()) { %>
          <tr>
             <% for (int i = 1; i <= numberOfColumns; i++) { %> 
                <td><%=rs.getString(i)%></td>
             <% } %>
          </tr>
        <%}%>

      </table>
        <%
        out.print("JSP RUN！");
        %>
        <p>OwO ///</p>

    </body>  
</html>
```
     
     
