servlet实现登陆注册
拿到信息必须进行非空验证
用servlet做注册登陆时,在form表单的action中不用加后缀.java,jsp文件需要加
public boolean CheckParm(String...args){
for(String s:args){
if("".equals(s)||s==null){
return false;
}
}
return true;
}
当不确定传入的参数个数是可以用String...args,表示多参数传入
有返回值类型的方法一定加return
在servlet里面执行的代码写在doget方法中
request.setCharacterEncoding("utf-8");
response.setCharacterEncoding("utf-8");
response.setContentType("text/html; charset=utf-8");
防止输出出现乱码的代码表示
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!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>Insert title here</title>
</head>
<body>
<form action="TextServlet" method="post">
<input type="text" name="username" placeholder="请输入用户名"/><br>
<input type="text" name="password" placeholder="请输入密码"/><br>
<input type="text" name="password1" placeholder="请再次输入密码"/><br>
<input type="text" name="realname" placeholder="请输入真实名字"/><br>
<input type="submit" value="提交"/>
<a href="login.jsp"> 超级链接</a>
</form> </body>
</html>
注册的servlet类
package com.hanqi.textservlet; import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse; /**
* Servlet implementation class TextServlet
*/
@WebServlet("/TextServlet")
public class TextServlet extends HttpServlet {
private static final long serialVersionUID = 1L; /**
* @see HttpServlet#HttpServlet()
*/
public TextServlet() {
super();
// TODO Auto-generated constructor stub
} /**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
request.setCharacterEncoding("utf-8");
response.setCharacterEncoding("utf-8");
response.setContentType("text/html; charset=utf-8"); String username = request.getParameter("username");
String password = request.getParameter("password");
String password1 = request.getParameter("password1");
String realname = request.getParameter("realname"); response.getWriter().append("username:"+username+"password:"+password+"realname:"+realname); if(CheckParm(username,password,password1)){
if(password.equals(password1)){
Object obj=request.getServletContext().getAttribute(username);
if(obj==null){
request.getServletContext().setAttribute(username, username);
request.getServletContext().setAttribute(password, password);
response.sendRedirect("message.jsp?code=1");
}else{
response.sendRedirect("message.jsp?code=4");
}
}else{
response.sendRedirect("message.jsp?code=3");
}
}else{
response.sendRedirect("message.jsp?code=2");
} } /**
* @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);
}
public boolean CheckParm(String...args){
for(String s:args){
if("".equals(s)||s==null){
return false;
}
} return true; } }
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!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>Insert title here</title>
</head>
<body>
<form action="LoginServlet" method="post">
<input type="text" name="username" placeholder="请输入用户名"/><br>
<input type="text" name="password" placeholder="请输入密码"/><br>
<input type="submit" value="登陆"/>
</form> </body>
</html>
登陆的servlet类
package com.hanqi.textservlet; import java.io.IOException; import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse; /**
* Servlet implementation class LoginServlet
*/
@WebServlet("/LoginServlet")
public class LoginServlet extends HttpServlet {
private static final long serialVersionUID = 1L; /**
* @see HttpServlet#HttpServlet()
*/
public LoginServlet() {
super();
// TODO Auto-generated constructor stub
} /**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
request.setCharacterEncoding("utf-8");
response.setCharacterEncoding("utf-8");
response.setContentType("text/html; charset=utf-8"); String username=request.getParameter("username");
String password=request.getParameter("password"); ServletContext application=request.getServletContext();
Object obj=application.getAttribute(username);
Object ob=application.getAttribute(password);
if(obj!=null){
String user=(String)obj;
if(username.equals(user)){
if(password.equals(ob)){
response.sendRedirect("index.jsp");
}else{response.sendRedirect("message.jsp?code=7");}
}else{
response.sendRedirect("message.jsp?code=5");
}
}else{
response.sendRedirect("message.jsp?code=6");
}
}
/**
* @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);
} }
未按要求输入的提示页面的实现
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!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>Insert title here</title>
</head>
<body>
<%
String code=request.getParameter("code");
if("".equals(code)){
out.print("<h1>注册成功!</h1>");
}if("".equals(code)){
out.print("<h1>信息输入不完整!</h1>");
}if("".equals(code)){
out.print("<h1>输入的密码不一致!</h1>");
}if("".equals(code)){
out.print("<h1>用户已经存在!</h1>");
}if("".equals(code)){
out.print("<h1>用户名错误!</h1>");
}if("".equals(code)){
out.print("<h1>用户不存在!</h1>");
}if("".equals(code)){
out.print("<h1>密码错误!</h1>");
} %>
</body>
</html>
servlet实现登陆注册的更多相关文章
- Android通过Http连接MySQL 实现登陆/注册(数据库+服务器+客户端)
写在最前: 在实际开发中,相信每个项目都会有用户登陆注册功能,这个实现的方法很多,下面是我实现的方法,供大家交流. 新人发帖,万分紧张,怎么样才能装作一副经常发帖的样子不被别人看出来呢-,- ? 好了 ...
- java 24 - 11 GUI之制作登陆注册页面
简单说说,懒得发了... 步骤: A:首先写出登陆注册需要用到类以及代码(IO流) B:然后创建登陆窗口和注册窗口 C:各个监听事件: a:登录窗口 1.重置:把2个文本框的内容全部清空 2.注册:关 ...
- PHP数据库登陆注册简单做法
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...
- javaweb 登陆注册页面
视图的数据修改,表中也修改引用工具类用<%@ page import=""%> <%@ page import="java.util.Date" ...
- 用户登陆注册【JDBC版】
前言 在讲解Web开发模式的时候,曾经写过XML版的用户登陆注册案例!现在在原有的项目上,使用数据库版来完成用户的登陆注册!如果不了解的朋友,可以看看我Web开发模式的博文! 本来使用的是XML文件作 ...
- 用ajax的同步请求解决登陆注册需要根据服务器返回数据判断是否能提交的问题
最近在写www.doubilaile.com的登陆注册.需要用ajax请求服务器判断用户名是否存在,用户名和密码是否匹配,进而提交数据.碰到的问题是异步请求都能成功返回数据,但是该数据不能作为紧接着的 ...
- tkinter 创建登陆注册界面
import tkinter as tk from tkinter import messagebox #设置窗口居中 def window_info(): ws = window.winfo_scr ...
- 《java入门第一季》模拟用户登陆注册案例集合版
需求:校验用户名和密码,登陆成功后玩猜数字小游戏. 在这里先写集合版.后面还有IO版.数据库版. 一.猜数字小游戏类: 猜数字小游戏的代码见博客:http://blog.csdn.net/qq_320 ...
- IdentityServer4【Topic】之登陆注册
Sign-in 登陆注册 为了让标识服务器(identity server)代表用户发出令牌,该用户必须登录到标识服务器. Cookie authentication Cookie认证 身份验证是由来 ...
随机推荐
- maven Spring+Spring MVC+Mybatis+mysql轻量级Java web开发环境搭建
之前一直在做的一个GIS系统项目,采用了jsp+servlet框架,数据传输框架采用了apache的thrift框架,短时多传的风格还不错,但是较其他的java web项目显得有点太臃肿了,现在给大家 ...
- asp.net Mvc 动态创建Controller
有这么个需求,Urls如下: http://localhost:52804 http://localhost:52804/home/test http://localhost:52804/test1 ...
- 50行代码实现的高性能动画定时器 raf-interval
写在前面 raf-interval 是基于 window.requestAnimationFrame() 封装的定时器. Github: https://github.com/dntzhang/raf ...
- 20170709_python_学习记录
a='ABC';变量赋值时发生了什么 1.在内存中创建一个字符串'ABC' 2.在内存中创建一个变量a指向字符串'ABC' list [] 相当于数组 指向可以变动 str[1,2,3,4] str. ...
- linux执行sh报错:$’\r’: 未找到命令的解决
背景 执行.sh脚本时出现$'\r': 未找到命令, 原因 是因为命令直接从windows 复制过来导致的 解决 yum install dos2unix dos2unix **.sh 进行转换 再次 ...
- CAP 介绍及使用【视频】
前言 很多同学可能对于CAP这个项目想有更一步的了解,包括她的工作原理以及适用的场景,所以博主就准备了一场直播给他家讲解了一下,这个视频是直播的一个录像. 由于我这次直播本来是没有打算对外的,所以也是 ...
- (转)spring学习之@ModelAttribute运用详解
@ModelAttribute使用详解 1 @ModelAttribute注释方法 例子(1),(2),(3)类似,被@ModelAttribute注释的方法会在此controller每个方法执行前被 ...
- Spring源码情操陶冶-AbstractApplicationContext#finishRefresh
承接前文Spring源码情操陶冶-AbstractApplicationContext#finishBeanFactoryInitialization 约定web.xml配置的contextClass ...
- redis 订阅与发布
PUBLISH,SUBSCRIBE,等命令实现订阅与发布 订阅/发布到频道 订阅/发布到模式 频道的订阅与信息发送 订阅subscribe,可以让客户端订阅任意数量的频道, 每当有新信息发送到 ...
- javascript中this的用法
this是Javascript语言的一个关键字. 它代表函数运行时,自动生成的一个内部对象,只能在函数内部使用.比如, function test(){ this.x = 1; } 随着函数使用场合的 ...