import com.mvc.linuxidc.factory.DAOFactory;
import com.mvc.linuxidc.vo.User;
/**
*
* @author 偶my耶
* Servlet
*/
public class LoginServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
public void doGet(HttpServletRequest req,HttpServletResponse resp) throws ServletException,IOException{
String path = "login.jsp" ;
String userid = req.getParameter("userid") ;
String userpass = req.getParameter("userpass") ;
System.out.println(userid);
System.out.println(userpass);
List<String> info = new ArrayList<String>() ;
if(userid==null || "".equals(userid)){
info.add("用户名不能为空!!!") ;
}
if(userpass==null || "".equals(userpass)){
info.add("密码不能为空!!") ;
}
if(info.size()==0){
User user = new User() ;
user.setUserid(userid) ;
user.setPassword(userpass) ;
try{
if(DAOFactory.getIUserDAOInstance().findLogin(user)){
info.add("欢迎" + user.getName() + "登陆") ;
} else {
info.add("请重新登录") ;
}
}catch(Exception e){
e.printStackTrace() ;
}
}
req.setAttribute("info",info) ;
req.getRequestDispatcher(path).forward(req,resp) ;
}
public void doPost(HttpServletRequest req,HttpServletResponse resp) throws ServletException,IOException{
this.doGet(req,resp) ;
}
}
web.xml文件
<?xml version="1.0" encoding="ISO-8859-1"?>
<web-app xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee "
version="2.5">
<display-name>Welcome to Tomcat</display-name>
<description>
Welcome to Tomcat
</description>
<servlet>
<servlet-name>login</servlet-name>
<servlet-class>
com.mvc.linuxidc.servlet.LoginServlet
</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>login</servlet-name>
<url-pattern>/LoginServlet</url-pattern>
</servlet-mapping>
</web-app>
login.jsp文件
<%@ page contentType="text/html" pageEncoding="GBK"%>
<%@ page import="java.util.*"%>
<html>
<head><title></title></head>
<body>
<script language="javascript">
function validate(f){
if(!(/^\w{5,15}$/.test(f.userid.value))){
alert("用户ID必须是5~15位!") ;
f.userid.focus() ;
return false ;
}
if(!(/^\w{5,15}$/.test(f.userpass.value))){
alert("密码必须是5~15位!") ;
f.userpass.focus() ;
return false ;
}
}
</script>
<%
request.setCharacterEncoding("GBK") ;
%>
<%
List<String> info = (List<String>) request.getAttribute("info") ;
if(info != null){ // 有信息返回
Iterator<String> iter = info.iterator() ;
while(iter.hasNext()){
%>
<h4><%=iter.next()%></h4>
<%
}
}
%>
<form action="LoginServlet" method="post" onSubmit="return validate(this)">
用户ID:<input type="text"><br>
密 码:<input type="password"><br>
<input type="submit" value="登陆">
<input type="reset" value="重置">
</form>
</body>
</html>
数据库文件.sql