SQL Database Management Part 3

1.JSP Filter
  open Eclipes
a. Creating the Application List
1st part
1. WebContent/WEB-INF/web.xml
2. Java Resoures/src/filter/MyFilter.java

3. WebContent/data.jsp
4. WebContent/nodata.jsp

5. WebContent/login.jsp
6. WebContent/check.jsp
7. WebContent/logout.jsp

2nd 9art

8. admin/admin01.jsp
9.  admin/admin02.jsp
10. admin/admin03.jsp


b. Import required jar file
   servlet-api.jar

c. Flow chart
Set data.jsp as the filter
Part 1


Part 2



d. Start building project and file structure
1.Create a new project  t080801 -- Dynamic


2.Create 2 files,data.jspnodata.jsp


3.In the body of data.jsp, type DATA. In the body of nodata.jsp, type NO DATA
4.Create a file in WEBINF, web.xml


5. Import servlet-api.jar (because need javax.servlet.filter )



6.create a filter, then put it in the filter package named MyFilter
   src --> new --> class


                         The package here will automatically create a filter folder under src.

7.Create login.jsp

8.Create check.jsp

9.Create logout.jsp


e. Start writing code

1. web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app>
<filter>
<filter-name>AccessController</filter-name>
<filter-class>filter.MyFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>AccessController</filter-name>
<url-pattern>/data.jsp</url-pattern>
</filter-mapping>
</web-app>


2. MyFilter.java

package filter;
import java.io.IOException;
import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.*;

public class MyFilter implements Filter {
public void destroy() {

}

public void doFilter(ServletRequest arg0, ServletResponse arg1,
FilterChain arg2) throws IOException, ServletException {
/*
因為MyFilter.java 是 class ,所以不能直接用 HttpSession,
要先宣告 HttpServletRequest 以及 HttpServletResponse 來承接,再轉換成 HttpSeaaion。
*/
/*
Because MyFilter.java is a class , you can't use HttpSession directly.
It is necessary to declare HttpServletRequest and HttpServletResponse to inherit and then convert to HttpSeaaion.

*/
HttpServletRequest request = (HttpServletRequest) arg0;
HttpServletResponse response = (HttpServletResponse) arg1;
HttpSession session = request.getSession();
//判斷 login_ok 是否為真
if (session.getAttribute("login_ok") != null
&& session.getAttribute("login_ok").equals("1"))
{
/*
login_ok 為真,則帶入 FilterChain arg2,檢查是否有下一個,若沒有,則回到之前的頁面 (data.jsp)
*/
arg2.doFilter(arg0, arg1);
}
else
{
//login_ok 為否,則將頁面轉送到 login.jsp
//Determine whether login_ok is true
RequestDispatcher dispatcher =
request.getRequestDispatcher("login.jsp");
dispatcher.forward(request, response);
}
}

public void init(FilterConfig arg0) throws ServletException {
}
}

3. data.jsp

<%@ page language="java" contentType="text/html; charset=BIG5"
    pageEncoding="BIG5"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=BIG5">
<title>Insert title here</title>
</head>
//只顯示出 DATA 文字。
//Show only DATA text.
<body>
DATA
//做一個登出按鈕
//Make a logout button
<form method="post" action="logout.jsp"  name="form2">
<p><input type="submit" value="登出" /></P>
</form>
</body>
</html>


PS:但是在執行 data.jsp 時,會直接將網頁內容顯示為 login.jsp 的內容。
這是正確的,因為
1. data.jsp web.xml 要篩選的檔案,所以會再將之轉到 MyFilter.java 做檢查
2. MyFilter.java 檢查出來,尚未登入,所以就交給 login.jsp

PS: However, when data.jsp is executed, the content of the web page is directly displayed as the content of login.jsp.

This is correct because
1. data.jsp is the file to be filtered by web.xml so it will be transferred to MyFilter.java to check

2. MyFilter.java is checked out, not logged in, so I will give it to login.jsp



4. nodata.jsp
此檔案是用來作為與 data.jsp 區別用。
 data.jsp是要被篩選的; nodata.jsp是要不被篩選的。
所以在網址列直接輸入 nodata.jsp 就會直接顯示"NO DATA"訊息,而不必經由檢查機制。

This file is used as a difference from data.jsp.
 Data.jsp is to be filtered; nodata.jsp is not to be filtered.

So entering nodata.jsp directly into the URL column will display the "NO DATA" message directly without having to go through the checking mechanism.

<%@ page language="java" contentType="text/html; charset=BIG5"
    pageEncoding="BIG5"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=BIG5">
<title>Insert title here</title>
</head>
<body>
NO DATA
</body>
</html>


5. login.jsp

<%@ page language="java" contentType="text/html; charset=BIG5"
    pageEncoding="BIG5"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=BIG5">
<title>Insert title here</title>
</head>
<body>
<form method="post" action="check.jsp" name="form1">
<p>帳號:<input type="text" name="username" /></p>
<p>帳號:<input type="text" name="password" /></p>
<p><input type="submit" value="登入" /></p>
</form>
</body>
</html>


6. check.jsp

<%@ page language="java" contentType="text/html; charset=BIG5"
    pageEncoding="BIG5"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">

<%
   if (request.getParameter("username").equals("abc")
  && request.getParameter("password").equals("123"))
   {
//登入成功,則導到 data.jsp 
//If the login is successful, it will redirect to data.jsp
  session.setAttribute("login_ok", "1");
  response.sendRedirect("data.jsp");
   }
   else
   {
//若登入失敗,則導到登入畫面
//If the login fails, then redirect to the login page.
  session.setAttribute("login_ok", "0");
  response.sendRedirect("login.jsp");
   }
%>


7. logout.jsp

<%@ page language="java" contentType="text/html; charset=BIG5"
    pageEncoding="BIG5"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<%
/*
將掌管登入的session 之數值改為 0,就代表登出。因為已經不能再用此 session 瀏覽其他頁面,所以若要 瀏覽其他頁面,就會導到 login.jsp。
*/
/*
Changing the value of the session in charge of login to 0 means logging out. Because you can no longer use this session to browse other pages, you will be directed to login.jsp if you want to browse other pages.

*/
  session.setAttribute("login_ok", "0");
%>
<html>
<body>
<p>Logged out 已登出</P>

<form method="post" action="login.jsp"  name="form2">
<p><input type="submit" value="Click here to login 按此重新登入" /></P>
</form>

</body>
</html>


2.JSP Filter - Advance
a. Intercept all files 
     Previously, for the interception of a single file (data.jsp), it is currently necessary to intercept all files under a folder (admin).
1. Edit web.xml
<url-pattern>/admin/*</url-pattern>
2. Edit Myfilter.java
request.getRequestDispatcher("/login.jsp");  //It was ("login.jsp"),one slash different
3. Edit login.jsp
     <form method="post" action="/t080801/check.jsp" name="form1">   //It was ("check.jsp")
4. Edit check.jsp
      response.sendRedirect("/t080801/admin/admin01.jsp");   //It was ("data.jsp")

PS:Solve Eclipse cache issues
1. Click Refresh

2. Copy the URL column to another browser and try it out.
3. Clear Eclipse cache

4. Forced to archive, open the work administrator, end the program with Tomcat and javaw.exe.


b. Return to the original page

之前的 check.jsp   是導到   response.sendRedirect("/t080801/admin/admin01.jsp");
但是每個各自的網頁必須回到原本的網頁。所以

The previous check.jsp is guided to response.sendRedirect("/t080801/admin/admin01.jsp");
However, each respective web page must be returned to the original web page. and so


1. Edit MyFilter.java

//因為所有網頁都會進來 MyFilter. java 這裡,所以可以先記錄原本網頁的位址
//Because all the pages will come in MyFilter. java here, so you can first record the address of the original page.


HttpServletRequest request = (HttpServletRequest) arg0;
HttpServletResponse response = (HttpServletResponse) arg1;
HttpSession session = request.getSession();
//加下面一行
//Add the following line
session.setAttribute("myuri", request.getRequestURI());
if (session.getAttribute("login_ok") != null
&& session.getAttribute("login_ok").equals("1"))


2. Edit check.jsp

Change
      response.sendRedirect("/t080801/admin/admin01.jsp");  
to
//將網頁導到之前紀錄的 URI,必須要轉成字串。
//The web page is imported to the previously recorded URI, which must be converted to a string.
      response.sendRedirect(session.getAttribute("myuri").toString());


PS:What is differences between URL and URI
URL:http://localhost:8080/t080801/admin/admin01.jsp
URI:/t080801/admin/admin01.jsp


3.The final Bug
如果直接用 login.jsp 登入,則會出現錯誤。因為 login.jsp 不在 admin 資料夾內,不會被篩選,就不會有 session 來記錄之前的網頁 URI,因此check.jsp

If you log in directly with login.jsp, an error will occur. Because login.jsp is not in the admin folder and will not be filtered, there will be no session to record the previous page URI, so check.jsp

  response.sendRedirect(session.getAttribute("myuri").toString());

中的 session 是 Null,沒辦法轉成字串。所以上面那一行要再加上 if  判斷式

The session above is Null and there is no way to convert it to a string. So the above line should be added with the if judgment.


The Final code of check.jsp will be 

   if (request.getParameter("username").equals("abc")

  && request.getParameter("password").equals("123"))
   {
  session.setAttribute("login_ok", "1");
//將 myuri =null 導到 admin01.jsp。也可以是就是將來的 index.jsp
//Direct myuri =null to admin01.jsp. It can also be the future index.jsp
  if (session.getAttribute("myuri")== null)
  response.sendRedirect("/t080801/admin/admin01.jsp");
  else
    response.sendRedirect(session.getAttribute("myuri").toString());
   }
   else
   {
  session.setAttribute("login_ok", "0");
  response.sendRedirect("login.jsp");
   }

No comments: