<body>
<form action="Servlet" method="post">
<table width="251" border="1">
<tr>
<td width="57">用户名:</td>
<td width="178"><label>
<input type="text" name="name" />
</label></td>
</tr>
<tr>
<td colspan="2"><input type="submit" value="提交"></td>
</tr>
</table>
</form>
</body>
二:Servlet页面Servlet.java
package com.servlet;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class Servlet extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doPost(request, response);
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
//取得内容
String name = request.getParameter("name");
//设置内容到下一页面
request.getSession().setAttribute("name", name);
//客户端跳转
response.sendRedirect("admin/ok.jsp");
//request.getRequestDispatcher("admin/ok.jsp").forward(request, response);
}
}
三:Filter过滤器实现统一中文编码FilterTest2.java
package com.filter;
import java.io.IOException;
import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
public class FilterTest2 implements Filter {
public void destroy() {
// TODO Auto-generated method stub
}
public void doFilter(ServletRequest arg0, ServletResponse arg1,
FilterChain arg2) throws IOException, ServletException {
System.out.println("**********统一中文过滤***********");
try{
arg0.setCharacterEncoding("utf-8");
arg1.setCharacterEncoding("utf-8");
}catch(Exception e){
e.printStackTrace();
}
arg2.doFilter(arg0, arg1);
}
public void init(FilterConfig arg0) throws ServletException {
// TODO Auto-generated method stub
}
}
四:Firlter过滤器实现登陆验证FirterTest.java
package com.filter;
import java.io.IOException;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
/**
* @author
*
*/
public class FilterTest implements javax.servlet.Filter {
public void destroy() {
// TODO Auto-generated method stub
System.out.println("**********销毁***********");
}
public void doFilter(ServletRequest arg0, ServletResponse arg1,
FilterChain arg2) throws IOException, ServletException {
System.out.println("**********过滤2***********");
//强制转换取得Request对象
HttpServletRequest req = (HttpServletRequest) arg0;
//强制转换取得Response对象
HttpServletResponse res = (HttpServletResponse)arg1;
if(req.getSession().getAttribute("name")!=null){
arg2.doFilter(arg0, arg1);
}else{
//客户端跳转
res.sendRedirect("../login.jsp");
//res.setHeader("refresh", "1;url=../login.jsp");
//arg0.getRequestDispatcher("../login.jsp").forward(arg0, arg1);
}
}
public void init(FilterConfig arg0) throws ServletException {
// TODO Auto-generated method stub
System.out.println("**********初始化***********");
}
}
五:Admin文件夹下的ok.jsp页面
<body>
再次欢迎${name }的到来~
</body>
六:web.xml的设置
<servlet-mapping>
<servlet-name>Servlet</servlet-name>
<url-pattern>/Servlet</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
<filter>
<filter-name>test2</filter-name>
<filter-class>com.filter.FilterTest2</filter-class>
</filter>
<filter-mapping>
<filter-name>test2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<filter>
<filter-name>test</filter-name>
<filter-class>com.filter.FilterTest</filter-class>
</filter>
<filter-mapping>
<filter-name>test</filter-name>
<url-pattern>/admin/*</url-pattern>
</filter-mapping>
//先过滤全部文件夹,再进行admin文件夹下的过滤~