| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 633 人关注过本帖
标题:关于是否登陆!!
只看楼主 加入收藏
HB515560244
Rank: 1
等 级:新手上路
帖 子:48
专家分:0
注 册:2007-10-9
收藏
 问题点数:0 回复次数:9 
关于是否登陆!!

我的Filter:

package myFilter;

import java.io.*;

import javax.servlet.*;
import javax.servlet.http.*;

public class SessionChecker implements Filter {

private ServletContext context;
private String targetURI;
public void init(FilterConfig config) throws ServletException {
// TODO 自动生成方法存根
context =config.getServletContext();
targetURI=config.getInitParameter("targetURI");
System.out.print(targetURI);
}

public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
// TODO 自动生成方法存根
HttpServletRequest httprequest=(HttpServletRequest) request;
HttpServletResponse httpresponse=(HttpServletResponse)response;
HttpSession session =httprequest.getSession();
String loginname=(String)session.getAttribute("myLoginName");
System.out.print(loginname);(这里是为空啊,)
if (loginname!=null){
chain.doFilter(httprequest, httpresponse);
}else {
httprequest.getRequestDispatcher(targetURI).forward(httprequest, httpresponse);
}
}
public void destroy() {
// TODO 自动生成方法存根

}
}

我的Servlet:
package myServlet;

import javax.servlet.*;
import javax.servlet.http.*;

import java.io.*;
public class Log_Servlet extends HttpServlet {

/**
*
*/
private static final long serialVersionUID = 6655394384809860986L;

public Log_Servlet(){
super();
}
protected void doPost(HttpServletRequest request,HttpServletResponse response)
throws ServletException,IOException{
response.setContentType("text/html;charset=gb2312");
request.getRequestDispatcher("index.jsp").forward(request,response);
}
public void doGet(HttpServletRequest request,HttpServletResponse response)
throws ServletException,IOException{
doPost( request, response);
}
}

我需要过滤的东西:
<%@ page contentType="text/html;charset=gb2312" %>

<html>
<head>
<title>学生基本信息操作</title>
</head>

<body>
<a href="Log_Servlet">我要下载 </a>
</body>

</html>

我的web.xml配置:
<filter>
<filter-name>SessionChecker</filter-name>
<filter-class>myFilter.SessionChecker</filter-class>
<init-param>
<param-name>targetURI</param-name>
<param-value>/user/Login0.jsp</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>SessionChecker</filter-name>
<servlet-name>/Log_Servlet</servlet-name>
   <dispatcher>FORWARD</dispatcher>(这个要加吗?)
</filter-mapping>

我想要的结果就是,当你没登陆时,你点"我要下载"时会转到登陆页面,

但现在直接到"index.jsp"页面,我的哪里错了,能告诉我吗?谢谢!在线等..




搜索更多相关主题的帖子: 登陆 
2007-11-06 22:33
HB515560244
Rank: 1
等 级:新手上路
帖 子:48
专家分:0
注 册:2007-10-9
收藏
得分:0 
顶顶啊,
2007-11-06 22:56
netstriker
Rank: 1
等 级:新手上路
威 望:1
帖 子:257
专家分:0
注 册:2007-3-24
收藏
得分:0 
帮你修改了一下,现在可以了,
<filter>
<filter-name>SessionChecker</filter-name>
<filter-class>myFilter.SessionChecker</filter-class>
<init-param>
<param-name>targetURI</param-name>
<param-value>/user/Login0.jsp</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>SessionChecker</filter-name>
<servlet-name>/Log_Servlet</servlet-name> <--这一个是一个servlet的名字不是它的一个路径名。
   <dispatcher>FORWARD</dispatcher>(这个要加吗?) <---可以不加上去
</filter-mapping>

我把这一个工程上传了,是Eclipse的工程,把它复制到Eclipse的workspace下就可以用了。如果还有什么问题再发帖吧
rY4gBOWi.rar (6.81 KB) 关于是否登陆!!


2007-11-07 09:55
HB515560244
Rank: 1
等 级:新手上路
帖 子:48
专家分:0
注 册:2007-10-9
收藏
得分:0 

恩,谢谢哈.

2007-11-07 10:43
HB515560244
Rank: 1
等 级:新手上路
帖 子:48
专家分:0
注 册:2007-10-9
收藏
得分:0 
<filter-name>SessionChecker</filter-name>
<filter-class>myFilter.SessionChecker</filter-class>
<init-param>
<param-name>targetURI</param-name>
<param-value>user/Login.jsp</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>SessionChecker</filter-name>
<servlet-name>myservlet</servlet-name>
</filter-mapping>

<servlet>
<servlet-name>myservlet</servlet-name>
<servlet-class>myServlet.Log_Servlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>myservlet</servlet-name>
<url-pattern>/Log_Servlet</url-pattern>
</servlet-mapping>


大哥,我改了怎么还没用啊,
但我用你有用哦,
其他的地方我没改啊,
2007-11-07 11:30
netstriker
Rank: 1
等 级:新手上路
威 望:1
帖 子:257
专家分:0
注 册:2007-3-24
收藏
得分:0 
  哦,我不但改了你的web.xml文件啊,其他的地方也改了一些啊。仔细看一下吧。会有收获的。
2007-11-07 12:52
HB515560244
Rank: 1
等 级:新手上路
帖 子:48
专家分:0
注 册:2007-10-9
收藏
得分:0 

恩,呵呵

2007-11-07 13:00
HB515560244
Rank: 1
等 级:新手上路
帖 子:48
专家分:0
注 册:2007-10-9
收藏
得分:0 
能告诉我你的联系方式吗?QQ?
2007-11-07 13:01
HB515560244
Rank: 1
等 级:新手上路
帖 子:48
专家分:0
注 册:2007-10-9
收藏
得分:0 
<filter>
<filter-name>SessionChecker</filter-name>
<filter-class>com.filter.SessionChecker</filter-class>
<init-param>
<param-name>targetURI</param-name>
<param-value>login.jsp</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>SessionChecker</filter-name>
<servlet-name>0myservlet</servlet-name>
</filter-mapping>

<servlet>
<servlet-name>myservlet</servlet-name>
<servlet-class>com.Servlet.Log_Servlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>myservlet</servlet-name>
<url-pattern>/Log_Servlet</url-pattern>
</servlet-mapping>
大哥啊,我把你的改成上面那样,它怎么还会跳转到login.jsp页面上,
这不是没过滤了吗?应该要跳到index.jsp页面上的啊,


我该成了和你的一样了啊,还是没用哦,不知道是怎么了,我上面那个写对了吗?
我在下面再全贴出来,你在当页上改好吗?



2007-11-07 13:20
HB515560244
Rank: 1
等 级:新手上路
帖 子:48
专家分:0
注 册:2007-10-9
收藏
得分:0 

我的Filter:

package myFilter;

import java.io.IOException;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

public class SessionChecker implements Filter {

private ServletContext context;
private String targetURI;
public void init(FilterConfig config) throws ServletException {
// TODO 自动生成方法存根
context =config.getServletContext();
targetURI=config.getInitParameter("targetURI");
System.out.print(targetURI);
}

public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException
{
// TODO 自动生成方法存根
HttpServletRequest httprequest=(HttpServletRequest) request;
HttpServletResponse httpresponse=(HttpServletResponse)response;
HttpSession session =httprequest.getSession();
String loginname=(String)session.getAttribute("myLoginName");
System.out.print(loginname);
if (loginname!=null){
chain.doFilter(httprequest, httpresponse);
}else {
httprequest.getRequestDispatcher(targetURI).forward(httprequest, httpresponse);
}
}
public void destroy() {
// TODO 自动生成方法存根
}
}


我的Servlet:
package myServlet;

import javax.servlet.*;
import javax.servlet.http.*;

import java.io.*;
public class Log_Servlet extends HttpServlet {

/**
*
*/
private static final long serialVersionUID = 6655394384809860986L;

public Log_Servlet(){
super();
}
protected void doPost(HttpServletRequest request,HttpServletResponse response)
throws ServletException,IOException{
response.setContentType("text/html;charset=gb2312");
request.getRequestDispatcher("index.jsp").forward(request,response);
}
public void doGet(HttpServletRequest request,HttpServletResponse response)
throws ServletException,IOException{
doPost( request, response);
}
}


我需要过滤的东西:
<%@ page contentType="text/html;charset=gb2312" %>

<html>
<head>
<title>学生基本信息操作</title>
</head>

<body>
<a href="Log_Servlet">我要下载 </a>
</body>

</html>

我的web.xml配置:
<filter>
<filter-name>SessionChecker</filter-name>
<filter-class>myFilter.SessionChecker</filter-class>
<init-param>
<param-name>targetURI</param-name>
<param-value>UserList.jsp</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>SessionChecker</filter-name>
<servlet-name>myservlet</servlet-name>
</filter-mapping>

<servlet>
<servlet-name>myservlet</servlet-name>
<servlet-class>myServlet.Log_Servlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>myservlet</servlet-name>
<url-pattern>/Log_Servlet</url-pattern>
</servlet-mapping>




2007-11-07 13:23
快速回复:关于是否登陆!!
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.064898 second(s), 9 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved