看我哪里错了,报java.lang.ClassNotFoundException: 错误

看我哪里错了,报java.lang.ClassNotFoundException: 错误

过滤器,过滤用户没登录不能登录 主页,报java.lang.ClassNotFoundException: 错误

注册

<%@ page language="java" contentType="text/html; charset=UTF-8"

    pageEncoding="UTF-8"%>

<!DOCTYPE html>

<html>

<head>

  <meta charset="UTF-8">

<title>用户注册</title>

<link href="css/common1.css" type="text/css" rel="stylesheet">

  <link href="css/add1.css" type="text/css" rel="stylesheet">

  <link rel="stylesheet" href="css/login1.css">

<script type="text/javascript">

function validate_form(){

//获取用户值

var username=document.getElementById("username").value;

if(username==null || username==""){

alert("用户不能为空");

return false;

}

//获取用户密码

var password=document.getElementById("password").value;

if(password==null || password==""){

alert("密码不能为空");

return false;

}

//获取用户再次输入密码

var repassword=document.getElementById("repassword").value;

if(repassword!=password){

alert("两次密码输入不一致");

return false;

}

return true;

}

</script>

</head>

<body>


>

    <div class="center-login">

      <div class="login-banner">

       <a href="#"><img src="image/login_banner.png" alt=""></a>

     </div>

     <div class="user-login">

       <div class="user-box">

         <div class="user-title">

           <p>用户注册</p>

         </div>

         <form id="regForm"  onsubmit="return validate_form()" class="login-table" action="${pageContext.request.contextPath }/RegistServlet" method="post">

          <div class="login-left">

            <label class="username">用户名&nbsp&nbsp&nbsp&nbsp</label>

            <input type="text" id="username" class="yhmiput" name="username">

          </div>

          <div class="login-left">

            <label class="username">密码&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp</label>

            <input type="password" id="password" class="yhmiput" name="password">

          </div>

  <div class="login-left">

            <label class="username">确认密码</label>

            <input type="password" id="repassword" class="yhmiput" name="repassword">

          </div>

 

        

        <div class="login-btn"><button>注册</button></div>

        </form>

      </div>

    </div>

  </div>



</body>

</html>

注册servlet

package com.imooc.web.servlet;


import java.io.IOException;

import java.util.HashMap;

import java.util.List;

import java.util.Map;


import javax.servlet.ServletException;

import javax.servlet.annotation.WebServlet;

import javax.servlet.http.HttpServlet;

import javax.servlet.http.HttpServletRequest;

import javax.servlet.http.HttpServletResponse;


import com.imooc.domain.User;

import com.imooc.service.UserService;

import com.imooc.service.impl.UserServiceImpl;


/**

 * Servlet implementation class RegistServlet

 */

@WebServlet("/RegistServlet")

public class RegistServlet extends HttpServlet {

private static final long serialVersionUID = 1L;

       

    /**

     * @see HttpServlet#HttpServlet()

     */

    public RegistServlet() {

        super();

        // TODO Auto-generated constructor stub

    }


/**

* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)

*/

protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

// 接收数据

String username=request.getParameter("username");

String password=request.getParameter("password");

//封装数据

User user=new User();

user.setUsername(username);

user.setPassword(password);

System.out.println("用户输入的账户密码是:"+user);

//处理数据

UserService userService1=new UserServiceImpl();

List<User> userList=(List<User>)getServletContext().getAttribute("userlist");

if(userList!=null ) {

userService1.regist(userList, user);

}

System.out.println("存储用户保存的账户密码:"+userList);

//显示数据

response.sendRedirect(request.getContextPath()+"/login.jsp");

}


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

doGet(request, response);

}

}

登录

<%@ page language="java" contentType="text/html; charset=UTF-8"

    pageEncoding="UTF-8"%>

<%String flag=request.getParameter("flag") ;%>

<!DOCTYPE html>

<html>

<head>

<meta charset="UTF-8">

<title>Insert title here</title>

 <link rel="stylesheet" href="css/login.css">

</head>

<body>

<div class="login">

            <div class="header">

                <h1>

                    <a href="/login.do">登录</a>

                  

                </h1>

                  <p style="color: red">${msg}</p>

                <button></button>

                

            </div>

            <form action="${pageContext.request.contextPath }/LoginServlet" method="post">

                <div class="name">

                    <input type="text" id="name" name="username">

                    <p></p>

                </div>

                <div class="pwd">

                    <input type="password" id="pwd" name="password">

                    <p></p>

                </div>

                <div class="code">

                    <input type="text" id="code" name="verifyCode" style="width: 150px">

                    &nbsp;&nbsp;&nbsp;&nbsp;

                    <img src="code.jpg" style="width: 150px;height: 42px;vertical-align: middle;">

                    <p></p>

                </div>

                <div class="btn-red">

                    <input type="submit" value="登录" id="login-btn">

                    <a href="register.jsp" id="login-btn">注册</a>

                </div>

            </form>

        </div>

        <script type="text/javascript">

        var flag='<%=flag%>';

        if("1"==flag){

        alert("请登录!");

        }

       

       

        </script>

</body>

</html>

登录servlet

package com.imooc.web.servlet;


import java.io.IOException;

import java.util.List;


import javax.servlet.ServletException;

import javax.servlet.annotation.WebServlet;

import javax.servlet.http.HttpServlet;

import javax.servlet.http.HttpServletRequest;

import javax.servlet.http.HttpServletResponse;


import com.imooc.domain.User;

import com.imooc.service.UserService;

import com.imooc.service.impl.UserServiceImpl;


/**

 * Servlet implementation class LoginServlet

 */

@WebServlet("/LoginServlet")

public class LoginServlet extends HttpServlet {

private static final long serialVersionUID = 1L;

       

    /**

     * @see HttpServlet#HttpServlet()

     */

    public LoginServlet() {

        super();

        // TODO Auto-generated constructor stub

    }


/**

* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)

*/

protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

//接收数据

String username=request.getParameter("username");

String password=request.getParameter("password");

//封装数据

User user=new User();

user.setPassword(password);

user.setUsername(username);

//处理数据

UserService userservice=new UserServiceImpl();

List<User> userlist=(List<User>) getServletContext().getAttribute("userlist");

User user1=userservice.login(userlist, user);

System.out.println(user1);

if(user1==null) {

//登录失败

request.setAttribute("msg","用户或密码错误,如未注册,请点击注册");

request.getRequestDispatcher("/login.jsp").forward(request, response);

}else {

//登录成功

request.getSession().setAttribute("user1", user1);

response.sendRedirect(request.getContextPath()+"/bookList.html");

}

}


/**

* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)

*/

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

// TODO Auto-generated method stub

doGet(request, response);

}


}

过滤器

package com.imooc.Filter;


import java.io.IOException;

import java.util.List;


import javax.servlet.Filter;

import javax.servlet.FilterChain;

import javax.servlet.FilterConfig;

import javax.servlet.ServletException;

import javax.servlet.ServletRequest;

import javax.servlet.ServletResponse;

import javax.servlet.http.HttpServletRequest;

import javax.servlet.http.HttpServletResponse;


import com.imooc.domain.User;


public class LoginFilter implements Filter {


@Override

public void destroy() {

// TODO Auto-generated method stub


}


@Override

public void doFilter(ServletRequest request, ServletResponse response, FilterChain fc)

throws IOException, ServletException {

HttpServletRequest hsrequest=(HttpServletRequest)request;

HttpServletResponse hsresponse=(HttpServletResponse)response;

List<User> userList=(List<User>) hsrequest.getServletContext().getAttribute("userlist");

if(userList==null) {

hsresponse.sendRedirect("/login.jsp?flag=1");

return;

}else {

fc.doFilter(request, response);

return;

}


}


@Override

public void init(FilterConfig arg0) throws ServletException {

// TODO Auto-generated method stub


}


}


配置过滤器

  <filter>

  <filter-name>LoginFilter</filter-name>

  <filter-class>com.imooc.Filter.LoginServlet</filter-class>

  </filter>

  <filter-mapping>

  <filter-name>LoginFilter</filter-name>

  <url-pattern>/bookList.html</url-pattern>

  </filter-mapping>

报错

严重: Exception starting filter [LoginFilter]

java.lang.ClassNotFoundException: com.imooc.Filter.LoginServlet

at org.apache.catalina.loader.WebappClassLoaderBase.loadClass(WebappClassLoaderBase.java:1360)

at org.apache.catalina.loader.WebappClassLoaderBase.loadClass(WebappClassLoaderBase.java:1182)

at org.apache.catalina.core.DefaultInstanceManager.loadClass(DefaultInstanceManager.java:546)

at org.apache.catalina.core.DefaultInstanceManager.loadClassMaybePrivileged(DefaultInstanceManager.java:527)

at org.apache.catalina.core.DefaultInstanceManager.newInstance(DefaultInstanceManager.java:150)

at org.apache.catalina.core.ApplicationFilterConfig.getFilter(ApplicationFilterConfig.java:262)

at org.apache.catalina.core.ApplicationFilterConfig.<init>(ApplicationFilterConfig.java:108)

at org.apache.catalina.core.StandardContext.filterStart(StandardContext.java:4546)

at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5191)

at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150)

at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1412)

at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1402)

at java.util.concurrent.FutureTask.run(FutureTask.java:266)

at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)

at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)

at java.lang.Thread.run(Thread.java:748)


正在回答 回答被采纳积分+1

登陆购买课程后可参与讨论,去登陆

2回答
芝芝兰兰 2019-11-03 16:19:15

同学你好。这里不需要加return呢~并且重定向时,需要增加上下文路径:

http://img1.sycdn.imooc.com//climg/5dbe8c0d090f92bd10710222.jpg

这里拼接了一个TestDemo项目作为测试,修改后,使用http://localhost:8080/TestDemo/bookList.html是可以被拦截并跳转的:

http://img1.sycdn.imooc.com//climg/5dbe8d4a0939694619140288.jpg

另外没有找到同学在哪里将userList放入hsrequest.getServletContext().setAttribute()呢,建议同学在RegistServlet中,将userList放入哦~

如果解答了同学的疑问,望采纳~

祝学习愉快~

芝芝兰兰 2019-11-03 10:36:25

同学你好。ClassNotFondException是找不到类产生的错误,报错信息提示,找不到类是com.imooc.Filter包下的LoginServlet这个类。

http://img1.sycdn.imooc.com//climg/5dbe3bed093574f307650236.jpg

从过滤器的定义可以看出,过滤器的确在com.imooc.Filter包下,过滤器的类名叫做LoginFilter而不叫LoginServlet:

http://img1.sycdn.imooc.com//climg/5dbe3c5a09337adc05210655.jpg

那么LoginServlet是哪来的呢?是配置文件中指定的filter-class出错了,系统根据这个filter-class找不到真正的类:

http://img1.sycdn.imooc.com//climg/5dbe3cb9095898a906420332.jpg


需要将其改为LoginFilter。


如果解答了同学的疑问,望采纳~

祝学习愉快~


  • 提问者 兮兮666 #1
    在麻烦看下,我 的过滤器没生效 ,是哪里错了啊
    2019-11-03 12:03:09
问题已解决,确定采纳
还有疑问,暂不采纳

恭喜解决一个难题,获得1积分~

来为老师/同学的回答评分吧

0 星
请稍等 ...
意见反馈 帮助中心 APP下载
官方微信

在线咨询

领取优惠

免费试听

领取大纲

扫描二维码,添加
你的专属老师