Servlet 注册与提交作业

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Strict//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>登录页</title>
<script type="text/javascript">
function check()
{
    var uid = document.getElementById("userid");
    if (uid.value == "")
        {
        alert("用户代码不能为空");
        
        return false;
    
        }
    if(uform.password.value == "")
        {
         alert("密码不能为空");
        
        return false;
        }
    return true;
    }



</script>


</head>
<body>
<form id="uform"  action="yanzheng"  method ="post" onsubmit="return check()">

用户:<input id="userid" type="text" name="userid" width="30" />

密码:<input id="password" type="password" name="password" width="30"/>

<input type="submit" value="登录">

<a href="zhuce.html">注册新用户</a>





</form>
</body>
</html>
login.html
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Strict//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<script type="text/javascript">

//在本页面对注册信息进行验证,使之不能为空 
function check()
{
    var uid = document.getElementById("userid");
    if (uid.value == "")
        {
        alert("用户代码不能为空");
        
        return false;
    
        }
     if(uform.usermingc.value == "")
        {
         alert("用户名称不能为空");
        
        return false;
        }
    if(uform.password.value == "")
        {
         alert("密码不能为空");
        
        return false;
        }

    if(uform.quer.value != uform.password.value)
      {
       alert("登录密码和确认密码不一致");
    
       return false;
      }
   
    return true;
    }

</script>

</head>

<body>
<form id="uform" action ="saveUser" onsubmit="return check()" >

用户代码:<input id="userid" type="text" name="userid" width=30 >
<br><br>
用户名称:<input id="usermingc" type="text" name="username" width=30>
<br><br>
登录密码:<input id="password" type="password" name="password" width=30>
<br><br>
确认密码:<input id="quer" type="password" name="queren" width=30>
<br><br>
<input type="submit" value="提交">
</form>





</body>
</html>
zhuce.html
package com.hanqi;

import java.io.IOException;

import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.hanqi.*;

/**
 * Servlet implementation class saveUser
 */
public class saveUser extends HttpServlet {
    private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public saveUser() {
        super();
        // TODO Auto-generated constructor stub
    }

    /**
     * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
     */
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        
        response.setCharacterEncoding("GBK");
        
        String strUserid =  request.getParameter("userid");

        String strUsername =  request.getParameter("username");

        String strPW =  request.getParameter("password");

        
        if(strUserid == null ||strUserid.trim().length() == 0)
        {
            response.getWriter().append("账户不能为空");
            
            response.setHeader("refresh","3;URL=login.html");
        }
        
        else if(strUsername == null ||strUsername.trim().length() == 0)
        {
            response.getWriter().append("用户名不能为空");
            
            response.setHeader("refresh","3;URL=login.html");
        }
        
        else if(strPW == null ||strPW.trim().length() == 0)
        {
            response.getWriter().append("密码不能为空");
            
            response.setHeader("refresh","3;URL=login.html");
        }
        
        else{
            
            ServletContext application = this.getServletContext();

            Object obj = application.getAttribute(strUserid);
            
            if( obj != null)
             {
                response.getWriter().append("该用户已存在");
                
                response.setHeader("refresh","3;URL=login.html");
             }
            
            else
                {
                
                strUsername = new String(strUsername.getBytes("ISO-8859-1"),"UTF-8");
            
                User user = new User();
                
                user.setUserID(strUserid);
                
                user.setUserName(strUsername);
                
                user.setPassword(strPW);
                
                application.setAttribute(strUserid, user);
            
                response.getWriter().append("注册成功");
                
                response.setHeader("refresh","3;URL=login.html");

                }

        }
        
        response.getWriter().append("Served at: saveUser").append(request.getContextPath());
    }

    /**
     * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
     */
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // TODO Auto-generated method stub
        doGet(request, response);
    }

}
saveUser.java

 

package com.hanqi;

import java.io.IOException;

import javax.servlet.ServletConfig;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.hanqi.*;

/**
 * Servlet implementation class yanzheng
 */
public class yanzheng extends HttpServlet {
    private static final long serialVersionUID = 1L;
    String strType = "";

    /**
     * @see HttpServlet#HttpServlet()
     */
    public yanzheng() {
        super();
        // TODO Auto-generated constructor stub
    }

    /**
     * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
     *      response)
     */
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setCharacterEncoding("GBK");

        String strUserid = request.getParameter("userid");

        String strPW = request.getParameter("password");

        if (strUserid == null || strUserid.trim().length() == 0) {
            response.getWriter().append("账户不能为空");

            response.setHeader("refresh", "3;URL=login.html");
        }

        else if (strPW == null || strPW.trim().length() == 0) {
            response.getWriter().append("密码不能为空");

            response.setHeader("refresh", "3;URL=login.html");
        }

        else {
            ServletContext application = this.getServletContext();

            Object obj = application.getAttribute(strUserid);
            
            if (obj != null) 
            {
                User user = (User) obj;

                String strUID = user.getUserID();

                String strUsername = user.getUserName();

                String strP = user.getPassword();

                if (strUserid.equals(strUID)) 
                {
                    if (strPW.equals(strP)) 
                    {
                        response.getWriter().append("欢迎" + strUsername + "登陆系统");
                    } else 
                    {
                        response.getWriter().append("密码输入错误");

                        response.setHeader("refresh", "3;URL=login.html");
                    }
                } 

            }
            else 
            {
                response.getWriter().append("该用户不存在");

                response.setHeader("refresh", "3;URL=login.html");
            }

        }

        // response.getWriter().append("Served at:
        // yanzheng").append(request.getContextPath());
    }

    /**
     * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
     *      response)
     */
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        // TODO Auto-generated method stub
        doGet(request, response);
    }

}
yanzheng.java
package com.hanqi;

//关于用户的实体类
public class User {

    //用户代码
    private String UserID;
    
    public String getUserID()
    {
        return UserID;
    }
    
    public void setUserID(String userID)
    {
        this.UserID = userID;
    }
    
    //用户名称
    private String UserName;
    
    public String getUserName() {
        return UserName;
    }

    public void setUserName(String userName) {
        UserName = userName;
    }
    
    //密码
    private String Password;

    public String getPassword() {
        return Password;
    }

    public void setPassword(String password) {
        Password = password;
    }
}
User.java

 

posted @ 2015-12-03 08:43  北辰孤星  阅读(195)  评论(0编辑  收藏  举报