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. HDU3247 AC自动机+dp

    题意:给出n个资源,m个病毒,将资源串拼接成一个串,必须包含所有的资源串,可以重叠,但是不能包含病毒,问最小的长度为多少 题解:所有串建AC自动机.对以资源串结尾的结点跑bfs,求出到其他资源串结尾的 ...

  2. Android listview和ListAdapter搭配使用

    ListView时Android中自带的数据显示控件,要使用ListView填充数据,必须要通过适配器来填充,这里给大家介绍一下ListAdapter适配器,效果图如下: java源码: packag ...

  3. 【Todo】Nginx架构学习

    要进行Web服务,绕不开的就是Nginx.这已经是大型网站的标配.对Nginx进行一定程度的深入学习. http://www.ituring.com.cn/article/4436 http://bl ...

  4. Android照相机模块编程 照片颠倒问题及查询摄像头参数问题的解决

    这两天编程弄Android照相机模块,设置好各种参数后,发现预览的时候,照片是颠倒了,不是上下颠倒而是颠倒90°. 我的手机是华为U9200,用的Android4.0.3,后来看到http://www ...

  5. poj 1144 Network(无向图求割顶数)

    题目链接:poj 1144 题意就是说有 n(标号为 1 ~ n)个网点连接成的一个网络,critical places 表示删去后使得图不连通的顶点,也就是割顶,求图中割顶的个数. 直接上大白书上的 ...

  6. jdk1.6安装

    下载jdk1.6 进入oracle官网,依次选择 Downloads-->Java SE-->拉倒网页最底部,点击Previous Releases - Java Archive后的DOE ...

  7. 【转】 C++的深拷贝与浅拷贝

    对于普通类型的对象来说,它们之间的复制是很简单的,例如:int a=88;int b=a; 而类对象与普通对象不同,类对象内部结构一般较为复杂,存在各种成员变量.下面看一个类对象拷贝的简单例子.   ...

  8. stream流批量读取并合并文件

    import java.io.BufferedReader; import java.io.File; import java.io.FileInputStream; import java.io.F ...

  9. (29)odoo的可用小图标

    * 系统的小图标都采用了 fontawesome    官网是 http://fontawesome.dashgame.com/    * 运用小图标    # 首先打开官网 http://fonta ...

  10. Spring Data Jpa配置

    Spring Data JPA提供的接口,也是Spring Data JPA的核心概念: 1:Repository:最顶层的接口,是一个空的接口,目的是为了统一所有Repository的类型,且能让组 ...