看我哪里错了,报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">用户名    </label>
<input type="text" id="username" class="yhmiput" name="username">
</div>
<div class="login-left">
<label class="username">密码       </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">
<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
- 参与学习 人
- 提交作业 676 份
- 解答问题 9666 个
本阶段将从前端网页搭建入手,到Java Web基础,前后端结合助你完成Java Web小白的蜕变!
了解课程
恭喜解决一个难题,获得1积分~
来为老师/同学的回答评分吧
0 星