这是过滤器方法
public void doFilter(ServletRequest req, ServletResponse resp, FilterChain chain) throws IOException, ServletException { HttpServletRequest request = (HttpServletRequest)req; HttpServletResponse response = (HttpServletResponse)resp; HttpSession session = request.getSession(); String path = request.getRequestURI(); if(path.contains("login.jsp") || path.contains("/login") ||path.contains("register.jsp") ||path.contains("register") || path.contains("/DrawValidateCode") || path.contains("/layui") || path.contains("/image")){ chain.doFilter(req, resp); //登录页面,登录请求,验证码请求,前端资源和图片一律放行 }else{ String username = (String)session.getAttribute("username"); if (username == null){ //用户不存在一律重定向到登录页 response.sendRedirect(request.getContextPath() + "/login.jsp"); }else { chain.doFilter(req, resp); } } }
这个注册 @WebServlet(urlPatterns = "/addUser") public class RegisterServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
System.out.printf("1111111111111");
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
req.setCharacterEncoding("utf-8");
String jsonRegistration = RequestUtil.getRequestBody(req);
Gson gson = new Gson();
JsonObject jsonObject = new JsonParser().parse(jsonRegistration).getAsJsonObject();
String username = jsonObject.get("username").getAsString();
String password = jsonObject.get("password").getAsString();
resp.setContentType("application/json");
BaseResponse<Integer> baseResponse = new BaseResponse<Integer>();
boolean registrationSuccess = AdminDao.insertAdministrator(username, password);
if (registrationSuccess) {
baseResponse.setCode(200);
baseResponse.setMsg("注册成功");
resp.sendRedirect(req.getContextPath() + "/login.jsp");
} else {
baseResponse.setCode(500);
baseResponse.setMsg("注册失败,请重试");
}
PrintWriter out = resp.getWriter();
out.print(gson.toJson(baseResponse));
out.flush();
out.close();
}
}
前端就 layui' <script> layui.use(['form'], function(){ var form = layui.form;
form.on('submit(registrationForm)', function(data){
var formData = {
username: data.field.username,
password: data.field.password
};
$.ajax({
url: '/addUser',
type: 'POST',
data: JSON.stringify(formData),
success: function(res) {
if(res.code === 200) {
layer.msg('注册成功!');
window.location.href = '/login.jsp';
} else {
layer.msg('注册失败!');
}
},
error: function(error) {
layer.msg('注册请求失败:' + error.statusText);
}
});
return false;
});
});
</script>
就是进不了这个放 addUser 打断也调试了看不出问题
1
mgzu 350 天前
github 整个 demo 先,这没法看
|
3
hemingyang OP @mgzu 我上传了,大佬看看
|
4
fiveStarLaoliang 350 天前
写惯了 springboot 项目, 现在遇到一个 jsp 的项目, 直接懵逼了, 都忘了怎么跑起来了
|
5
hemingyang OP @fiveStarLaoliang 我也是看得头大,哈哈,用 idea 摸索了会才运行起来
|
6
undeflife 350 天前
不确定是不是这个原因
https://stackoverflow.com/questions/26089902/webservlet-annotation-doesnt-work-with-tomcat-8 web-app version 要大于 3 |
7
hemingyang OP @undeflife 不是这个原因,我就新加了注册方法,其实其他请求都正常,我怀疑
https://github.com/hemingyang/studentManagement/blob/main/src/main/java/com/zcx/studentManagement/filter/LoginFilter.java 这个过滤器的原因 |
8
fiveStarLaoliang 350 天前
你把 path 打印一下呗, 看看请求的具体路径是什么
|
9
taogen 350 天前
你请求的路径是什么?有没有加 Context Path ?
request URI = Context Path + Servlet Path + Path Info + Query String e.g. http://localhost:8080/myproject/myservlet/remove?id=1 - Context Path: `/myproject` - Servlet Path: `/myservlet` - Path Info: `/remove` - Query String: `?id=1` |
10
chanchan 350 天前
看响应 404 ?估计路径不对
|
11
zoharSoul 350 天前
要不咱还是整个 spring 吧
|
12
siweipancc 350 天前
哇塞, 够原始, 我喜欢
|
13
javak 350 天前 1
@hemingyang 这 demo 给的太不负责了啊。 整成 maven 或者 gradle 或者 ant 的项目,把依赖弄齐,然后说明怎么跑起来了。直接丢源码,让我们自己去补全依赖。
|
14
ikas 350 天前
jdk17 加 tomcat8.5 测试了,可以访问到 addUser ,get 与 post 都可以
|
15
hemingyang OP |
16
hemingyang OP @ikas 我就想加个注册功能不知道为啥就是进不了方法
|
17
xuanbg 349 天前
后端方法进不去可以肯定是路径的问题,盲猜是文件没有访问权限
|
18
hemingyang OP @xuanbg https://mjj.today/i/8TMDkO 这是请到后台的信息
|
19
hemingyang OP 解决了 直接改成了
<form class="layui-form" method="post" id="registrationForm" action="/addUser"> 原生的 action 没有 layui <script> layui.use(['form'], function(){ var form = layui.form; form.on('submit(registrationForm)', function(data){ var formData = { username: data.field.username, password: data.field.password }; $.ajax({ url: '/addUser', type: 'POST', data: JSON.stringify(formData), success: function(res) { if(res.code === 200) { layer.msg('注册成功!'); window.location.href = '/login.jsp'; } else { layer.msg('注册失败!'); } }, error: function(error) { layer.msg('注册请求失败:' + error.statusText); } }); return false; }); }); </script> |