日期:2014-05-19 浏览次数:20667 次
<%@ page contentType="text/html" pageEncoding="GBK"%> <%@ page import="java.util.*"%> <html> <head><title>www.mldnjava.cn,MLDN高端Java培训</title></head> <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> <body> <h2>用户登录程序</h2> <% 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" name="userid"><br> 密 码:<input type="password" name="userpass"><br> <input type="submit" value="登陆"> <input type="reset" value="重置"> </form> </body> </html>
package org.lxh.mvcdemo.servlet ; import java.io.* ; import java.util.* ; import javax.servlet.* ; import javax.servlet.http.* ; import org.lxh.mvcdemo.factory.* ; import org.lxh.mvcdemo.vo.* ; public class LoginServlet extends HttpServlet { /** * */ public void doGet(HttpServletRequest req,HttpServletResponse resp) throws ServletException,IOException{ String path = "login.jsp" ; String userid = req.getParameter("userid") ; String userpass = req.getParameter("userpass") ; List<String> info = new ArrayList<String>() ; // 收集错误 if(userid==null || "".equals(userid)){ info.add("用户id不能为空!") ; } 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{ // PrintWriter out=resp.getWriter(); // out.println("hello shanghai ."); this.doGet(req,resp) ; } }