`
dreamer_0924
  • 浏览: 178294 次
  • 性别: Icon_minigender_1
  • 来自: 济南
社区版块
存档分类
最新评论

J2EE学习经验:JSP学习总结2(转载)

阅读更多

接着写个Servlet来加载log4j:

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");
          }
}
}

然后同时要在web.xml下配置:

<servlet>
         
<servlet-name>log4jInit</servlet-name>
         
<servlet-class>powerwind.servlet.Log4jInit</servlet-class>
              
<init-param>
                
<param-name>log4j</param-name>
                
<param-value>WEB-INF/classes/log4j.properties</param-value>
              
</init-param>
         
<load-on-startup>1</load-on-startup>
</servlet>

6、国际化

#test_zh_CN.properties
#login page
login.title
=登录页面
小型的应用中,我们并不常需要国际化。但是,如果网站要中文版和英文版的话,这个就不错啦。使用时很简单,把资源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

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics