ãè®°å½ãå¦ä½å®æ´å¾å®ç°ä¸ä¸ªç®æç»é注åç³»ç»
æ¦è¿°
å®ç°ä¸ä¸ªå®æ´çç»é注åç³»ç»éè¦å å«ä¸é¢è¿ä¸å¤§é¨åï¼é¡µé¢ï¼ç»é注åå ¥å£ï¼ãåå°ï¼å¤çç¨æ·è¡¨åï¼ãæ°æ®åºï¼åæ¾è´¦æ·æ°æ®ï¼
å¨é¡µé¢ç«¯è·åå°ç»éæè æ¯æ³¨åçä¿¡æ¯ï¼åæ¶å¯¹è¡¨åæ°æ®è¿è¡å¤å®æ¯å¦æ»¡è¶³ç¸åºæ¡ä»¶ï¼æ¯å¦å¯ç é¿åº¦æ¯å¦å°äº8ä½ï¼è´¦å·é¿åº¦æ¯å¦å°äº6ä½ï¼ä¸¤æ¬¡è¾å ¥çå¯ç æ¯å¦ä¸è´ççï¼
å¨é¡µé¢ç«¯è·åå°æ°æ®åå°±éè¦ååå°ä¼ è¾è¿äºæ°æ®
åå°æ¥å°è¿äºè¡¨åæ°æ®åå°±éè¦æ§è¡ä»¥ä¸æä½ï¼ä¾å¦ä½¿ç¨MD5å å¯å¯ç ãåæ°æ®åºä¼ å ¥æ³¨åä¿¡æ¯ãæ£æµè´¦å·æ¯å¦å·²ç»åå¨ãè¿åç»é/注åæ¯å¦æåç页é¢ççã
èå ³äºè¿æ¥æ°æ®åºï¼å°±éè¦å端è°ç¨DBUtilç»ä»¶æ¥å¿«æ·å®ç°ç»éï¼æ³¨åï¼å¤æåå¨çåè½ã
è¿å°±æ¯å®ç°å®æ´çç®æ注åç»éç³»ç»ç大è´æµç¨ã
详ç»æ¥éª¤ä»ç»
Part.1 页é¢
ââââ>页é¢ç«¯æ³¨å代ç
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<script>
function checkname(){
var element = document.getElementById("name");
var element2 = document.getElementById("tips1");
if(element.value.length<6){
element2.innerHTML="ç¨æ·å太çï¼è³å°6ä½ï¼";
}else{
element2.innerHTML="ç¨æ·åæ£ç¡®ï¼å¯ä»¥ä½¿ç¨ï¼";
}
}
function checkpassword(){
var element = document.getElementById("password");
var element2 = document.getElementById("tips2");
if(element.value.length<8){
element2.innerHTML="å¯ç 太çï¼è³å°8ä½ï¼";
}else{
element2.innerHTML="";
}
}
function checksame(){
var element = document.getElementById("password");
var element2 = document.getElementById("tips3");
var element3 = document.getElementById("password2");
if(element.value != element3.value){
element2.innerHTML="两次è¾å
¥å¯ç ä¸åï¼è¯·éæ°è¾å
¥ï¼ ";
}else{
element2.innerHTML="";
}
}
function submitcheck(){
var element01 = document.getElementById("name");
var element02 = document.getElementById("password");
var element03 = document.getElementById("password2");
if(element01.value==""){
return false;
}
if(element01.value.length<6){
return false;
}
if(element02.value.length<8){
return false;
}
if(element02.value.length==0){
return false;
}
if(element02.value != element03.value){
return false;
}
}
</script>
<form id="form1" action="./RegistServlet" οnsubmit="return submitcheck()">
<input id="name" placeholder="Pleace write the name you wanted" name="user_name" οnchange="checkname()" />
<span id="tips1"></span>
<br />
<input id="password" type="password" placeholder="Pleace write your password"name="user_password" οnchange="checkpassword()"/>
<span id="tips2"></span>
<br />
<input id="password2" type="password"placeholder="Pleace write your password again" name="user_password2" οnchange="checksame()" />
<span id="tips3"></span>
<br />
<input type="submit" value="Regist"/>
</form>
</body>
</html>
ââ>解æ
(å¾ å®æããã)
ââââ>页é¢ç«¯ç»é代ç
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<script>
function check(){
var element = document.getElementById("name");
if(element.value.length<6){
alert("ç¨æ·åé误ï¼è¯·ç¡®è®¤åè¾å
¥ï¼");
return;
}
}
</script>
<form action="./LoginServlet">
<input id="name" placeholder="Pleace write your name" name="user_name" οnchange="check()"/>
<br/>
<input id="password" type="password" placeholder="Pleace write your password" name="user_password"/>
<br/>
<input type="submit" value="login"/>
</form>
</body>
</html>
ââ>解æ
(å¾ å®æããã)
Part.2 å端
ââââ>å端注å代ç
package com.deepice.userinfo;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.deepice.util.DBUtil;
import com.deepice.util.MD5Util;
public class RegistServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String name=request.getParameter("user_name");
System.out.println(name);
String password = request.getParameter("user_password");
System.out.println(password);
password =MD5Util.getInstance().getMD5(password).toUpperCase();
System.out.println(password);
if(DBUtil.exist("select * from user_inf where name=?",name)){
System.out.println("111111");
request.setAttribute("message", "该ç¨æ·å已被注åï¼è¯·éæ°æ³¨åï¼");
request.getRequestDispatcher("result.jsp").forward(request, response);
}else {
if(DBUtil.upDate("insert into user_inf (name,password) values (?,?)",name,password)) {
request.setAttribute("message", "注åæåï¼");
request.getRequestDispatcher("result.jsp").forward(request, response);
}
}
}
}
ââââ>å端ç»é代ç
package com.deepice.userinfo;
import java.io.IOException;
import java.sql.ResultSet;
import java.sql.SQLException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.deepice.util.DBUtil;
import com.deepice.util.IRowMapper;
import com.deepice.util.MD5Util;
public class LoginServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String name = request.getParameter("user_name");
String password = request.getParameter("user_password");
password=MD5Util.getInstance().getMD5(password).toUpperCase();
IRowMapper rowMapper = (ResultSet resultSet)->{
try {
if(resultSet.next()) {
request.setAttribute("message", "ç»éæåï¼");
request.getRequestDispatcher("result.jsp").forward(request, response);
}else {
request.setAttribute("message", "å¯ç é误ï¼ç»é失败ï¼");
request.getRequestDispatcher("result.jsp").forward(request, response);
}
} catch (Exception e) {
e.printStackTrace();
}
};
DBUtil.select("select name from user_inf where name=? and password=?",rowMapper,name,password);
}
}
æªå®å¾ ç»â¦â¦
上一篇: 爆损,变着法儿玩套路