用户登陆后,在每个JSP页面中如何判断,用户是否登陆?当用户退出登陆后,使每个JSP页面无法访问? 用户登陆后,在每个JSP页面中如何判断,用户是否登陆?当用户退出登陆后,使每个JSP页面无法访问?分数不多了,请大家多包涵!在每个JSP页面中添加什么样的代码,能实现上述的验证登陆的功能?希望给出完整的示例代码. 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 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);} 没必要这么复杂,你可以做一个判断页,在这个判断页中进行检查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"/><% } %> 2、3楼的方法我认为太复杂!我认为是在登录页面保存把ID和PASSWORD保存在SEEEION对象中然后在其它JSP页面中读取,来判断是否登录!最好的办法是通过filter过滤器来实现统一验证! 高手们给我讲一讲JNDI 登录老出错! 偷偷问个很龌龊的东西:有关文件的上传,服务器集群以后的问题 如何将日期(一串数字)(以秒为单位) 转换为 格式(yyyy-MM-dd hh:mm:ss) 一道数据库面试题 strtus最简单的应用 jsp超级难题,排序问题 我想在后台编写一个程序,然后每个半个小时执行一次!应该怎么做?JAVA类能实现吗?怎么实现? 求助,关于JSP的数据库显示问题,我想用一个表显示出来,该如何做,最好有原代码!!! jsp连接access那个方法效率高? Eclipse如何设定文件的打开方式?? 这是怎么回事?
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);
}
<%@ 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"/>
<% } %>
然后在其它JSP页面中读取,来判断是否登录!最好的办法是通过filter过滤器来实现统一验证!