學習筆記,每一天我們都在進步。
JSP Servlet
2007/03/22 15:07
Tags: ,
[codes=java]
/**
*
*/
package cn.org.jock.filter;

import javax.servlet.Filter;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.FilterChain;
import java.io.IOException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import javax.servlet.http.HttpServletResponse;

/**
* @author jock
*
*/
public class AuthorizationFilter implements Filter {

 /*
  * (non-Javadoc)
  *
  * @see javax.servlet.Filter#destroy()
  */
 public void destroy() {
   // TODO Auto-generated method stub

 }

 /*
  * (non-Javadoc)
  *
  * @see javax.servlet.Filter#doFilter(javax.servlet.ServletRequest,
  *      javax.servlet.ServletResponse, javax.servlet.FilterChain)
  */
 public void doFilter(ServletRequest request, ServletResponse response,
     FilterChain chain) throws IOException, ServletException {
   HttpServletRequest req = (HttpServletRequest) request;
   HttpServletResponse res = (HttpServletResponse) response;

   HttpSession session = req.getSession(true);

   String username = (String) session.getAttribute("username");

   if (username == null || "".equals(username)) {
     // 跳转到登陆页面
     res.sendRedirect("/login.jsp");
   } else {
     chain.doFilter(request, response);
   }

 }

 /*
  * (non-Javadoc)
  *
  * @see javax.servlet.Filter#init(javax.servlet.FilterConfig)
  */
 public void init(FilterConfig arg0) throws ServletException {
   // TODO Auto-generated method stub

 }

}

[/codes]
2007/03/22 12:05
Tags: ,
[codes=java]
package cn.org.jock.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;
import javax.servlet.http.HttpServletRequest;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

public final class LogFilter implements Filter {
 private FilterConfig filterConfig = null;

 private static final Log log = LogFactory.getLog(LogFilter.class);

 public void doFilter(ServletRequest request, ServletResponse response,
     FilterChain chain) throws IOException, ServletException {

   final long start = System.currentTimeMillis();
   final String address = request.getServerName();
   
   final String file = ((HttpServletRequest) request).getRequestURI();
   chain.doFilter(request, response);

   log.info("["+address + "_" + file + "]||[time="
       + (System.currentTimeMillis() - start)+"]");
 }

 public void destroy() {
 }

 public void init(FilterConfig filterConfig) {
   this.filterConfig = filterConfig;
 }
}

[/codes]


web.xml的配置
2007/02/03 18:28
Tags: , ,
文件上传在web应用中非常普遍,要在jsp环境中实现文件上传功能是非常容易的,因为网上有许多用java开发的文件上传组件,本文以commons-fileupload组件为例,为jsp应用添加文件上传功能。
common-fileupload组件是apache的一个开源项目之一,可以从http://jakarta.apache.org/commons/fileupload/下载。用该组件可实现一次上传一个或多个文件,并可限制文件大小。
下载后解压zip包,将commons-fileupload-1.0.jar复制到tomcat的webapps\你的webapp\WEB-INF\lib\下,目录不存在请自建目录。
新建一个servlet: Upload.java用于文件上传:
2007/01/12 11:51
Tags: ,
回到原点,重新认识
  一、page 对象

page对象代表JSP本身,更准确地说它代表JSP被转译后的Servlet,它可以调用Servlet类所定义的方法。

  二、config 对象

config 对象里存放着一些Servlet 初始的数据结构。
config 对象实现于javax.servlet.ServletConfig 接口,它共有下列四种方法:
public String getInitParameter(name)
public java.util.Enumeration getInitParameterNames( )
public ServletContext getServletContext( )
public Sring getServletName( )

  三、request 对象

request 对象包含所有请求的信息,如:请求的来源、标头、cookies和请求相关的参数值等等。
request 对象实现javax.servlet.http.HttpServletRequest接口的,所提供的方法可以将它分为四大类:
2007/01/11 22:06
Tags: ,
request:pageContext.getRequest();
session:pageContext.getSession();
application:pageContext.getServletContext();
response:pageContext.getResponse();

pageContext隱含物件對應於javax.servlet.jsp.PageContext型態之物件,隱含物件都自動的被加入至pageContext中,您可以藉由它來取得與JSP相關的隱含物件對應之Servlet物件,像是getRequest()可以取得ServletRequest,getServletConfig()可以取得ServletConfig,getSession()可以取得HttpSession等等。提供取得隱含物件對應之Servlet物件並不是pageContext的主要作用,它的主要功能在提供一個單一個介面,以管理各種的公開物件(像是HttpSession、ServletContext、ServletConfig、ServletRequest、ServletResponse等等),提供一個單一的API來管理屬性作用範圍等等。

我們之前曾經使用過session的setAttribute()等方法設定一個進程可以共用的屬性物件,使用session設定的屬性在同一個進程中可以共用,除了session之外,還有pageContext、request、application也都可以使用setAttribute()之類的方法(詳請查閱API文件)來設定可以共用的屬性物件,只不過這四個物件所設定的屬性其共用範圍各不相同。
分页: 1/2 第一页 1 2 下页 最后页 [ 显示模式: 摘要 | 列表 ]