码迷,mamicode.com
首页 > 其他好文 > 详细

Servlet作业--实现注册和登录

时间:2015-12-03 02:16:15      阅读:207      评论:0      收藏:0      [点我收藏+]

标签:

1.注册页面  zhuce.html

<!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>注册新用户</title>
<script type="text/javascript">

function check()
{    
    if(zhuce.userid.value == null || zhuce.userid.value.trim().length ==0)
        {
        alert("请输入用户代码")
        return false;
        }
    if(zhuce.username.value == null || zhuce.username.value.trim().length ==0)
        {
        alert("请输入用户名称");
        return false;
        }
    if(zhuce.password.value == null || zhuce.password.value.trim().length ==0)
        {
        alert("请输入密码");
        return false;
        }
    if(zhuce.password2.value == null || zhuce.password2.value.trim().length ==0)
        {
        alert("请再次密码");
        return false;
        }
    if(zhuce.password2.value != zhuce.password.value)
        {
        alert("两次输入的密码不一致");
        return false;
        }
    return true;
}


</script>
</head>
<body>
<form id="zhuce" action="zhuce" onSubmit="return check()" >

用户代码:<input id="userid" type="text" name="userid" width=30 />
<br><br>
用户名称:<input id="username" type="text" name="username" width=30 />
<br><br>
输入密码:<input id="password" type="password" name="password" width=30 />
<br><br>
确认密码:<input id="password2" type="password" name="password2" width=30 />
<br><br>
<input type="submit" value="提交" />

</form>

</body>
</html>

2.注册处理  zhuce.java

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 org.apache.catalina.User;
import com.hanqi.*;

/**
 * Servlet implementation class zhuce
 */
@SuppressWarnings("unused")
public class zhuce extends HttpServlet {
    private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public zhuce() {
        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 un = new String(request.getParameter("username").getBytes("ISO-8859-1"),"UTF-8"); String ui = request.getParameter("userid"); String pw = request.getParameter("password"); String pw2 = request.getParameter("password2"); if(ui == null || ui.trim().length() == 0) { response.getWriter().append("用户代码不能为空");
       //自动跳转到注册页面 response.setHeader(
"refresh", "3;URL=zhuce.html"); } else if(un == null || un.trim().length() == 0) { response.getWriter().append("用户名不能为空"); response.setHeader("refresh", "3;URL=zhuce.html"); } else if(pw == null || pw.trim().length() == 0) { response.getWriter().append("密码不能为空"); response.setHeader("refresh", "3;URL=zhuce.html"); } else { ServletContext application = this.getServletContext(); Object obj = application.getAttribute(ui); if(obj != null) { response.getWriter().append("用户代码已存在"); response.setHeader("refresh", "3;URL=zhuce.html"); } else { //以对象的形式保存用户信息 //实体类,对用户信息的封装 user zc = new user(); zc.setuserID(ui); zc.setuserName(un); zc.setpassword(pw); response.getWriter().append("注册成功"); response.setHeader("refresh", "3;URL=index.html"); } } } /** * @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); } }

3.自定义存储用户注册信息的类: user.java

package com.hanqi;

public class user {

  //用户代码
private String ui; public String getuserID() { return ui; } public void setuserID(String ui) { this.ui = ui; } //用户名称 private String un; public String getuserName() { return un; } public void setuserName(String un) { this.un = un; } //密码 private String pw; public String getpassword() { return pw; } public void setpassword(String pw) { this.pw = pw; } }

4.登录页面 index.html

<!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>请登录</title>
<script type="text/javascript">

function check()
{
    var uid = document.getElementById("userid");
    
    if(uid.value == "")
        {
        alert("用户代码不能为空")
        return false;
        }
    if(login.password.value == "")
        {
        alert("密码不能为空");
        return false;
        }
    return true;
}


</script>

</head>
<body>

<form id="login" method="post" action="CheckLogin" onSubmit="return check();">
用户代码:
<input id="userid" name="userid" type="text" width="30" />
<a href="P142-3.1.jsp">注册新用户</a>
<br>
登录密码:
<input id="password" name="password" type="password" width="30" />
<br>
<input type="submit" value="登录"/>


</form>

</body>
</html>

5.登录处理页面  CheckLogin.java

Servlet作业--实现注册和登录

标签:

原文地址:http://www.cnblogs.com/shadowduke/p/5014897.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!