加载jsp之前需要验证

时间:2013-05-02 19:14:55

标签: java servlets jspinclude

我有一个场景,我希望在welcome.jsp页面加载之前进行一些验证。实际上验证就像我有一个用户ID(使用request.getRemoteUser),我想在我的Db中检查它是否已经存在。如果是,那么我想将其重定向到另一个页面,例如login.jsp。如果没有,它将重定向到我的welcome.jsp。我已经声明了一个servlet(CheckUser.java),我在doGet中调用doPost,并在welcome.jsp中包含相同的servlet,这样在加载之前doGet就会调用。

**CheckUser.java**

package DBResource;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class CheckUser extends DBConnection {
        String page="ListProjects.jsp";
        public Connection conn= null;
        // This Happens Once and is Reused
@Override
public void init(ServletConfig config) throws ServletException
   {
       super.init(config);                       
   }

public boolean SearchUser(String Usrid)  throws SQLException{
   try{

        // Establishing connection using data source
       conn=ds.getConnection();

       PreparedStatement stmt=conn.prepareStatement("select * from OSS_USER where USER_EMAIL='"+Usrid+"'");

                ResultSet rs = stmt.executeQuery();

                boolean hasResult = rs.next();
                return hasResult;

   }

   catch (Exception e){
        e.printStackTrace();
        return false;
    }
   finally {
        if (conn != null) conn.close();
    }

}


@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {
    System.out.println("on CheckUser doGet...........");
    doPost(request,response);

}


@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {


            System.out.println("on CheckUser doPost..........");
            //String Usrid= request.getRemoteUser();
            String Usrid= "abc@tr.com";

            contextPath = request.getContextPath();
            page = "/ListProjects.jsp";


    try {
        boolean susr =  SearchUser(Usrid);
        if (susr==true)
        {
            response.sendRedirect(contextPath + "/ListProjects.jsp");   

        } 
    } catch (SQLException ex) {
        Logger.getLogger(DBConnection.class.getName()).log(Level.SEVERE, null, ex);
    }
    finally {
        if (conn != null) try {
            conn.close();
        } catch (SQLException ex) {
            Logger.getLogger(DBConnection.class.getName()).log(Level.SEVERE, null, ex);
        }
    }


}

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

}

Welcome.jsp中

<body>

    <jsp:include page="/CheckUser" flush="true" />          

2 个答案:

答案 0 :(得分:2)

使用bean&amp; amp;和/或小脚本。在Jsp页面中,我导入了我的java类(StoreUsrDetails)并声明了一个jsp use bean(storeusr)。

在jsp页面

<%@page import="com.thomsonreuters.alpaca.StoreUsrDetails" %>
<jsp:useBean id="storeusr" class="com.thomsonreuters.alpaca.StoreUsrDetails" scope="request" />

使用bean我在我的java方法(validateUser)中调用一个方法,该方法返回数据库中是否存在用户详细信息。 之后我在jsp身体的开头写了一个scriplet代码。并且基于validateUser方法的retun值,我将它们重定向到另一个页面。

Jsp Body

<body>
    <% 

           if(storeusr.validateUser(request.getRemoteUser()))  
          response.sendRedirect("ListProjects.jsp");   


    %> 

答案 1 :(得分:0)

1)您需要扩展HTTPServlet以使您的班级CheckUser成为Servlet

即: Public class CheckUser extends HTTPServlet

2)什么是DBConnection?这会继承HTTPServlet吗?

3)然后您需要在 web.xml 中配置CheckUser servlent。

即:

<servlet>
  <servlet-name>checkUser</servlet-name>
  <servlet-class>package.CheckUser</servlet-class>
</servlet>

<servlet-mapping>
  <servlet-name>checkUser</servlet-name>
  <url-pattern>/CheckUser</url-pattern>
</servlet-mapping>

注意:请提及您所做的事情以及您面临的困难