package powerwind.servlet;
import org.apache.log4j.Logger;
import org.apache.log4j.PropertyConfigurator;
import javax.servlet.*;
import javax.servlet.http.*;
public class Log4jInit extends HttpServlet {
public void init(ServletConfig config) throws ServletException {
super.init(config);
String prefix = getServletContext().getRealPath("/");
String file = getInitParameter("log4j");
System.out.println("init log4j...");
if (file != null){
PropertyConfigurator.configure(prefix+file);
}else
{
PropertyConfigurator.configure(prefix+"log4j.properties");
}
}
}
小型的应用中,我们并不常需要国际化。但是,如果网站要中文版和英文版的话,这个就不错啦。使用时很简单,把资源test_zh_CN.properties文件放到classes目录下,然后用JSTL的fmt标签调用。
<fmt:setLocale value="zh_CN" scope=”session” />
<fmt:setBundle basename="test" scope=”session” var=”hehe” />
<fmt:message key="login.title" bundle=”${hehe}” scope=”session” />
其中var和scope属性不是必需的。三者结合,就可以实现国际化了。
二、极限与安全
资源放在WEB-INF下是安全的,因为这个目录对于客户端是不存在的。权限控制并不是仅仅这样就可以了。如果只是简单地判断用户是否登录,可用一个过滤器检查Session对象即可。若需要级别控制的话,就在Session中保存级别信息,然后加以判断。
一般把权限的控制做成一个标签(tag)。如:
public int doEndTag() throws JspException {
HttpSession session = pageContext.getSession();
if ((session != null) && (session.getAttribute("user") != null)) {
String t = ((UserBean) session.getAttribute("user")).getType();
if (t == null || role == null) {
invalid();
return (SKIP_PAGE);
}
String[] roles = role.split(delimiter);
for (int i = 0; i < roles.length; i++) {
if (roles[i].equalsIgnoreCase(role))
return (EVAL_PAGE);
}
} else {
invalid();
return (SKIP_PAGE);
}
return (EVAL_PAGE);
}
三、上传与下载
上传的话,一般使用已有的组件,如commons-fileupload 或者欧莱礼的cos (可能会遇到中文编码的问题)。而下载,比较简单,就自己写了个Servlet。
public void handleRequest(HttpServletRequest request,
HttpServletResponse response) throws IOException, ServletException {
String name = request.getParameter("name");
String type = request.getParameter("type");
String dir = request.getParameter("dir");
if (name == null || name.length() < 2 || dir == null || dir.length() < 1 || type == null || type.length() < 1) {
throw new ServletException("Sorry,error occured");
}
char ch = dir.charAt(dir.length() - 1);
if (ch != '/' || ch != '\')
dir = dir + "/";
ServletOutputStream os = null;
BufferedInputStream bis = null;
try {
File file = new File(dir + name);
if (!file.exists() || file.length() >= Integer.MAX_VALUE) {
logger.error("Invalid file or file to large,file: " + name);
throw new ServletException(
"Invalid file or file to large,file: " + name);
}
response.setContentType("application/" + type);
response.addHeader("Content-Disposition", "attachment; filename="+ name);
response.setContentLength((int) file.length());
os = response.getOutputStream();
bis = new BufferedInputStream(new FileInputStream(file));
int size = -1;
while ((size = bis.read()) != -1)
os.write(size);
} catch (IOException ioe) {
throw new ServletException(ioe.getMessage());
} finally {
if (os != null)
os.close();
if (bis != null)
bis.close();
}
}
以上只是个示例程序,灵活与方便的做法应该是在Servlet初始化参数(<init-param>)设置下载文件所在目录,当然也可以在页面中设置参数。甚至可以做成一个下载标签,方便使用。
http://java.chinaitlab.com/ServletJsp/532923_2.html
相关推荐
j2ee实验二:学习使用Session和Cookie
j2ee实验三:学习使用JSP技术j2ee实验三:学习使用JSP技术
j2ee课程实验:学生管理系统
韩顺平 J2EE视频实战教程JSP详细目录: 韩顺平.j2ee视频实战教程jsp第1讲(上集) 韩顺平.j2ee视频实战教程jsp第1讲(下集) 韩顺平.j2ee视频实战教程jsp第2讲(上集) 韩顺平.j2ee视频实战教程jsp第2讲(中集) 韩顺平.j2ee...
J2EE学习计划 J2EE 学习计划 J2EE 学习计划J2EE 学习计划J2EE 学习计划
activation.jar javax.servlet.jar javax.servlet.jsp.jar jboss-j2ee.jar jboss-jaxrpc.jar jboss-jsr77.jar jboss-saaj.jar mail.jar namespace.jar xml-apis.jar
在飞实习学J2EE之路:12.9.23 Hibernate的基本流程(登录)
j2ee学习笔记j2ee学习笔记j2ee学习笔记j2ee学习笔记j2ee学习笔记
在飞实习学J2EE之路:12.9.4 Struts的基本流程
j2ee学习经验和流程,在这里我谈谈我在学习j2ee流程,并谈到在此过程中领会的经验和教训。以便后来者少走弯路。
NULL 博文链接:https://1601844782.iteye.com/blog/2271814
《搞定j2ee核心技术与企业应用:ajax,jsp,struts 2,spring,hibernate》是笔者在多年项目开发过程中的经验总结,它通过丰富的实例由浅入深、循序渐进地介绍了目前采用java进行web开发的各种框架的使用方法,从而...
J2EE学习经验和流程
在飞实习学J2EE之路:12.9.5 Struts用DBCP连接池(登陆,注册)
大连理工J2EE课程,学习资料+复习资料。包括提纲、题库、知识点等。 把手头上的都拖进去了,所以有点乱,请自行选择合适的取用。
工作5年J2EE经验文档总结, 文档包括69个J2EE相关文档 Extjs学习笔记 ...j2ee学习总结 JAVA经典总结 spring经验总结 struts2标签库总结 Spring_AOP_学习小结 自己总结的spring 等几十个总结大全
j2ee的个人简单总结,主要是针对基础的汇总。
j2ee学习路径 路线图 j2ee学习路径
jsp_DAO jsp_Servlet jsp_sql jsp_smartupload j2ee学习基本功
韩顺平.j2ee视频实战教程jsp