使用jsp和servlet使用mysql对Web应用程序进行身份验证

时间:2012-10-03 13:51:20

标签: mysql jsp servlets login web

我正在开发一个Web应用程序并且坚持使用身份验证代码,我已经在eclipse上使用hibernate完成了连接,现在它是关于身份验证当我提交身份验证时没有任何反应,请帮助我,我是新手它。 这是JSP文件:

    <%@ page language="java" contentType="text/html; charset=windows-1256"
pageEncoding="windows-1256"%>
    <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
    <html>
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
    <title>Survey Project</title>
    <link rel="stylesheet" href="css/screen.css" type="text/css" media="screen" title="default" />
    <!--  jquery core -->
    <script src="js/jquery/jquery-1.4.1.min.js" type="text/javascript"></script>

    <!-- Custom jquery scripts -->
    <script src="js/jquery/custom_jquery.js" type="text/javascript"></script>

     <!-- MUST BE THE LAST SCRIPT IN <HEAD></HEAD></HEAD> png fix -->
     <script src="js/jquery/jquery.pngFix.pack.js" type="text/javascript"></script>
     <script type="text/javascript">
     $(document).ready(function(){
     $(document).pngFix( );
     });
     </script>
     </head>
     <body id="login-bg" >
     <!-- Start: login-holder -->
     <div id="login-holder">

 <!-- start logo -->
 <div id="logo-login"></div>
 <!-- end logo -->

 <div class="clear"></div>
 <form name="form1" method="post" action="../LoginServlet">
 <!--  start loginbox ................................................................................. -->
 <div id="loginbox">

 <!--  start login-inner -->
 <div id="login-inner">
    <table border="0" cellpadding="0" cellspacing="0">
        <tr>
        <th>Username</th>
        <td><input type="text"  class="login-inp" id="login" /></td>
    </tr>
    <tr>
        <th>Password</th>
        <td><input type="password" value=""  onfocus="this.value=''" class="login-inp" id="pass" /></td>
    </tr>
    <tr>
        <th></th>
        <td><input type="button" class="submit-login" id="submit" /></td>
    </tr>
    </table>
    </div>
    <!--  end login-inner -->
    <div class="clear"></div>
        </div>
  <!--  end loginbox -->
     </form>  
  <!--  start forgotbox ................................................................................... -->
<div id="forgotbox">
    <div id="forgotbox-text">Please send us your email and we'll reset your password.</div>
    <!--  start forgot-inner -->
    <div id="forgot-inner">
    <table border="0" cellpadding="0" cellspacing="0">
    <tr>
        <th>Email address:</th>
        <td><input type="text" value=""   class="login-inp" /></td>
    </tr>
    <tr>
        <th> </th>
        <td><input type="button" class="submit-login"  /></td>
    </tr>
    </table>
    </div>
    <!--  end forgot-inner -->
    <div class="clear"></div>
    <a href="" class="back-login">Back to login</a>
</div>
<!--  end forgotbox -->

    </div>
   <!-- End: login-holder -->
   </body>
   </html>

这是登录的servlet:

    package stage.servlet;

    import java.io.IOException;
    import java.io.PrintWriter;
    import java.util.List;

    import javax.servlet.RequestDispatcher;
    import javax.servlet.ServletException;
    import javax.servlet.annotation.WebServlet;
    import javax.servlet.http.HttpServlet;
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;

    import stage.Personnel;
    import stage.dao.HibernateSessionFactory;

    import net.sf.hibernate.HibernateException;
    import net.sf.hibernate.Query;
    import net.sf.hibernate.Session;
    import net.sf.hibernate.Transaction;


    @WebServlet(name = "LoginServlet", urlPatterns = {"/LoginServlet"})
    public class LoginServlet extends HttpServlet {

 protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException, HibernateException {
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();
        try {


            String login = request.getParameter("login");
            String pass = request.getParameter("pass");

            // creation d'un request dispatcher
            RequestDispatcher rd = null;

            Session s = HibernateSessionFactory.getSessionFactory().openSession();
            Transaction trs = s.beginTransaction();
            Query q = s.createQuery("from personnel where login='"+login+"'");

            List<Personnel> l = q.list();
            if(l.isEmpty())
            {
                 rd = request.getServletContext().getRequestDispatcher("/pages/echec1.jsp");

            }
            else{
                Personnel p = l.get(0);

                if(p.getPassword().equals(pass))
                {

                    request.setAttribute("operation", "affichage");
                    rd = request.getServletContext().getRequestDispatcher("/AdminServlet");


                }   


                else{


                rd = request.getServletContext().getRequestDispatcher("/pages/echec2.jsp");

                }



                rd.forward(request, response);



            }

        }

        finally {            
            out.close();
        }

 }

// <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /** 
     * Handles the HTTP <code>GET</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        try {
            processRequest(request, response);
        } catch (HibernateException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

    /** 
     * Handles the HTTP <code>POST</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        try {
            processRequest(request, response);
        } catch (HibernateException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

    /** 
     * Returns a short description of the servlet.
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>

}

0 个答案:

没有答案