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

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 javax.servlet.http.HttpSession;
import com.hanqi.*; /**
* Servlet implementation class CheckLogin
*/
public class CheckLogin extends HttpServlet {
private static final long serialVersionUID = 1L; /**
* @see HttpServlet#HttpServlet()
*/
public CheckLogin() {
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=index.html");
}
else if(strPW == null || strPW.trim().length() == 0)
{
response.getWriter().append("密码不能为空");
response.setHeader("refresh", "3,URL=index.html");
}
else
{ ServletContext application = this.getServletContext(); if(application != null){
Object obj = application.getAttribute(strUserID); if(obj != null)
{
user zc = (user)obj; String strusername = zc.getuserName();
String mima = zc.getpassword(); if(strPW.equals(mima))
{
response.getWriter().append(strusername + "登录成功");
}
else
{
response.getWriter().append("密码有误");
}
}
else
{
response.getWriter().append("用户不存在,请注册");
}
} }
} /**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// 请求跳转
doGet(request, response);
} }

Servlet作业--实现注册和登录的更多相关文章

  1. SpringBoot整合Mybatis完整详细版二:注册、登录、拦截器配置

    接着上个章节来,上章节搭建好框架,并且测试也在页面取到数据.接下来实现web端,实现前后端交互,在前台进行注册登录以及后端拦截器配置.实现简单的未登录拦截跳转到登录页面 上一节传送门:SpringBo ...

  2. DBCP(MySql)+Servlet+BootStrap+Ajax实现用户登录与简单用户管理系统

    目  录   简介 本次项目通过Maven编写 本文最后会附上代码 界面截图 登录界面 注册界面 登录成功进入主页 增加用户操作 删除用户操作 修改用户操作 主要代码 Dao层代码 DBCP代码 Se ...

  3. Vue.js——使用$.ajax和vue-resource实现OAuth的注册、登录、注销和API调用

    概述 上一篇我们介绍了如何使用vue resource处理HTTP请求,结合服务端的REST API,就能够很容易地构建一个增删查改应用.这个应用始终遗留了一个问题,Web App在访问REST AP ...

  4. 1.注册或登录页面设计:UILabel,UIButton,UITextField

    学习iOS开发已经有一段时日了,之前一直没有系统的对iOS开发的相关知识进行归纳总结,导致很多知识点云里雾里在脑子里形不成iOS开发的思想,现将自己在学习过程中遇到的一些知识进行总结,希望能对iOS初 ...

  5. PC 端微信扫码注册和登录

    一.前言 先声明一下,本文所注重点为实现思路,代码及数据库设计主要为了展现思路,如果对代码效率有着苛刻要求的项目切勿照搬. 相信做过微信开发的人授权这块都没少做过,但是一般来说我们更多的是为移动端的网 ...

  6. bmob云 实现注册和登录的功能

    向大家介绍一款我感觉非常溜的一款后端云服务bmob云 借助bmob云我们可以实现注册和登录页面的功能,下面就让我给大家演示一下借助bmob云服务实现这两个功能吧.  1. 用户是一个应用程序的核心.对 ...

  7. 10天学会phpWeChat——第十天:phpWeChat的会员注册、登录以及微信网页开发

    通过前面的系列教程,我们系统的讲解了phpWeChat从视图端.控制器端到模型端的操作流程:熟悉了phpWeChat的目录结构:掌握了视图端模板如何创建一个丰富的表单和模型端如何操作数据库.这一切都是 ...

  8. 【京东个人中心】——Nodejs/Ajax/HTML5/Mysql爬坑之注册与登录监听

    一.引言 在数据库和静态页面都创建好之后,下面就该接着完成后台Node.js监听注册和登录的部分了.这个部分主要使用的技术是:Node.js的Express框架和ajax异步请求.登录和注册的代码实现 ...

  9. ASP.NET Core Identity Hands On(2)——注册、登录、Claim

    上一篇文章(ASP.NET Core Identity Hands On(1)--Identity 初次体验)中,我们初识了Identity,并且详细分析了AspNetUsers用户存储表,这篇我们将 ...

随机推荐

  1. jQuery动态加载脚本 $.getScript();

    jQuery.getScript("/path/to/myscript.js", function(data, status, jqxhr) {       /*          ...

  2. [js]事件综合 整理

    原文链接:http://www.cnblogs.com/xxcanghai/p/5205998.html 事件流:页面接收事件的顺序,IE提出了事件冒泡流,Netscape提出了事件捕获流. 事件冒泡 ...

  3. 下载Android 5.0源码(附源码)

    下载方法见我的另一篇博客:http://blog.csdn.net/maosidiaoxian/article/details/41680769 2014-12-24更新:5.0.1源码已上传. 这次 ...

  4. D3.js 布局

    布局,可以理解成 “制作常见图形的函数”,有了它制作各种相对复杂的图表就方便多了. 一.布局是什么 布局,英文是 Layout.从字面看,可以想到有“决定什么元素绘制在哪里”的意思.布局是 D3 中一 ...

  5. HBase启动和停止命令

    启动HBase集群: bin/start-hbase.sh 单独启动一个HMaster进程: bin/hbase-daemon.sh start master 单独停止一个HMaster进程: bin ...

  6. smarty 快速上手

    smarty半小时快速上手入门教程 投稿:shichen2014 字体:[增加 减小] 类型:转载 时间:2014-10-27我要评论 这篇文章主要介绍了smarty半小时快速上手入门教程,以实例的形 ...

  7. Mysql有两种存储引擎:InnoDB与Myisam

    http://www.cnblogs.com/kevingrace/p/5685355.html

  8. AngularJS的directive(指令)配置选项说明

    js代码如下: var appModule = angular.module("appModule", []); appModule.controller("Ctrl&q ...

  9. java 内部类2(成员内部类)

    成员内部类: 特点:在其所在的外部类,的成员函数中,的类. 难点:看注释(涉及到jvm) /*test()执行完毕时,x2从内存中消失,inner的声明周,比x2长,inner还在访问,给人的感觉好像 ...

  10. Working with Data » Getting started with ASP.NET Core and Entity Framework Core using Visual Studio » 创建复杂数据模型

    Creating a complex data model 创建复杂数据模型 8 of 9 people found this helpful The Contoso University sampl ...