最近用struts寫個登陸的。老是不知道怎么寫
特別是那個action類關于獲取MYSQL數據庫里面的用戶密碼各位大哥有沒有好的例子借小弟我看一下啊。讓我學習學習

解决方案 »

  1.   

    登陆页面 index.html
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
    <html>
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=gb2312">
    <title>******系统</title>
    <script src="mySrcipt/login.js"></script>
    <script language="JavaScript" type="text/JavaScript">
    </script>
    </head><body marginwidth="0" marginheight="0">
    <center>
      <table width="770" height="510">
        <tr>
          <td width="781" height="451" valign="top"> 
            <p>&nbsp;</p>
            <p>&nbsp;</p>
            <p>&nbsp;</p>
            <p>&nbsp;</p>
            <p>&nbsp;</p>
            <p>&nbsp;</p>
            <p>&nbsp;</p>
            <p>&nbsp;</p>
            <p>&nbsp;</p>
            <form name="form1" method="post" action="/exam/admin_login.do">
              <table width="100%">
                <tr> 
                  <td width="50%" height="30">&nbsp;</td>
                  <td width="50%">用户登陆</td>
                </tr>
                <tr> 
                  <td height="30" align="right">用户名</td>
                  <td><input type="text" name="textfield" size="15" maxlength="15"></td>
                </tr>
                <tr> 
                  <td height="30" align="right">密 码</td>
                  <td><input name="textfield2" type="password" size="15" maxlength="15"></td>
                </tr>
                <tr>
                  <td height="29" align="right">&nbsp;</td>
                  <td>
       <input name="image1" type="image" id="image1" onClick="login()" src="image/btn_login.gif" align="absmiddle" width="60" height="25" border="0"> 
                    <a href="newuser.jsp" target="_blank"><img src="image/btn_reg.gif" width="60" height="25" border="0"></a> 
                </tr>
              </table>
            </form>
            
          </td>
      </tr>
    </table>
    </center>
    </body>
    </html>
    acction类
    //Created by MyEclipse Struts
    // XSL source (default): platform:/plugin/com.genuitec.eclipse.cross.easystruts.eclipse_4.1.1/xslt/JavaClass.xslpackage com.yourcompany.struts.action;import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;
    import javax.servlet.http.HttpSession;import org.apache.struts.action.Action;
    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 com.my.dbconnect.dbconnect;
    import com.my.login.login;
    import com.mysql.jdbc.Connection;/** 
     * MyEclipse Struts
     * Creation date: 06-07-2006
     * 
     * XDoclet definition:
     * @struts.action validate="true"
     * @struts.action-forward name="ok" path="/admin/index.jsp"
     */
    public class Admin_loginAction extends Action {
    // --------------------------------------------------------- Instance Variables // --------------------------------------------------------- Methods /** 
     * Method execute
     * @param mapping
     * @param form
     * @param request
     * @param response
     * @return ActionForward
     */
    public ActionForward execute(
    ActionMapping mapping,
    ActionForm form,
    HttpServletRequest request,
    HttpServletResponse response) {

    Connection con = null;
    String back_string=null;//跳转页面返回值
    String tz=null;//跳转页面


    //取提交过来的用户ID,用户密码
    String userid=(String)request.getParameter("textfield");
    String password=(String)request.getParameter("textfield2");
    //取配置文件中的数据库连接参数
    MessageResources messages=getResources(request);
    String dbDriver=messages.getMessage("jdbc.dbDriver");
    String dbUrl=messages.getMessage("jdbc.dbUrl");
    String dbUser=messages.getMessage("jdbc.dbUser");
    String dbPassword=messages.getMessage("jdbc.dbPassword");


    //调用数据库连接类,并初始化
    try{
    dbconnect db=new dbconnect(dbDriver,dbUrl,dbUser,dbPassword);
    con=db.link();


    //创建session对象,用于存储数据库连接对象
    HttpSession session = request.getSession(true); 
    session.setAttribute("lianjie",con);


    //调用用户登陆类,判断登陆用户的权限
    login lg=new login(con,userid,password);
    String purview=lg.checkPurview();

    if (purview.equals("系统管理员"))
    {
    tz="admin";
    }
    else
    {
    tz="wrong";
    back_string="用户名或密码错误,请核对后重新填写。";
    }

    }catch(Exception e){
    back_string="数据库连接失败,请确定数据库服务开启后,重新登陆。";
    tz="wrong";
    }

    request.setAttribute("bks",back_string);

    return mapping.findForward(tz);
    }}login.java类
    package com.my.login;import java.sql.PreparedStatement;
    import java.sql.ResultSet;import com.mysql.jdbc.Connection;public class login {
    String purview;//返回的用户权限
    Connection ction;
    String id;
    String psd;
    public login(Connection con,String userid,String password) {
    ction=con;
    id=userid;
    psd=password;
    }


    /*
     * 判断登陆用户的权限,并将得到的权限返回
     */
    public String checkPurview(){

    try{
    PreparedStatement sec=ction.prepareStatement("select * from password where id=? and password=?");
    sec.setString(1,id);
    sec.setString(2,psd);

    ResultSet ret=sec.executeQuery();

    /*判断登陆的用户在数据库中是否存在*/
    if (ret.next())
    {
    int purviewId=ret.getInt("purview");

    /*通过检索到的权限编号,到权限表检索对应的权限*/
    try{
    PreparedStatement sec2=ction.prepareStatement("select * from purview where id=?");

    sec2.setInt(1,purviewId);

    ResultSet ret2=sec2.executeQuery();

    while (ret2.next())
    {
    //乱码处理
    purview=new String((ret2.getString("purview")).getBytes("ISO8859_1"),"GBK");
    }


    }catch(Exception e2){

    }
    }
    else
    {
    purview="查无此人";
    }
    }catch(Exception e){

    }

    return purview;
    }
    }