JBuilder9+Weblogic8.1——Filter过滤器在Jbuilder9中的运用
生活随笔
收集整理的這篇文章主要介紹了
JBuilder9+Weblogic8.1——Filter过滤器在Jbuilder9中的运用
小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
JBuilder9+Weblogic8.1——Filter過濾器在Jbuilder9中的運(yùn)用 一、首先建立工程FilterTest,建立Web Application,名稱也為FilterTest。
然后創(chuàng)建 JSP頁(yè)面,這里用到三個(gè)JSP網(wǎng)頁(yè),分別為login.jsp、login_error.jsp和index.jsp。 1.login.jsp:
<%@ page contentType="text/html; charset=GBK" %>
<%@ page errorPage="login_error.jsp" %>
<html>
<head>
<title>
Filter的運(yùn)用
</title>
</head>
<body bgcolor="#c0c0c0">
<h1>
<center>
??? <p/>
??? <h1>歡迎使用Filter過濾器!</h1>
??? <form action="index.jsp" method="post">
??? <font size="4">
????? <b>用戶名:</b> <input type="text" name="username" value="">
????? <br><br>
????? <b>密 碼:</b> <input type="text" name="password" value="">
????? <br><br>
????? <input type="submit" name="login" value="登錄">  
????? <input type="reset" value="重寫">
??? </font>
??? </form>
</center>
</h1>
<form method="post">
<br><br>
<input type="submit" name="Submit" value="Submit">
<input type="reset" value="Reset">
</form>
</body>
</html>
2.login_error.jsp: <%@ page contentType="text/html; charset=GBK" %>
<%@ page isErrorPage="true" %>
<html>
<title>Filter的運(yùn)用</title>
<body bgcolor="#ffffff">
<center>
??? <h1>Filter錯(cuò)誤提示:</h1>
? </center>
? <%
??? String err = request.getParameter("errmsg");
??? String errmsg = new String(err.getBytes("ISO-8859-1"),"GBK");
? %>
? <p>對(duì)不起,您的操作有誤。請(qǐng)參考下列提示:</p>
? 您輸入的 <%=errmsg%>錯(cuò)誤!
</body>
</html>
3.index.jsp: <%@ page contentType="text/html; charset=GBK" %>
<html>
<head>
<title>
Filter的運(yùn)用
</title>
</head>
<body bgcolor="#c0c0c0">
<center>
??? <p/>
??? <h1>祝賀你順利通過Filter過濾!</h1>
? </center>
</body>
</html>
二、創(chuàng)建 Filter servlet 1.在菜單欄選擇File/New... ,彈出Object Gallery窗口; 2.點(diǎn)擊Web子頁(yè); 3.選擇Servlet然后點(diǎn)擊OK,出現(xiàn)Servlet窗口; 4.在Class后輸入FilterTest,選中Filter servlet,這個(gè)向?qū)?chuàng)建一個(gè)在filter包中名為FilterTest的class。點(diǎn)擊Finish完成創(chuàng)建。 在web.xml中你可以更改<filter-mapping/>中的<url-pattern/>內(nèi)的值來指定這個(gè)filter是對(duì)具體哪張網(wǎng)頁(yè)做過濾,默認(rèn)是/*,即對(duì)所有網(wǎng)頁(yè)都進(jìn)行過濾。注意,<filter>必須在整個(gè)web.xml中位置最前,否則不符合DTD語(yǔ)法要求。 5.下面是FilterTest.java的代碼:
package filtertest; import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
import java.util.*; public class FilterTest extends HttpServlet implements Filter {
? private FilterConfig filterConfig;
? //初始化用戶名和密碼
? public static final String UserName = "aaa";
? public static final String PassWord = "aaa";
? //Handle the passed-in FilterConfig
? public void init(FilterConfig filterConfig) {
??? this.filterConfig = filterConfig;
? }
? //Process the request/response pair
? public void doFilter(ServletRequest request, ServletResponse response, FilterChain filterChain) {
??? try {
????? filterChain.doFilter(request, response);
????? String username = ((HttpServletRequest)request).getParameter("username");
????? String password = ((HttpServletRequest)request).getParameter("password");
????? System.out.println("用戶 \"" + username + "\" 正在試圖登錄,他的密碼是 \"" + password + "\"。");
????? if (username == null && password == null)
????? {
????? System.out.println("頁(yè)面值傳遞錯(cuò)誤,或者非法進(jìn)入。");
????? }
????? else
????? {
??????? if (!UserName.equals(username))
??????? {
????????? ((HttpServletResponse) response).sendRedirect("login_error.jsp?errmsg=username");
????????? return;
??????? }
??????? else
??????? {
????????? if (!PassWord.equals(password))
????????? {
??????????? ( (HttpServletResponse) response).sendRedirect("login_error.jsp?errmsg=password");
????????????? return;
????????? }
??????? }
????? }
??? }
??? catch(ServletException sx) {
????? filterConfig.getServletContext().log(sx.getMessage());
??? }
??? catch(IOException iox) {
????? filterConfig.getServletContext().log(iox.getMessage());
??? }
? }
? //Clean up resources
? public void destroy() {
? }
}
6.部署應(yīng)用程序,啟動(dòng)運(yùn)行。
然后創(chuàng)建 JSP頁(yè)面,這里用到三個(gè)JSP網(wǎng)頁(yè),分別為login.jsp、login_error.jsp和index.jsp。 1.login.jsp:
<%@ page contentType="text/html; charset=GBK" %>
<%@ page errorPage="login_error.jsp" %>
<html>
<head>
<title>
Filter的運(yùn)用
</title>
</head>
<body bgcolor="#c0c0c0">
<h1>
<center>
??? <p/>
??? <h1>歡迎使用Filter過濾器!</h1>
??? <form action="index.jsp" method="post">
??? <font size="4">
????? <b>用戶名:</b> <input type="text" name="username" value="">
????? <br><br>
????? <b>密 碼:</b> <input type="text" name="password" value="">
????? <br><br>
????? <input type="submit" name="login" value="登錄">  
????? <input type="reset" value="重寫">
??? </font>
??? </form>
</center>
</h1>
<form method="post">
<br><br>
<input type="submit" name="Submit" value="Submit">
<input type="reset" value="Reset">
</form>
</body>
</html>
2.login_error.jsp: <%@ page contentType="text/html; charset=GBK" %>
<%@ page isErrorPage="true" %>
<html>
<title>Filter的運(yùn)用</title>
<body bgcolor="#ffffff">
<center>
??? <h1>Filter錯(cuò)誤提示:</h1>
? </center>
? <%
??? String err = request.getParameter("errmsg");
??? String errmsg = new String(err.getBytes("ISO-8859-1"),"GBK");
? %>
? <p>對(duì)不起,您的操作有誤。請(qǐng)參考下列提示:</p>
? 您輸入的 <%=errmsg%>錯(cuò)誤!
</body>
</html>
3.index.jsp: <%@ page contentType="text/html; charset=GBK" %>
<html>
<head>
<title>
Filter的運(yùn)用
</title>
</head>
<body bgcolor="#c0c0c0">
<center>
??? <p/>
??? <h1>祝賀你順利通過Filter過濾!</h1>
? </center>
</body>
</html>
二、創(chuàng)建 Filter servlet 1.在菜單欄選擇File/New... ,彈出Object Gallery窗口; 2.點(diǎn)擊Web子頁(yè); 3.選擇Servlet然后點(diǎn)擊OK,出現(xiàn)Servlet窗口; 4.在Class后輸入FilterTest,選中Filter servlet,這個(gè)向?qū)?chuàng)建一個(gè)在filter包中名為FilterTest的class。點(diǎn)擊Finish完成創(chuàng)建。 在web.xml中你可以更改<filter-mapping/>中的<url-pattern/>內(nèi)的值來指定這個(gè)filter是對(duì)具體哪張網(wǎng)頁(yè)做過濾,默認(rèn)是/*,即對(duì)所有網(wǎng)頁(yè)都進(jìn)行過濾。注意,<filter>必須在整個(gè)web.xml中位置最前,否則不符合DTD語(yǔ)法要求。 5.下面是FilterTest.java的代碼:
package filtertest; import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
import java.util.*; public class FilterTest extends HttpServlet implements Filter {
? private FilterConfig filterConfig;
? //初始化用戶名和密碼
? public static final String UserName = "aaa";
? public static final String PassWord = "aaa";
? //Handle the passed-in FilterConfig
? public void init(FilterConfig filterConfig) {
??? this.filterConfig = filterConfig;
? }
? //Process the request/response pair
? public void doFilter(ServletRequest request, ServletResponse response, FilterChain filterChain) {
??? try {
????? filterChain.doFilter(request, response);
????? String username = ((HttpServletRequest)request).getParameter("username");
????? String password = ((HttpServletRequest)request).getParameter("password");
????? System.out.println("用戶 \"" + username + "\" 正在試圖登錄,他的密碼是 \"" + password + "\"。");
????? if (username == null && password == null)
????? {
????? System.out.println("頁(yè)面值傳遞錯(cuò)誤,或者非法進(jìn)入。");
????? }
????? else
????? {
??????? if (!UserName.equals(username))
??????? {
????????? ((HttpServletResponse) response).sendRedirect("login_error.jsp?errmsg=username");
????????? return;
??????? }
??????? else
??????? {
????????? if (!PassWord.equals(password))
????????? {
??????????? ( (HttpServletResponse) response).sendRedirect("login_error.jsp?errmsg=password");
????????????? return;
????????? }
??????? }
????? }
??? }
??? catch(ServletException sx) {
????? filterConfig.getServletContext().log(sx.getMessage());
??? }
??? catch(IOException iox) {
????? filterConfig.getServletContext().log(iox.getMessage());
??? }
? }
? //Clean up resources
? public void destroy() {
? }
}
6.部署應(yīng)用程序,啟動(dòng)運(yùn)行。
總結(jié)
以上是生活随笔為你收集整理的JBuilder9+Weblogic8.1——Filter过滤器在Jbuilder9中的运用的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 关于登陆到域的用户,不需要显示登陆界面的
- 下一篇: 智能安全实验室-杀马(Defendio)