用户登陆后,在每个JSP页面中如何判断,用户是否登陆?当用户退出登陆后,使每个JSP页面无法访问?分数不多了,请大家多包涵!在每个JSP页面中添加什么样的代码,能实现上述的验证登陆的功能?希望给出完整的示例代码.

解决方案 »

  1.   

    String username = request.getParameter("username").trim
      String password = request.getParameter("userpassword").trim();
        if(username!=null&&!username.equals(""))
        {
        if(checkLogin(username,password))
        {  
            session.setAttribute("username",username);       
            response.sendRedirect("login.jsp");
          
         }
         else 
         {
          out.print("<script>");
           out.print("alert('用户名或密码错误,请重新登录');");
           out.print("history.go(-1);");
          out.print("</script>");
         }
         }
    //////////////////////////////////////////////////
    上面调用的方法
    public boolean checkLogin(String userName, String pwd)// 判断登录是否准确
    {
    boolean flag = false;
    String sql = "";
    String selectUserName = "";
    try {
    SqlserverDBConnection dbc = new SqlserverDBConnection();
    sql = "select username from TB_user where username='" + userName
    + "' and password='" + pwd + "'";
    ResultSet rs = null;
    rs = dbc.executeQuery(sql);
    while (rs.next()) {
    selectUserName = rs.getString("username");
    }
    rs.close();
    dbc.close();
    if (selectUserName.equals(userName.toString())) {
    flag = true;
    }
    } catch (Exception e) {
    // TODO: handle exception
    }
    return flag;
    }
    ///////////////////////////////////////////////////////////////////////////////
    然后使用filterif(session.getAttribute("name")==null || session.getAttribute("name")=="")
    {    
    Functions share = new Functions();
    response.sendRedirect(share.getProperties("sendUrl"));
    }
    else
    {
    fc.doFilter(req,res);
    }
      

  2.   

    没必要这么复杂,你可以做一个判断页,在这个判断页中进行检查login是否正常,如
    <%@ page import="javax.faces.context.FacesContext"%> 
    <%@ page import="javax.faces.el.ValueBinding"%>
    <%@ page import="com.sun.faces.util.Util"%>
    <%@ page import="com.login"%>
    <%
    ValueBinding binding = Util.getValueBinding("#{login}");
    login user=(login) binding.getValue(FacesContext.getCurrentInstance());
    String path = request.getContextPath();
    if(user.name==null){out.print("<script language='JScript'>self.parent.location='"+basePath+"';</script>");//自动返回到初始页}
    %>---------------------------------------------------<%@ taglib uri="http://java.sun.com/jsf/html" prefix="h" %>
    <%@ taglib uri="http://java.sun.com/jsf/core" prefix="f" %>
    <%@ taglib uri="http://java.sun.com/jstl/core" prefix="c"%><jsp:useBean class="arbeit.Login" id="login" scope="session"/>
    <%
    if (login.getName()==null){
    %>
    <c:redirect url="index.faces"/>
    <% } %>
      

  3.   

    2、3楼的方法我认为太复杂!我认为是在登录页面保存把ID和PASSWORD保存在SEEEION对象中
    然后在其它JSP页面中读取,来判断是否登录!最好的办法是通过filter过滤器来实现统一验证!