LinuxSir.cn,穿越时空的Linuxsir!

 找回密码
 注册
搜索
热搜: shell linux mysql
查看: 692|回复: 5

能否帮我找找错误,我实在是没发现,谢谢

[复制链接]
发表于 2005-1-7 10:29:32 | 显示全部楼层 |阅读模式
class Constants
[PHP]
/*
* Created on 2005-1-5
*
* TODO To change the template for this generated file go to
* Window - Preferences - Java - Code Style - Code Templates
*/
package com.jxd.cart;

/**
* @author Administrator
*
* TODO To change the template for this generated type comment go to
* Window - Preferences - Java - Code Style - Code Templates
*/

public class Constants
{
        public static final String CART_KEY = "cartuser";
}

[/PHP]

2 class CartLoginSession
[PHP]
/*
* Created on 2005-1-5
*
* TODO To change the template for this generated file go to
* Window - Preferences - Java - Code Style - Code Templates
*/
package com.jxd.cart;

/**
* @author Administrator
*
* TODO To change the template for this generated type comment go to
* Window - Preferences - Java - Code Style - Code Templates
*/

public class CartLoginSession
{
        private String userName = null;
       
        public String getUserName()
        {
                return userName;
        }
       
        public void setUserName(String userName)
        {
                this.userName = userName;
        }
       
        public void saveToPersistentStore()
        {
               
        }
}

[/PHP]

3 class CartLoginForm
[PHP]
/*
* Created on 2005-1-5
*
* TODO To change the template for this generated file go to
* Window - Preferences - Java - Code Style - Code Templates
*/
package com.jxd.cart;

/**
* @author Administrator
*
* TODO To change the template for this generated type comment go to
* Window - Preferences - Java - Code Style - Code Templates
*/
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 class CartLoginForm extends ActionForm
{
        private String userName = null;
        private String userPasswd = null;
       
        public String getUserName()
        {
                return userName;
        }
       
        public void setUserName(String userName)
        {
                this.userName = userName;
        }
       
        public String getUserPasswd()
        {
                return userPasswd;
        }
       
        public void setUserPasswd(String userPasswd)
        {
                this.userPasswd = userPasswd;
        }
       
        public void reset(ActionMapping mapping,HttpServletRequest request)
        {
                this.userName = null;
                this.userPasswd = null;
        }
       
        public ActionErrors validate(ActionMapping mapping,HttpServletRequest request)
        {
                ActionErrors errors = new ActionErrors();
                if((userName == null)||(userName.length()<1))
                {
                        errors.add("username",new ActionMessage("cart.login.error"));
                }
                else if((userPasswd == null)||(userPasswd.length()<1))
                {
                        errors.add("userpasswd",new ActionMessage("cart.login.error"));
                }
                else
                {
                        errors.add("nothing",new ActionMessage("cart.login.error"));
                }
                return errors;
        }
}

[/PHP]

4 class CartLoginBean
[PHP]
/*
* Created on 2005-1-5
*
* TODO To change the template for this generated file go to
* Window - Preferences - Java - Code Style - Code Templates
*/
package com.jxd.cart;

/**
* @author Administrator
*
* TODO To change the template for this generated type comment go to
* Window - Preferences - Java - Code Style - Code Templates
*/
import java.sql.*;

public class CartLoginBean
{
        private String userName = null;
        private String userPasswd = null;
       
        public CartLoginBean(String userName,String userPasswd)
        {
                this.userName = userName;
                this.userPasswd = userPasswd;
        }
       
        public boolean check()
        {
                 boolean f = false;
                 Connection con = null;
                 ResultSet rs = null;
                 try
                 {
                         Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
                         con = DriverManager.getConnection("jdbcdbc:cart");
                         PreparedStatement pstmt = con.prepareStatement("SELECT userid,userpasswd FROM regist WHERE userid=? AND userpasswd=?");
                         pstmt.setString(1,userName);
                        pstmt.setString(2,userPasswd);
                        rs = pstmt.executeQuery();
                        if(rs.next())
                        {
                                f = true;
                        }
                        else
                        {
                                f = false;
                        }
                 }
                 catch(Exception err)
                 {
                         err.printStackTrace();
                 }
                 finally
                 {
                         if(rs != null)
                         {
                                 try
                                {
                                         rs.close();
                                }
                                 catch(Exception err)
                                {
                                         err.printStackTrace();
                                }
                         }
                         if(con != null)
                         {
                                 try
                                {
                                         con.close();
                                }
                                 catch(Exception err)
                                {
                                         err.printStackTrace();
                                }
                         }
                 }
                 return f;
        }
}

[/PHP]

5 class CartLoginAction
[PHP]
/*
* Created on 2005-1-5
*
* TODO To change the template for this generated file go to
* Window - Preferences - Java - Code Style - Code Templates
*/
package com.jxd.cart;

/**
* @author Administrator
*
* TODO To change the template for this generated type comment go to
* Window - Preferences - Java - Code Style - Code Templates
*/
//import javax.servlet.RequestDispatcher;
//import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
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.ActionMapping;
import org.apache.struts.action.ActionForward;
//import org.apache.struts.util.MessageResources;

public class CartLoginAction extends Action  
{
        public ActionForward execute(ActionMapping mapping,ActionForm form,HttpServletRequest request,HttpServletResponse response) throws Exception
        {
                boolean flag;
                ActionMessages errors = new ActionMessages();
                String userName = (String)((CartLoginForm)form).getUserName();
                String userPasswd = (String)((CartLoginForm)form).getUserPasswd();
                if((userName == null)||(userPasswd == null))
                {
                        errors.add("username",new ActionMessage("cart.login.error",userName));
                        saveErrors(request,errors);
                        return (new ActionForward(mapping.getInput()));
                }
                else
                {
                       
                       
                        CartLoginBean cartLogin = new CartLoginBean(userName,userPasswd);
                        flag = cartLogin.check();
                }
                if(flag)
                {
                        //System.out.println("ssss");
                        request.removeAttribute(mapping.getAttribute());
                        CartLoginSession cartSession = new CartLoginSession();
                        cartSession.setUserName(userName);
                        cartSession.saveToPersistentStore();
                        HttpSession session = request.getSession();
                        session.setAttribute(Constants.CART_KEY,cartSession);
                        return (mapping.findForward("login"));
                }
                else
                {
                        //System.out.println("ffffff");
                        return (mapping.findForward("index"));
                }
        }
}

[/PHP]

6 struts-config.xml
[PHP]
<?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-beans>
                <form-bean name="userForm" type="strutsuser.UserForm"/>
                <form-bean name="LoginForm" type="com.jxd.struts.login.LoginForm"/>
                <form-bean name="CartLoginForm" type="com.jxd.cart.CartLoginForm"/>
        </form-beans>
        <global-forwards>
                <forward name="userCreated" path="/jxd/viewUser.jsp"/>
                <forward name="regist" path="/cart/register.jsp"/>
        </global-forwards>
        <action-mappings>
                <action path="/createUser" type="strutsuser.UserAction" name="userForm" scope="request" validate="true" input="/jxd/createUser.jsp">
                </action>
                <action path="/Login" type="com.jxd.struts.login.LoginAction" name="LoginForm" scope="request" validate="true" input="/jxd/login.jsp">
                <forward name="Success" path="/jxd/success.jsp"/>
                <forward name="Fail" path="/jxd/fail.jsp"/>
                </action>

                <action path="/CartShopping" type="com.jxd.cart.CartLoginAction" name="CartLoginForm" validate="true" scope="request" input="/cart/index.jsp">
                <forward name="login" path="/cart/login.jsp"/>
                <forward name="index" path="/cart/index.jsp"/>
                </action>
        </action-mappings>

</struts-config>
[/PHP]

7 index.jsp
[PHP]
<%@ page contentType="text/html;charset=gb2312" language="java"%>
<%@ taglib uri="/struts-bean" prefix="bean"%>
<%@ taglib uri="/struts-html" prefix="html"%>
<%@ taglib uri="/struts-logic" prefix="logic"%>
<html:html locale="true">
<head>
<title>电子商店</title>
<html:base/>
</head>
<body>
<center>
欢迎光临我的购物商店,请随便挑选
<html:form action="/CartShopping.do" focus="userName">
用户名:<html:text property="userName" size="18" maxlength="18"/><br>
密  码:<html:password property="userPasswd" size="18" maxlength="18"/><br>
<html:submit property="submit" value="提交"/> &nbsp;&nbsp
<html:reset value="重写"/>
</html:form>
&nbsp;&nbsp
没有
<html:link forward="regist">
注册吗?
</html:link>
</center>
</body>
</html:html>
[/PHP]

8 login.jsp
[PHP]
<%@ page contentType="text/html;charset=gb2312" language="java"%>
<%@ taglib uri="/struts-bean" prefix="bean"%>
<%@ taglib uri="/struts-html" prefix="html"%>
<%@ taglib uri="/struts-logic" prefix="logic"%>
<html:html locale="true">
<head>
<title>电子商店</title>
<html:base/>
</head>
<body>
welcome
</body>
</html:html>
[/PHP]

9 register.jsp
[PHP]
<%@ page contentType="text/html;charset=gb2312" language="java"%>
<%@ taglib uri="/struts-bean" prefix="bean"%>
<%@ taglib uri="/struts-html" prefix="html"%>
<%@ taglib uri="/struts-logic" prefix="logic"%>
<html:html locale="true">
<head>
<title>电子商店</title>
<html:base/>
</head>
<body>
<center>
请您注册
<html:form action="/CartRegist.do" focus="userName">
用户名:<html:text property="userName" size="18" maxlength="18"/>***<br>
密  码:<html:password property="userPasswd" size="18" maxlength="18"/>***<br>
用户呢称:<html:text property="userAlias" size="10" maxlength="10"/>***<br>
用户性别:<html:text property="userSex" size="2" maxlength="2"/>***<br>
<html:submit property="submit" value="提交"/>
&nbsp;&nbsp
<html:reset value="重写"/>
</html:form>
</center>
</body>
</html:html>
[/PHP]
 楼主| 发表于 2005-1-7 10:33:08 | 显示全部楼层
! 现在发代码不是加[code][code]吗!!!!
发表于 2005-1-7 21:10:16 | 显示全部楼层
没发现错误,就当它是正确的...
呵呵
发表于 2005-1-8 00:19:26 | 显示全部楼层
运行有什么问题
 楼主| 发表于 2005-1-8 08:48:19 | 显示全部楼层

re

倒塌了………… 上午的时候无论查询数据库正确与否都返回错误页面,但是到下午什么都没改,测试的时候却又正常了…… 目前我也解释不清楚,还有发代码的时候是加什么符号去了??谢谢,还是[php] [/php] 吗?
发表于 2005-1-8 09:55:26 | 显示全部楼层
这个要加(去掉空格)                     
[ PHP][/ PHP]
您需要登录后才可以回帖 登录 | 注册

本版积分规则

快速回复 返回顶部 返回列表