登录界面
login_form.jsp
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>My JSP 'login_form.jsp' starting page</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
</head>
<body>
<pre>
<form action="login_accept" method="post">
用户名:<input type="text" name="name"/><br/>
身 份:<input type="text" name="identity"/><br/>
<input type="submit" value="提交"/><br/>
</form>
</pre>
${errorip }
</body>
</html>
Servlet 处理程序
LoginAcceptServlet.javapackage 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 LoginAcceptServlet extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
String name=request.getParameter("name");
String identity=request.getParameter("identity");
PrintWriter out=response.getWriter();
System.out.println(name+" "+identity);
out.println(name+"<br/>");
out.println(identity);
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doGet(request,response);
}
}Filter 过滤程序
FilterTest1.java 过滤字符集
package 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 FilterTest1 implements Filter {
private String encoding=null;
public void destroy() {
// TODO Auto-generated method stub
}
public void doFilter(ServletRequest request, ServletResponse response,
FilterChain chain) throws IOException, ServletException {
request.setCharacterEncoding(encoding);
response.setCharacterEncoding(encoding);
response.setContentType("text/html;charset=utf-8");
chain.doFilter(request, response);
}
public void init(FilterConfig config) throws ServletException {
encoding=config.getInitParameter("characterencoding");
}
}FilterTest2.java 过滤非法ip
package 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() {}
public void doFilter(ServletRequest request, ServletResponse response,
FilterChain chain) throws IOException, ServletException {
String ip=request.getRemoteAddr();
System.out.println(ip+" ");
if(ip.startsWith("127"))
{
System.out.println(ip+" ");
chain.doFilter(request, response);
}
else
{
System.out.println("防止非法ip登陆");
request.setAttribute("errorip", "地址不正确,不能登录");
request.getRequestDispatcher("login_form.jsp").forward(request, response);
}
}
public void init(FilterConfig arg0) throws ServletException {}
}
配置文件 web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5"
xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
<filter>
<filter-name>encodingFilter</filter-name>
<filter-class>filter.FilterTest1</filter-class>
<init-param>
<param-name>characterencoding</param-name>
<param-value>utf-8</param-value>
</init-param>
</filter>
<filter>
<filter-name>ipFilter</filter-name>
<filter-class>filter.FilterTest2</filter-class>
</filter>
<filter-mapping>
<filter-name>encodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<filter-mapping>
<filter-name>ipFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<servlet>
<description>This is the description of my J2EE component</description>
<display-name>This is the display name of my J2EE component</display-name>
<servlet-name>LoginAcceptServlet</servlet-name>
<servlet-class>servlet.LoginAcceptServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>LoginAcceptServlet</servlet-name>
<url-pattern>/login_accept</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>
本文详细解析了登录界面的实现原理,包括JSP页面、Servlet处理程序和Filter过滤程序的作用,以及它们如何共同完成登录验证的过程。文章还介绍了如何通过配置文件web.xml设置过滤器来确保字符集的一致性和过滤非法IP地址。
1513

被折叠的 条评论
为什么被折叠?



