Topic: 高手sos!!! struts中filter错误。新加了login.jsp和LogonForm.java

  Print this page

1.高手sos!!! struts中filter错误。新加了login.jsp和LogonForm.java Copy to clipboard
Posted by: wilson751
Posted on: 2004-12-26 20:01

高手sos!!!

------------------------------------------------
实现功能:
连接Oracle数据库,并实现国际化。
------------------------------------------------
出现的问题:

进入首页时,可以实现国际化,但当LogonAction.java调用MemberBean.java的connectDB()方法时,
系统报错:chain.doFilter() occurs Servlet error!
------------------------------------------------

文件个数:4个
------------------------------------------------

文件功能及之间的调用关系:

SetCharacterEncodingFilter.java
过滤器,实现struts的国际化

DBBean.java
连接数据库

MemberBean.java
实现了saveToDB()方法,为了将来能将数据存到库中。
其中调用了DBBean的getConnection()方法连接数据库。

LogonAction.java
将用户提交的数据处理后,进行转发。
调用了memberbean的saveToDB()方法。

------------------------------------------------

环境:
win2000
tomcat5.0
oracle8

下面是:4个.java文件,和struts-config.xml,web.xml

------------------------------------------------ SetCharacterEncodingFilter.java

package filters;

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.UnavailableException;

public class SetCharacterEncodingFilter implements Filter {

public void destroy(){}

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

request.setCharacterEncoding("UTF-8");
try{
chain.doFilter(request,response);
}catch(IOException ioex){
System.out.println("chain.doFilter() occurs I/O error!");
}catch(ServletException se){
System.out.println("chain.doFilter() occurs Servlet error!");
}
}

public void init(FilterConfig filterConfig) throws ServletException{}
}

---------------------------------- LogonAction.java

package logon;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpSession;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.Action;
import org.apache.struts.action.ActionMessage;
import org.apache.struts.action.ActionMessages;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.util.MessageResources;
//import logon.MemberBean;
import java.sql.*;
//import oracle.jdbc.driver.*;

public final class LogonAction extends Action{

public ActionForward execute(ActionMapping mapping,
ActionForm form,
HttpServletRequest request,
HttpServletResponse response) throws Exception{

//get infomation from logon\application.properties
MessageResources messages = getResources(request);
ActionMessages errors = new ActionMessages();

String userName =(String)((LogonForm)form).getUserName();
String password =(String)((LogonForm)form).getPassword();

//this file has no business logic validation
// business logic validation
String badUserName = "bad";
if(userName.equalsIgnoreCase(badUserName)){
errors.add("username",new ActionMessage("logon.dont.talk.to.bad",badUserName));
saveErrors(request,errors);
return(new ActionForward(mapping.getInput()));
}

MemberBean memberbean = new MemberBean();
memberbean.setUserName(userName);
memberbean.setPassword(password);

try{
memberbean.connectDB();
System.out.println("memberbean.connectDB() success!");
}catch(SQLException se){
System.out.println("memberbean.connectDB() occurs error!");
}

//request.setAttribute(Constants.MEMBER_KEY,memberbean);
request.removeAttribute(mapping.getAttribute());

// business logic validation is successful,then forward
return(mapping.findForward("logon"));
}
}

---------------------------------- DBBean.java

package logon;

import java.sql.*;
import oracle.jdbc.driver.*;

public class DBBean {

public Connection connect = null;
public PreparedStatement pstmt = null;
public ResultSet rs = null;
public String sql = "";

public DBBean() {}

public static Connection getConnection() throws SQLException{
Connection conn = null;
try {
DriverManager.registerDriver(new OracleDriver());
conn = DriverManager.getConnection("jdbc:oracle:thinAngry127.0.0.1:1521:oradb",
"testsystem", "testsystem");
System.out.println("conn is "+conn);
} catch (SQLException ex) {
ex.printStackTrace();
throw ex;
}
return conn;
}

public PreparedStatement getPrepareStatement() throws SQLException {
PreparedStatement pst = null;

if (connect==null) {
connect = getConnection();
}

pst = connect.prepareStatement(sql);

return pst;
}

public ResultSet execQuery() throws SQLException{
ResultSet rst = null;

rst = pstmt.executeQuery();

return rst;
}

public int execUpdate() throws SQLException {
int ret = 0;

ret = pstmt.executeUpdate();

return ret;
}

public void close() throws SQLException{
if (rs!=null) {
rs.close();
}
if (pstmt!=null) {
pstmt.close();
}
}

public void setSql(String pSql) {
sql = pSql;
}

public String getSql() {
return sql;
}

public void setAutoCommit(boolean pBoolean) throws SQLException{
connect.setAutoCommit(pBoolean);
}

public void commit() throws SQLException{
connect.commit();
}
}

---------------------------------- MemberBean.java

package logon;

import java.sql.*;

public class MemberBean{

private String userName;
private String password;
public Connection conn = null;

public MemberBean(){}

public void setUserName(String name){
this.userName = name;
}

public String getUserName(){
return (this.userName);
}

public void setPassword(String pwd){
this.password = pwd;
}

public String getPassword(){
return (this.password);
}

DBBean dbbean = new DBBean();

// connect to Oracle DB
public void connectDB() throws SQLException{
try{
conn = DBBean.getConnection();
}catch(SQLException se){
System.out.println("getConnection() occurs error!");
//dbbean.close();
//System.out.println("close() success!");
throw se;
}
}

}

---------------------------------- struts-config.xml

<?xml version="1.0" encoding="ISO-8859-1" ?>

<!DOCTYPE struts-config PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 1.1//EN"
"http://jakarta.apache.org/struts/dtds/struts-config_1_1.dtd">

<struts-config>

<!-- ======== Form Bean Definitions =================================== -->
<form-beans>
<form-bean name="LogonForm" type="logon.LogonForm"/>
</form-beans>

<!-- ========== Action Mapping Definitions ============================== -->
<action-mappings>
<action path = "/LogonCheck"
type = "logon.LogonAction"
name = "LogonForm"
scope = "request"
validate = "false"
input = "/logon.jsp"
>
<forward name="logon" path="/main.jsp" />
</action>
</action-mappings>

<!-- ========== Message Resources Definitions =========================== -->
<message-resources parameter="logon.application"/>

</struts-config>

---------------------------------- web.xml

<?xml version="1.0" encoding="UTF-8"?>

<!DOCTYPE web-app
PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.2//EN"
"http://java.sun.com/j2ee/dtds/web-app_2_2.dtd">

<web-app>
<display-name>test manage system</display-name>

<!-- international filter -->
<filter>
<filter-name>Set Character Encoding</filter-name>
<filter-class>filters.SetCharacterEncodingFilter</filter-class>
</filter>

<filter-mapping>
<filter-name>Set Character Encoding</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>

<servlet>
<servlet-name>action</servlet-name>
<servlet-class>org.apache.struts.action.ActionServlet</servlet-class>
<init-param>
<param-name>config</param-name>
<param-value>/WEB-INF/struts-config.xml</param-value>
</init-param>
<load-on-startup>2</load-on-startup>
</servlet>

<servlet-mapping>
<servlet-name>action</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping>

<welcome-file-list>
<welcome-file>logon.jsp</welcome-file>
</welcome-file-list>

<taglib>
<taglib-uri>/WEB-INF/struts-bean.tld</taglib-uri>
<taglib-location>/WEB-INF/struts-bean.tld</taglib-location>
</taglib>

<taglib>
<taglib-uri>/WEB-INF/struts-html.tld</taglib-uri>
<taglib-location>/WEB-INF/struts-html.tld</taglib-location>
</taglib>

<taglib>
<taglib-uri>/WEB-INF/struts-logic.tld</taglib-uri>
<taglib-location>/WEB-INF/struts-logic.tld</taglib-location>
</taglib>

<taglib>
<taglib-uri>/WEB-INF/struts-nested.tld</taglib-uri>
<taglib-location>/WEB-INF/struts-nested.tld</taglib-location>
</taglib>

<taglib>
<taglib-uri>/WEB-INF/struts-tiles.tld</taglib-uri>
<taglib-location>/WEB-INF/struts-tiles.tld</taglib-location>
</taglib>
</web-app>

---------------------------------- logon.jsp

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib uri="/WEB-INF/struts-bean.tld" prefix="bean" %>
<%@ taglib uri="/WEB-INF/struts-html.tld" prefix="html" %>
<%@ taglib uri="/WEB-INF/struts-logic.tld" prefix="logic" %>
<%@ taglib uri="/WEB-INF/struts-nested.tld" prefix="nested" %>
<%@ taglib uri="/WEB-INF/struts-tiles.tld" prefix="tiles" %>

<html:html locale="true">
<head>
<title><bean:message key="logon.jsp.title" /></title>
<link href="style.css" rel="stylesheet" type="text/css">
<script language="vbscript">
function form1_onsubmit()
if document.form1.Username.value="" then
alert "请填写您的用户名!"
form1_onsubmit=false
else
if document.form1.Pwd.value="" then
alert "请填写您的密码!"
form1_onsubmit=false
end if
end if
end function
</script>
</head>

<body>
<table width="300" border="0" align="center" cellpadding="1" cellspacing="0" >
<tr><td>
<font color="#ff0000"><html:errors/></font> 
</td></tr>
</table>
<p></p>

<table width="300" border="0" align="center" cellpadding="1" cellspacing="0"

background="images/bg.gif">
<tr>
<td height="210"><table wIDth="300" border="0" align="center" cellpadding="0" cellspacing="0"

bgcolor="#BED3BE">
<tr bgcolor="#E9F0F8" valign="top">
<td width="300" height="180" bgcolor="#BED3BE">

<!-- <form method="post" action="logincheck.jsp" name="form1"> -->
<html:form action="/LogonCheck.do">
<table wIDth="300" align="center" cellpadding="0" cellspacing="0" bgcolor="#BED3BE">
<tr>
<td height="15" class="d">  </td>
</tr>
<tr>
<td height="20" class="d">
<div align="center" class="big">
<font class="c"><bean:message key="logon.jsp.logonform.heading" /></font>
</div></td>
</tr>
</table>

<table width="100%" height="1" border="0" cellpadding="0" cellspacing="0"

background="images/bg.gif">
<tr><td></td></tr>
</table>

<table wIDth="300" border="0" align="center" cellpadding="0" cellspacing="0" bgcolor="#BED3BE">
<tr>
<td valign="top">
<table width="300" border="0" cellspacing="0" cellpadding="0" align="center">
<tr>
<td colspan="2" height="10"> </td>
</tr>
<tr>
<td wIDth="30%" height="35"> <div align="right">
<span class="a"><bean:message key="logon.jsp.logonform.username" /> </span>
</div></td>
<td height="35">
<div align="left">
<!-- <input name="UserName" type="text" size="17"> -->
<html:text property="userName" />
</div></td>
</tr>
<tr>
<td height="34"> <div align="right">
<span class="a"><bean:message key="logon.jsp.logonform.password" /> </span>
</div></td>
<td height="34">
<div align="left"><span class="a">
<!-- <input name="PassWord" type="password" size="17"> -->
<html:text property="password" />
<span class="a" > </span></span></div></td>
</tr>
<tr>
<td height="50" colspan="2" valign="bottom"><div align="center">
<!-- 写法1: <input type="submit" value="登录" name="submit"> -->
<!-- 写法2: <html:submit>登录</html:submit> -->
<html:submit property="submit">
<bean:message key="logon.jsp.logonform.submit" />
</html:submit>

<!-- 写法1: <input type="reset" value="重写" name="reset"> -->
<!-- 写法2: <html:submit>重写</html:submit> -->
<html:submit property="reset">
<bean:message key="logon.jsp.logonform.reset" />
</html:submit>
</div></td>
</tr>
</table></td>
</tr>
</table>
</html:form>
</td>
</tr>
</table></td>
</tr>
</table>
</body>
</html:html>

---------------------------------- LogonForm.java

package logon;

import javax.servlet.http.HttpServletRequest;
import org.apache.struts.action.ActionMessage;
import org.apache.struts.action.ActionErrors;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionMapping;

public final class LogonForm extends ActionForm{
private String userName;
private String password;

public void setUserName(String name){
this.userName = name;
}

public String getUserName(){
return (this.userName);
}

public void setPassword(String pwd){
this.password = pwd;
}

public String getPassword(){
return (this.password);
}

//##########################################################################
public void reset(ActionMapping mapping,HttpServletRequest request){
this.userName = null;
this.password = null;
}

}

2.Re:高手sos!!! struts中filter错误 [Re: wilson751] Copy to clipboard
Posted by: doway
Posted on: 2004-12-28 11:57

不完整吧?我 copy 以上代码,又加了个 logon.jsp 和 LogonForm.java,在WL8上通过了。原先的代码看上去说得过去,但现在JDBC部分的代码像是很差了。

另外我用jb2005做时,“saveErrors(request,errors);”这一句必须被注释掉,否则通不过的——这让我非常困惑,哪位了解的朋友请指教一下。Smile

3.Re:高手sos!!! struts中filter错误。新加了login.jsp和LogonForm.java [Re: wilson751] Copy to clipboard
Posted by: wilson751
Posted on: 2004-12-28 23:07

在原贴上又加了logon.jsp和LogonForm.java
JDBC部分的代码差在哪里呢,还望不吝赐教,多谢了

4.Re:高手sos!!! struts中filter错误。新加了login.jsp和LogonForm.java [Re: wilson751] Copy to clipboard
Posted by: doway
Posted on: 2004-12-29 08:43

这可是一言难尽了。首先推荐一本书 http://www.china-pub.com/computers/common/info.asp?id=14126 ,对于准备做J2EE开发的朋友来说绝对是值的。书中有关于数据访问的处理方法,你看过就明白我所说的了。

这里只说几个要点:
应该从容器获取数据源,使用数据源获取连接
不使用数据源的话也应该将数据库连接URL写入配置中
必须确保总是能关闭打开的连接
对JDBC的异常处理应该封装到基类或助手类中,需要使用模板方法或策略设计模式

祝愉快!Smile


   Powered by Jute Powerful Forum® Version Jute 1.5.6 Ent
Copyright © 2002-2021 Cjsdn Team. All Righits Reserved. 闽ICP备05005120号-1
客服电话 18559299278    客服信箱 714923@qq.com    客服QQ 714923