我的思路是:
有4个jsp文件,登陆login.jsp 还有判断用户jugelogin.jsp,登陆成功页面:loginsuccess.jsp,还有filtepage.jsp这个是成功之后才能看到的链接页面。把loginsuccess.jsp和filtepage.jsp放到了一个拥有权限才能访问的文件夹Auth中,路径目录是(myeclipse的IDE)项目名是Filter,所以各个文件的路径分别为:login.jsp、jugelogin.jsp还有Auth文件夹都位于WebRoot中.
还有一个过滤器文件是:com.filter.AuthFilter,和配置文件Web.xml.但是我在启动服务器,浏览器中输入http://127.0.0.1:8081/Filter/login.jsp的时候可能是路径不对还是什么不对,就是什么也不显示就是
type Status reportmessage /Filter/login.jspdescription The requested resource (/Filter/login.jsp) is not available.
我觉得是文件路径有问题,连login.jsp 都不显示,连错误都不报。查了好多遍,都看不出来。求哪位能帮帮忙吧?主要代码如下:
login.jsp:
<body>
<h1>登陆页面</h1>
<form action="jugelogin.jsp">
姓名:<input type="text" name="username">
密码:<input type="password" name="password">
<input type="submit" value="提交" >
</form>
</body>
jugelogin.jsp:
<%
String username = request.getParameter("username");
String password = request.getParameter("password");
System.out.print("username:"+username);
System.out.print("password:"+password);
if(username.trim().equals("tom")&&password.trim().equals("tom")){
session.setAttribute("isLogin","true");
response.sendRedirect("/Auth/loginsucess.jsp");
}else{
response.sendRedirect("login.jsp");
}
%>
loginsuccess.jsp:
<body>
欢迎您,登陆成功!点击进入受限的页面
<a href ="/Auth/filterpage.jsp">进入过滤器受限页面</a>
</body>
filterpage.jsp:
<body>
当你看到这个页面的时候代表是登陆成功 过滤器符合条件的用户
</body>
AuthFilter.java
public void doFilter(ServletRequest req, ServletResponse res,
FilterChain chain) throws IOException, ServletException {
HttpServletRequest hreq =(HttpServletRequest)req;
HttpServletResponse hres = (HttpServletResponse)res;
HttpSession session = hreq.getSession();
String isLogin = (String)session.getAttribute("isLogin");
System.out.println(isLogin);
try{
if("true".equals(isLogin)&&isLogin!=null){
System.out.println("您已经通过了~~");
chain.doFilter(req, res);
}else{
hres.sendRedirect("login.jsp");
}
}catch(Exception e ){
e.printStackTrace();
}
} public void init(FilterConfig config) throws ServletException {
this.fconfig = config;
}
public void destroy() {
fconfig = null;
}web.xml:
<filter>
<filter-name>AuthFilter</filter-name>
<filter-class>com.filter.AuthFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>AuthFilter</filter-name>
<url-pattern>/Auth/*</url-pattern>
</filter-mapping>
<welcome-file-list>
<welcome-file>login.jsp</welcome-file>
</welcome-file-list>
注:以上只是部分主要代码。请大家给看看啊~
有4个jsp文件,登陆login.jsp 还有判断用户jugelogin.jsp,登陆成功页面:loginsuccess.jsp,还有filtepage.jsp这个是成功之后才能看到的链接页面。把loginsuccess.jsp和filtepage.jsp放到了一个拥有权限才能访问的文件夹Auth中,路径目录是(myeclipse的IDE)项目名是Filter,所以各个文件的路径分别为:login.jsp、jugelogin.jsp还有Auth文件夹都位于WebRoot中.
还有一个过滤器文件是:com.filter.AuthFilter,和配置文件Web.xml.但是我在启动服务器,浏览器中输入http://127.0.0.1:8081/Filter/login.jsp的时候可能是路径不对还是什么不对,就是什么也不显示就是
type Status reportmessage /Filter/login.jspdescription The requested resource (/Filter/login.jsp) is not available.
我觉得是文件路径有问题,连login.jsp 都不显示,连错误都不报。查了好多遍,都看不出来。求哪位能帮帮忙吧?主要代码如下:
login.jsp:
<body>
<h1>登陆页面</h1>
<form action="jugelogin.jsp">
姓名:<input type="text" name="username">
密码:<input type="password" name="password">
<input type="submit" value="提交" >
</form>
</body>
jugelogin.jsp:
<%
String username = request.getParameter("username");
String password = request.getParameter("password");
System.out.print("username:"+username);
System.out.print("password:"+password);
if(username.trim().equals("tom")&&password.trim().equals("tom")){
session.setAttribute("isLogin","true");
response.sendRedirect("/Auth/loginsucess.jsp");
}else{
response.sendRedirect("login.jsp");
}
%>
loginsuccess.jsp:
<body>
欢迎您,登陆成功!点击进入受限的页面
<a href ="/Auth/filterpage.jsp">进入过滤器受限页面</a>
</body>
filterpage.jsp:
<body>
当你看到这个页面的时候代表是登陆成功 过滤器符合条件的用户
</body>
AuthFilter.java
public void doFilter(ServletRequest req, ServletResponse res,
FilterChain chain) throws IOException, ServletException {
HttpServletRequest hreq =(HttpServletRequest)req;
HttpServletResponse hres = (HttpServletResponse)res;
HttpSession session = hreq.getSession();
String isLogin = (String)session.getAttribute("isLogin");
System.out.println(isLogin);
try{
if("true".equals(isLogin)&&isLogin!=null){
System.out.println("您已经通过了~~");
chain.doFilter(req, res);
}else{
hres.sendRedirect("login.jsp");
}
}catch(Exception e ){
e.printStackTrace();
}
} public void init(FilterConfig config) throws ServletException {
this.fconfig = config;
}
public void destroy() {
fconfig = null;
}web.xml:
<filter>
<filter-name>AuthFilter</filter-name>
<filter-class>com.filter.AuthFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>AuthFilter</filter-name>
<url-pattern>/Auth/*</url-pattern>
</filter-mapping>
<welcome-file-list>
<welcome-file>login.jsp</welcome-file>
</welcome-file-list>
注:以上只是部分主要代码。请大家给看看啊~
解决方案 »
- 在JSP里面如何获取document.getElementById('EmployeeName').value的值
- 急 数据类型
- iReport html中文乱码问题
- 帮忙看下javabean的问题,谢谢!
- 请问,如何把“上海”下的所以子目录(包括“上海”)移动到“北京”的前面
- 用数据源连接SQLSERVER的小问题??/
- 奉劝网上交易的程序员们,为了自己的辛勤汗水,一定要在收到买方的汇款后,再给他发送软件等东西,亲身经历。事情这样:
- 怎样按指定的长度把内容输出在网页上?
- 服务器为"tomcat+jdk1.4"的配置问题????很急!!!!!!!
- win xp 下tomcat 问题
- jquery高手进来拿分!
- 启动tomcat是找不到actionservlet
<welcome-file-list>
<welcome-file>login.jsp </welcome-file>
</welcome-file-list>
直接输入:http://localhost:8081/Filter
你得确定你的tomcat端口号是8081,项目名称是Filter,看看能不能出来login.jsp。如果不能,进入tomcat物理目录,删掉这个项目,重新发布,访问看看。
不行!至于重新部署项目,我都试过,就是出不来。我觉得至少应该可以找到首页才对啊~~是不是我配置的Web.xml有问题呢?还有就是 目录路径 那有没有问题呢?我觉得没有问题啊~~苦恼!
AuthFilter.java 中的有写东西看不懂是什么!你想要做的就是一个限制阅读权限的功能也就是只有登陆以后才能进入页面,否则就再次跳转到登陆页面。如果我没有理解错的话。首先你的jugelogin.jsp 页面中在判断if(username.trim().equals("tom")&&password.trim().equals("tom"))
之前一定要先判断是否为空。负责如果不登陆直接进入此页一定报空指针异常。
你既然要做必须登录后才能显示页面就应该在每个页面都要加入判断是否登录的验证。看你用了session是没错了。可你只在一个中判断了。为了少些代码把判断单独些一个jsp页面然后插入到没一个页面中!
看你用http://127.0.0.1:8081/Filter/login.jsp这个地址难道你在WebRoot文件夹下有创建了一个Filter文件夹吗~!还有Tomcat的默认端口是8080吧!8081你在安装的时候只自己设置的吗!
建议在开网页之前先用http://127.0.0.1:8081打开Tomcat 看是否能进入管理界面再看看 发布的站点里面有没有你发布的站点。