Skip to content

Commit

Permalink
version 0.2.3 update
Browse files Browse the repository at this point in the history
将JDBC初始化功能交由监听器实现,并移除对应的过滤器;使用过滤器实现JDBC反注册,避免内存泄漏;使用注解替代web.xml完成servlet和监听器的注册;重写前端校验,对于支持html5的浏览器使用其自带校验
  • Loading branch information
ZongXR committed Aug 25, 2020
1 parent 1bfb31e commit a3b0cd1
Show file tree
Hide file tree
Showing 14 changed files with 157 additions and 223 deletions.
13 changes: 13 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,18 @@
</td>
<td>2020年8月24日</td>
</tr>
<tr>
<td>0.2.3</td>
<td>
<ul>
<li>将JDBC初始化功能交由监听器实现,并移除对应的过滤器</li>
<li>使用过滤器实现JDBC反注册,避免内存泄漏</li>
<li>使用注解替代web.xml完成servlet和监听器的注册</li>
<li>重写前端校验,对于支持html5的浏览器使用其自带校验</li>
</ul>
</td>
<td>2020年8月25日</td>
</tr>
</table>
<h2>配置情况</h2>
<ul>
Expand All @@ -131,6 +143,7 @@
<li>HttpServletRequest的装饰者类HttpServletRequestDecorator中的getParameterMap重写方法中,不能在原地进行参数字符集修改,否则如果调用两次及以上该方法时会编码多次,造成乱码</li>
<li>对请求参数的拦截处理,如果用到装饰者模式,最好在一处完成。如果在多处完成会造成请求参数的重复处理,从而导致乱码或者拿到非预期的参数值</li>
<li>用户的登录信息最好保存到session域中,在0.2.1中的自动登录成功后误把user对象塞入到了request域,造成了重启服务器自动登录失效的bug。</li>
<li>使用过滤器将密码加密,不仅要将参数为password的值加密,还要将参数为password2的值加密,否则永远确认密码不一致</li>
</ul>
</body>
</html>
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ public String getParameter(String name) {
String encryptMethod = this.request.getServletContext().getInitParameter("encryptMethod");
if (result == null)
return null;
else if ("password".equalsIgnoreCase(name))
else if ("password".equalsIgnoreCase(name) || "password2".equalsIgnoreCase(name))
// 如果键是password,对其进行加密处理
return WebUtils.encrypt(result[0], encryptMethod);
else
Expand Down
26 changes: 0 additions & 26 deletions src/com/supermarket/filter/UseDataSource.java

This file was deleted.

5 changes: 4 additions & 1 deletion src/com/supermarket/listener/ApplicationListener.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.supermarket.listener;

import com.supermarket.utils.JDBCUtils;
import org.apache.log4j.Logger;

import javax.servlet.ServletContext;
Expand All @@ -23,10 +24,12 @@ public class ApplicationListener implements ServletContextListener{
// -------------------------------------------------------

/**
* web应用启动回调函数
* web应用启动回调函数,在其中注册JDBC驱动
* @param sce ServletContext事件
*/
public void contextInitialized(ServletContextEvent sce) {
String datasource = sce.getServletContext().getInitParameter("datasource");
JDBCUtils.setPool(datasource);
log.debug("web应用已创建");
}

Expand Down
2 changes: 1 addition & 1 deletion src/com/supermarket/web/AjaxUsernameServlet.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet(name = "AjaxUsernameServlet")
@WebServlet(name = "AjaxUsernameServlet", value = "/AjaxUsernameServlet")
public class AjaxUsernameServlet extends HttpServlet {
private UserService userService = new UserService();

Expand Down
2 changes: 1 addition & 1 deletion src/com/supermarket/web/LoginServlet.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
import java.io.IOException;
import java.util.Objects;

@WebServlet(name = "LoginServlet")
@WebServlet(name = "LoginServlet", value = "/LoginServlet")
public class LoginServlet extends HttpServlet {
private UserService userService = new UserService();
private ValistrService valistrService = new ValistrService();
Expand Down
2 changes: 1 addition & 1 deletion src/com/supermarket/web/LogoutServlet.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
import javax.servlet.http.HttpSession;
import java.io.IOException;

@WebServlet(name = "LogoutServlet")
@WebServlet(name = "LogoutServlet", value = "/LogoutServlet")
public class LogoutServlet extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws IOException {
this.doGet(request, response);
Expand Down
2 changes: 1 addition & 1 deletion src/com/supermarket/web/RegistServlet.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
import java.io.IOException;
import java.sql.SQLException;

@WebServlet(name = "RegistServlet")
@WebServlet(name = "RegistServlet", value = "/RegistServlet")
public class RegistServlet extends HttpServlet {
private UserService userService = new UserService();
private ValistrService valistrService = new ValistrService();
Expand Down
2 changes: 1 addition & 1 deletion src/com/supermarket/web/ValiImgServlet.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
import java.io.IOException;
import java.io.OutputStream;

@WebServlet(name = "ValiImgServlet")
@WebServlet(name = "ValiImgServlet", value = "/ValiImgServlet")
public class ValiImgServlet extends HttpServlet {
private ValistrService valistrService = new ValistrService();
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
Expand Down
Binary file not shown.
73 changes: 0 additions & 73 deletions web/WEB-INF/web.xml
Original file line number Diff line number Diff line change
Expand Up @@ -14,16 +14,6 @@
<url-pattern>/*</url-pattern>
</filter-mapping>

<!-- 指定全局连接池 -->
<filter>
<filter-name>UseDataSource</filter-name>
<filter-class>com.supermarket.filter.UseDataSource</filter-class>
</filter>
<filter-mapping>
<filter-name>UseDataSource</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>

<!-- 自动登录过滤器 -->
<filter>
<filter-name>AutoLogin</filter-name>
Expand Down Expand Up @@ -64,67 +54,4 @@
<param-value>md5</param-value>
</context-param>

<!-- 注册功能Servlet -->
<servlet>
<servlet-name>RegistServlet</servlet-name>
<servlet-class>com.supermarket.web.RegistServlet</servlet-class>
<init-param>
<param-name>datasource</param-name>
<param-value>c3p0</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>RegistServlet</servlet-name>
<url-pattern>/RegistServlet</url-pattern>
</servlet-mapping>

<!-- 查看用户名是否重复的ajax -->
<servlet>
<servlet-name>AjaxUsernameServlet</servlet-name>
<servlet-class>com.supermarket.web.AjaxUsernameServlet</servlet-class>
<init-param>
<param-name>datasource</param-name>
<param-value>c3p0</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>AjaxUsernameServlet</servlet-name>
<url-pattern>/AjaxUsernameServlet</url-pattern>
</servlet-mapping>

<!-- 用于生成验证码的servlet -->
<servlet>
<servlet-name>ValiImgServlet</servlet-name>
<servlet-class>com.supermarket.web.ValiImgServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>ValiImgServlet</servlet-name>
<url-pattern>/ValiImgServlet</url-pattern>
</servlet-mapping>

<!-- 登陆的servlet -->
<servlet>
<servlet-name>LoginServlet</servlet-name>
<servlet-class>com.supermarket.web.LoginServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>LoginServlet</servlet-name>
<url-pattern>/LoginServlet</url-pattern>
</servlet-mapping>

<!-- 注销servlet -->
<servlet>
<servlet-name>LogoutServlet</servlet-name>
<servlet-class>com.supermarket.web.LogoutServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>LogoutServlet</servlet-name>
<url-pattern>/LogoutServlet</url-pattern>
</servlet-mapping>

<!-- 监听web应用的listener -->
<listener>
<listener-class>com.supermarket.listener.ApplicationListener</listener-class>
</listener>

</web-app>
65 changes: 62 additions & 3 deletions web/js/methods.js
Original file line number Diff line number Diff line change
@@ -1,19 +1,78 @@
// 判断是否为空
function isNull(element, msg) {
let node = $(element);
if (node.val() === ""){
if (node.val() === "") {
// 如果是空
node.nextAll("span").text(msg).css("color", "red");
return true;
}else{
} else {
// 如果非空
node.nextAll("span").text("");
return false;
}
}

// 正则校验
function isRegexValid(element, regex, msg) {
let node = $(element);
if (node.val() === "") {
// 空值
return false;
} else if (regex.test(node.val())) {
// 正则验证通过
node.nextAll("span").text("");
return true;
} else {
// 正则不通过
node.nextAll("span").text(msg).css("color", "red");
return false;
}
}

// 值相等校验
function isEqual(element1, node2, msg) {
let node1 = $(element1);
if (node1.val() === node2.val() && node1.val() !== "" && node2.val() !== "") {
// 二者相等
node1.nextAll("span").text("");
node2.nextAll("span").text("");
return true;
} else if (node1.val() === "" || node2.val() === "") {
// 存在为空的
} else {
// 二者不相等,对离焦控件做出提示
node1.nextAll("span").text(msg).css("color", "red");
return false;
}
}

// 刷新验证码
function refreshValistr(element){
function refreshValistr(element) {
let time = new Date().getTime();
$(element).attr("src", "/ValiImgServlet?time=" + time);
}

// 可用性校验
function isAvailable(element, msg1, msg2, path) {
let node = $(element);
if (node.val() === "") {
// 如果没通过非空校验,直接返回false
node.nextAll("span").text("");
return false;
}
$.ajax({
"url": path,
"data": {[node.attr("name")]: node.val()},
"async": true,
"type": "POST",
"success": function (result) {
if (eval(result)) {
// 用户名可以使用
node.nextAll("span").text(msg1).css("color", "#00716d");
} else {
// 用户名不可以使用
node.nextAll("span").text(msg2).css("color", "red");
}
}
});
}
11 changes: 8 additions & 3 deletions web/login.jsp
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,11 @@
// 如果用户名不为空,则记住用户名
$("input[name='remname']").attr("checked", "checked");
}
// 如果支持html5,那么使用自带校验
if (window.applicationCache){
$("form").removeAttr("onsubmit");
$("input").removeAttr("onblur");
}
})
// 提交表单的检查函数
Expand All @@ -46,21 +51,21 @@
<tr>
<td class="tdx">用户名:</td>
<td>
<input type="text" name="username" value="" onblur="isNull(this, '用户名不能为空')"/>
<input type="text" name="username" value="" required="required" onblur="isNull(this, '用户名不能为空')"/>
<span></span>
</td>
</tr>
<tr>
<td class="tdx">密&nbsp;&nbsp; 码:</td>
<td>
<input type="password" name="password" onblur="isNull(this, '密码不能为空')"/>
<input type="password" name="password" required="required" onblur="isNull(this, '密码不能为空')"/>
<span></span>
</td>
</tr>
<tr>
<td class="tdx">验证码:</td>
<td>
<input type="text" name="valistr" onblur="isNull(this, '验证码不能为空')"/>
<input type="text" name="valistr" required="required" onblur="isNull(this, '验证码不能为空')"/>
<img src="${pageContext.request.contextPath}/ValiImgServlet" width="" height="" alt="验证码"
onclick="refreshValistr(this)"/>
<span></span>
Expand Down
Loading

0 comments on commit a3b0cd1

Please sign in to comment.