这是过滤器方法
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 打断也调试了看不出问题
这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。
V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。
V2EX is a community of developers, designers and creative people.