Posts Tagged ‘ j2ee

解决Jsp插入Mysql,设置UTF8依然乱码问题

问题描述:
JSP网页里是charset=UTF-8: 

<%@ page contentType= “text/html; charset=UTF-8 ” language= “java ” import= “java.sql.* ” errorPage= “error.jsp ” %> 

Mysql5里存取的数据库的charset也设成了UTF-8,可是通过JSP网页存取中文还是显示乱码 

解决方案:加个过滤类 

package com.bx.util; 

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 SetEncodingFilter implements Filter { 

    // ----------------------------------------------------- Instance  Variables 

    /**
     * The default character encoding to set for requests that pass through this
     * filter.
     */
    protected String encoding = null; 

    /**
     * The filter configuration object we are associated with. If this value is
     * null, this filter instance is not currently configured.
     */
    protected FilterConfig filterConfig = null; 

    /**
     * Should a character encoding specified by the client be ignored?
     */
    protected boolean ignore = true; 

    // --------------------------------------------------------- Public Methods 

    /**
     * Take this filter out of service.
     */
    public void destroy() {
        this.encoding = null;
        this.filterConfig = null;
    } 

    /**
     * Select and set (if specified) the character encoding to be used to
     * interpret request parameters for this request.
     *
     * @param request
     * The servlet request we are processing
     * @param result
     * The servlet response we are creating
     * @param chain
     * The filter chain we are processing
     *
     * @exception IOException
     * if an input/output error occurs
     * @exception ServletException
     * if a servlet error occurs
     */
    public void doFilter(ServletRequest request, ServletResponse response,
            FilterChain chain) throws IOException, ServletException {
        // Conditionally select and set the character encoding to be used
        if (ignore || (request.getCharacterEncoding() == null)) {
            String encoding = selectEncoding(request);
            if (encoding != null) {
                request.setCharacterEncoding(encoding);
            }
        } 

        // Pass control on to the next filter
        chain.doFilter(request, response);
    } 

    /**
     * Place this filter into service.
     *
     * @param filterConfig
     * The filter configuration object
     */
    public void init(FilterConfig filterConfig) throws ServletException { 

        this.filterConfig = filterConfig;
        this.encoding = filterConfig.getInitParameter("encoding");
        String value = filterConfig.getInitParameter("ignore");
        if (value == null) {
            this.ignore = true;
        } else if (value.equalsIgnoreCase("true")) {
            this.ignore = true;
        } else if (value.equalsIgnoreCase("yes")) {
           this.ignore = true;
        } else {
           this.ignore = false;
        }
    } 

    // ------------------------------------------------------ Protected Methods 

    /**
     * Select an appropriate character encoding to be used, based on the
     * characteristics of the current request and/or filter initialization
     * parameters. If no character encoding should be set, return
     * <code>null</code>.
     * <p>
     * The default implementation unconditionally returns the value configured
     * by the <strong>encoding</strong> initialization parameter for this
     * filter.
     *
     * @param request
     * The servlet request we are processing
     */
    protected String selectEncoding(ServletRequest request) {
        return (this.encoding);
    }
}

然后在web.xml文件中配置一下就可以了 

<filter>
    <filter-name>Set Character Encoding</filter-name>
    <filter-class>com.bx.util.SetEncodingFilter类的路径</filter-class>
    <init-param>
        <param-name>encoding</param-name>
        <param-value>UTF-8</param-value>
    </init-param>
</filter>
<filter-mapping>
    <filter-name>Set Character Encoding</filter-name>
    <url-pattern>/*</url-pattern>
</filter-mapping>

解决JDK1.6下SunJCE() is not accessible问题

一个老的项目从JDK1.5迁移到1.6时, Eclipse抛出一堆错误 主要是:

Access restriction: The constructor SunJCE() is not accessible due to restriction on required library /pp/develop/install/jdk1.6.0_21/jre/lib/ext/sunjce_provider.jar

访问限制: 由于/pp/develop/install/jdk1.6.0_21/jre/lib/ext/sunjce_provider.jar库的限制, SunJCE() 构造函数不可访问.

在网上搜了一下, 解决办法是修改编译选项, 方法如下: Window ->Preferences -> Java -> Compiler -> Errors/Warnings -> Deprecated and restricted API -> Forbidden reference (access rules) -> Warnings或者Ignore即可.

JSP解决session过期时写入数据库操作

背景如下:

我在做一个系统的时候希望实现当用户点击jsp页面上的注销按钮时实现在数据库中保存用户注销的时间. 另外如果用户没有正常退出, 则在session超时时自动记录超时时候的时间.

仿照找到的关于利用HttpSessionListener实现在线人数统计的方法来处理:

对每一个正在访问的用户, J2EE应用服务器会为其建立一个对应的HttpSession对象. 当一个浏览器第一次访问网站的时候, J2EE应用服务器会 新建一个HttpSession对象, 并触发HttpSession创建事件, 如果注册了HttpSessionListener事件监听器, 则会调用 HttpSessionListener事件监听器的sessionCreated方法. 相反,当这个浏览器访问结束超时的时候, J2EE应用服务器会销 毁相应的HttpSession对象, 触发HttpSession销毁事件, 同时调用所注册HttpSessionListener事件监听器的 sessionDestroyed方法.

可见, 对应于一个用户访问的开始和结束, 相应的有sessionCreated方法和sessionDestroyed方法执行. 因此, 我们只需在 HttpSessionListener实现类的sessionDestroyed方法中让其执行数据库的更新操作就可以了.

下面是示例代码:

package libms.session;

import javax.servlet.http.HttpSessionEvent;
import javax.servlet.http.HttpSessionListener;

import libms.service.UserServiceImpl;

public class UserOnlineListener
        implements HttpSessionListener {

    private static long userId = 0;

    public static void setUserId(long id) {
        userId = id;
    }

    public void sessionCreated(HttpSessionEvent event) {
    }

    public void sessionDestroyed(HttpSessionEvent event) {
        if (userId > 0) {
            // 这里写更新数据库的操作
        }
    }
}

在web.xml文件中注册一个监听器:

<listener>
    <listener-class>com.online.OnlineCountListener</listener-class>
</listener>

在用户登录的时候, 把用户的id使用UserOnlineListener .setUserId (id)的方法保存下来. 当用户点击注销按钮的时候, 调用session.invalidate ()的方法清空session, 就会触发监听器sessionDestroyed(HttpSessionEvent event)方法了, 同样, 如果用户非正常退出, 则在session超时的时候, 也会出发该方法.