基于struts的登录验证:
1.新建web项目;
2.将struts的开发包加入web工程
3.编写login.jsp
4.编写ActionForm(用户表单)Action分控制器;
5.编写struts-config.xml文件,该文件用于配置action actionform的对应关系,和跳转位置,一般放在WEB-INF目录下
6.编写成功失败页面jsp;
7.配置web.xml文件,在该文件中配置struts-config.xml文件
8.测试ok
扩展
1.在登陆成功之后显示用户名;
2.使用filter配合struts解决中文乱码;
写一个过滤器,并设置request.setCharacterEncoding("utf-8");
public class MyFilter extends HttpServlet implements Filter {
@Override
public void doFilter(ServletRequest request, ServletResponse response,
FilterChain filter) throws IOException, ServletException {
// TODO Auto-generated method stub
request.setCharacterEncoding("utf-8");//设置编码
filter.doFilter(request, response);//必须
}
@Override
public void init(FilterConfig arg0) throws ServletException {
// TODO Auto-generated method stub
}
}
在web.xml文件中配置过滤器:
<!-- 配置filter -->
<filter>
<filter-name>MyFilter</filter-name>
<filter-class>filter.MyFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>MyFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
3.struts-config.xml
4.scope:
5.每个action都是单态的,包括ActionServlet;
单态:指在程序运行过程中始终只有一个实例对象;
6.struts-config.xml 不一定在WEB-INF目录下,也不一定只有一个只需在web.xml文件配置即可.
<init-param>
<param-name>config</param-name>
<param-value>/WEB-INF/struts-config.xml,/WEB-INF/struts-config-2.xml</param-value>
</init-param>
7.....
基于struts的用户注册验证:
1.导入struts标签库;
<%@ page language="java" contentType="text/html; charset=utf-8"
pageEncoding="utf-8"%>
<%@ taglib prefix="html" uri="http://struts.apache.org/tags-html" %>
<!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=utf-8">
<title>Insert title here</title>
</head>
<body>
<html:form action="/register.do" method="post" >
用户:<html:text property="username"></html:text><br/>
密码:<html:text property="password"></html:text><br/>
邮箱:<html:text property="email"></html:text><br/>
<html:submit value="注册"></html:submit>
<html:cancel value="取消"></html:cancel>
</html:form>
</body>
</html>
注意:这里的用户、密碼、郵箱必须在<html:form>中并且在form中有相应的字段与之对应.否则会报500错误.因为struts默认认为你的输入框等控件是要用来提交的,所以,必须写在form中且有相应的form类与之对应.而不是在你提交的时候才检测.
2.在web.xml中添加字符编码过滤器Filter,在配置文件中设置字符编码可以使程序更加灵活,也更利于维护;
<filter>
<filter-name>MyFilter</filter-name>
<filter-class>com.zky.struts.filter.MyFilter</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>utf-8</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>MyFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
public class MyFilter extends HttpServlet implements Filter {
private String encoding;
@Override
public void doFilter(ServletRequest request, ServletResponse response,
FilterChain chain) throws IOException, ServletException {
// TODO Auto-generated method stub
request.setCharacterEncoding(encoding);
chain.doFilter(request, response);
}
@Override
public void init(FilterConfig config) throws ServletException {
// TODO Auto-generated method stub
encoding = config.getInitParameter("encoding");
}
}
3.在web.xml中添加关键字过滤器SafeFilter,虽然在Action中也可以实现关键字过滤,但是在filter中实现更好;
<filter>
<filter-name>SafeFilter</filter-name>
<filter-class>com.zky.struts.filter.SafeFilter</filter-class>
<init-param>
<param-name>keywords</param-name>
<param-value>拉登;小明;XXX;西吧</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>SafeFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
java代码:
public class SafeFilter extends HttpServlet implements Filter {
private String[] keywords;
@Override
public void doFilter(ServletRequest request, ServletResponse response,
FilterChain chain) throws IOException, ServletException {
// TODO Auto-generated method stub
String username = (String) request.getAttribute("username");
if (username != null) {
for (String keyword : keywords) {
if (keyword.equals(username)) {
request.getRequestDispatcher("/WEN-INF/err.jsp").forward(request, response);
return;
}
}
}
chain.doFilter(request, response);
}
@Override
public void init(FilterConfig config) throws ServletException {
// TODO Auto-generated method stub
keywords = config.getInitParameter("keywords").split(";");
}
}
问题:1.在web.xml文件中编辑filter的配置信息是,不小心删除了ActionServlet的部分配置文件,导致404错误;
2.在获得用户输入的用户名是应使用request.getParameter("username");而不是request.getAttribute("username");
导致过滤器无法使用.
案例:文件的上传下载
* 文件上传需要解决的几个问题:
1.文件名重复时,后上传的文件会覆盖先上传文件的问题;
2.中文文件名显示乱码问题;