|
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="提交"/>  
<html:reset value="重写"/>
</html:form>
 
没有
<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="提交"/>
 
<html:reset value="重写"/>
</html:form>
</center>
</body>
</html:html>
[/PHP] |
|