JavaWeb Servlet开发注册页面实例


Posted in Java/Android onApril 11, 2022

Servlet-注册页面

环境准备:

本文所用到环境如下:

软件:Eclipse(2018)

服务器:Tomcat 9

JavaWeb Servlet开发注册页面实例

JavaWeb Servlet开发注册页面实例

JavaWeb Servlet开发注册页面实例

JavaWeb Servlet开发注册页面实例

JavaWeb Servlet开发注册页面实例

JavaWeb Servlet开发注册页面实例

JavaWeb Servlet开发注册页面实例

JavaWeb Servlet开发注册页面实例

JavaWeb Servlet开发注册页面实例

JavaWeb Servlet开发注册页面实例

在index.jsp添加相关的代码

<%@ 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>

<h1>用户注册</h1>
<div>
    <form action="RegisterServlet" method="get">
    <table>
    <tr>
    <td>登录名:</td> 
    <td><input type="text" name="username" required> (可包含a-z、0-9和下划线)</td>
    </br>
    <tr>
    <td>密码:</td> 
    <td><input type="password" name="password" required> (至少包含6个字符)</td>
    </tr>
    <tr>
    <td>再次输入密码:</td> 
    <td><input type="password" name="confirmPassword" required></td>
    </tr>
    <tr>
    <td>电子邮箱:</td> 
    <td><input type="text" id="email" name="youxaing" required> (必须包含@字符)</td>
    </tr>
    <tr>
    <td>性别:</td> 
    <td><input type="radio" name="gender" value="男" required>男
        <input type="radio" name="gender" value="女" required>女</td>
    </tr>
     <td>头像:</td> 
    <td><input type="file" name="bfile" value="选择文件" accept="image/*" ></td>
    </tr>
     <td>爱好:</td> 
    <td>
    <p>
   	  <input type="checkbox" name="hobby" value="运动"> 运动
      <input type="checkbox" name="hobby" value="聊天" > 聊天
      <input type="checkbox" name="hobby" value="玩游戏" > 玩游戏</p>
      
    </td>
    </tr>
   
     </tr>
     <td>喜欢的城市:</td> 
    <td>
         <select name="selectList">
         <option>[请选择]</option>
         <option>北海</option>
         <option>海南</option>
         <option>重庆</option>
         <option>杭州</option>
         <option>深圳</option>
         <option>成都</option>
     	 </select>
    </tr>    
    </table>
    <div>
    <p>
       <input type="submit" value="提交">
       <input type="reset" value="重置">
     </p>
     </div>
    </form>
</div>

</body>
</html>

效果为:

JavaWeb Servlet开发注册页面实例

JavaWeb Servlet开发注册页面实例

JavaWeb Servlet开发注册页面实例

JavaWeb Servlet开发注册页面实例

JavaWeb Servlet开发注册页面实例

JavaWeb Servlet开发注册页面实例

Login_Servlet.java代码

package onlyxiu_ceshi.com;

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;
import javax.servlet.http.HttpSession;

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

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
//		response.getWriter().append("Served at: ").append(request.getContextPath());
		
		
		 request.setCharacterEncoding("UTF-8");//处理乱码问题
	        response.setCharacterEncoding("UTF-8");//处理乱码问题
	        //获得用户在register界面提交的数据
	        String username = request.getParameter("username");
	        String password = request.getParameter("password");
	        String confirmPassword = request.getParameter("confirmPassword");
	        String gender = request.getParameter("gender");
	        String youxaing = request.getParameter("youxaing");
	        String bfile = request.getParameter("bfile");
	        String hobby  = request.getParameter("hobby");
	        String selectList = request.getParameter("selectList");
	        //判断密码是否正确
	        if (password.equals(confirmPassword)){//如果密码相同,重定向到成功界面
	            HttpSession session = request.getSession();//获取session
	            session.setAttribute("session_username",username);
	            session.setAttribute("session_password",password);
	            session.setAttribute("session_gender",gender);
	            session.setAttribute("session_youxaing",youxaing);
	            session.setAttribute("session_bfile",bfile);
	            session.setAttribute("session_hobby", hobby);
	            session.setAttribute("session_selectList",selectList);
	           
	            request.getRequestDispatcher("SuccessServlet").forward(request, response);
	          //  response.sendRedirect("register_login.jsp");//重定向到成功页面
	        }else {//如果密码不同,通知用户密码输入不一样
	            request.getSession().setAttribute("passwordError","yes");
	            request.getRequestDispatcher("ErrorServlet").forward(request, response);
//	            response.sendRedirect("register.jsp");//重定向到登陆界面
	        }
	    }
	

	/**
	 * @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);
	}

}

配置web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" id="WebApp_ID" version="3.1">
  <display-name>onlyxiu_ceshi</display-name>
  <welcome-file-list>
    <welcome-file>index.html</welcome-file>
    <welcome-file>index.htm</welcome-file>
    <welcome-file>index.jsp</welcome-file>
    <welcome-file>default.html</welcome-file>
    <welcome-file>default.htm</welcome-file>
    <welcome-file>default.jsp</welcome-file>
  </welcome-file-list>
  
  <servlet>
    <servlet-name>Login_Servlet</servlet-name>
    <servlet-class>onlyxiu_ceshi.com</servlet-class>
  </servlet>
  <servlet-mapping>
    <servlet-name>Login_Servlet</servlet-name>
    <url-pattern>/onlyxiu_ceshi/Login_Servlet</url-pattern>
  </servlet-mapping>
  
</web-app>

JavaWeb Servlet开发注册页面实例

ErrorServlet.java的代码

package onlyxiu_ceshi.com;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.Arrays;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

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

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
//		response.getWriter().append("Served at: ").append(request.getContextPath());
		
		response.setHeader("Content-type","text/html; charset=UTF-8");
		String username = request.getParameter("username");
		String password = request.getParameter("password");
		String gender = request.getParameter("gender");
		String youxaing = request.getParameter("youxaing");
		String bfile = request.getParameter("bfile");
		String hobby = request.getParameter("hobby");
		String selectList = request.getParameter("selectList");
		PrintWriter out = response.getWriter();
		out.print("<h1>用户注册信息</h1>");
		out.print("用户名:\n"+username+"<br>");
		out.print("密码:"+password+"<br>");
		out.print("电子邮箱:"+youxaing+"<br>");
		out.print("性别:"+gender+"<br>");
		out.print("头像:"+bfile+"<br>");
		out.print("爱好:"+hobby+"<br>");
		out.print("喜欢的城市:"+selectList+"<br>");
//		out.print("登陆失败");
		out.close();
	}

	/**
	 * @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);
	}

}

SuccessServlet.java

package onlyxiu_ceshi.com;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

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

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
//		response.getWriter().append("Served at: ").append(request.getContextPath());
		response.setHeader("Content-type","text/html; charset=UTF-8");
		String username = request.getParameter("username");
		String password = request.getParameter("password");
		String gender = request.getParameter("gender");
		String youxaing = request.getParameter("youxaing");
		String bfile = request.getParameter("bfile");
		String hobby = request.getParameter("hobby");
		String selectList = request.getParameter("selectList");
		PrintWriter out = response.getWriter();
		out.print("<h1>用户注册信息</h1>");
		out.print("用户名:\n"+username+"<br>");
		out.print("密码:"+password+"<br>");
		out.print("电子邮箱:"+youxaing+"<br>");
		out.print("性别:"+gender+"<br>");
		out.print("头像:"+bfile+"<br>");
		out.print("爱好:"+hobby+"<br>");
		out.print("喜欢的城市:"+selectList+"<br>");
//		out.println("登陆成功");
		out.close();
	}

	/**
	 * @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);
	}

}

JavaWeb Servlet开发注册页面实例

JavaWeb Servlet开发注册页面实例

Java/Android 相关文章推荐
SpringCloud Alibaba项目实战之nacos-server服务搭建过程
Jun 21 Java/Android
Netty结合Protobuf进行编解码的方法
Jun 26 Java/Android
Java基础-封装和继承
Jul 02 Java/Android
swagger如何返回map字段注释
Jul 03 Java/Android
springboot集成springCloud中gateway时启动报错的解决
Jul 16 Java/Android
java设计模式--七大原则详解
Jul 21 Java/Android
Java如何实现通过键盘输入一个数组
Feb 15 Java/Android
Android自定义ScrollView实现阻尼回弹
Apr 01 Java/Android
Java存储没有重复元素的数组
Apr 29 Java/Android
AndroidStudio图片压缩工具ImgCompressPlugin使用实例
Aug 05 Java/Android
基于Android10渲染Surface的创建过程
Aug 14 Java/Android
HttpClient实现文件上传功能
Aug 14 Java/Android
Java中的继承、多态以及封装
JAVA长虹键法之建造者Builder模式实现
Apr 10 #Java/Android
SpringCloud项目如何解决log4j2漏洞
Apr 10 #Java/Android
零基础学java之循环语句的使用
Apr 10 #Java/Android
零基础学java之带参数以及返回值的方法
Apr 10 #Java/Android
零基础学java之带返回值的方法的定义和调用
Apr 10 #Java/Android
零基础学java之方法的定义与调用详解
Apr 10 #Java/Android
You might like
虚拟主机中对PHP的特殊设置
2006/10/09 PHP
PHP MySQL应用中使用XOR运算加密算法分享
2011/08/28 PHP
zf框架的Filter过滤器使用示例
2014/03/13 PHP
封装ThinkPHP的一个文件上传方法实例
2014/10/31 PHP
PHP计算日期相差天数实例分析
2016/02/23 PHP
Thinkphp5.0 框架使用模型Model添加、更新、删除数据操作详解
2019/10/11 PHP
jQuery 顺便学习下CSS选择器 奇偶匹配nth-child(even)
2010/05/24 Javascript
this和执行上下文实现代码
2010/07/01 Javascript
js获取时间(本周、本季度、本月..)
2013/11/22 Javascript
查找Oracle高消耗语句的方法
2014/03/22 Javascript
jquery实现页面关键词高亮显示的方法
2015/03/12 Javascript
简单的分页代码js实现
2016/05/17 Javascript
微信小程序 Nginx环境配置详细介绍
2017/02/14 Javascript
jQuery在header中设置请求信息的方法
2017/03/06 Javascript
用Webpack构建Vue项目的实践
2017/11/07 Javascript
vue-cli脚手架打包静态资源请求出错的原因与解决
2019/06/06 Javascript
JavaScript 类的封装操作示例详解
2020/05/16 Javascript
Python设计模式之代理模式实例
2014/04/26 Python
Django 中间键和上下文处理器的使用
2019/03/17 Python
Python PO设计模式的具体使用
2019/08/16 Python
PyTorch预训练的实现
2019/09/18 Python
keras 获取某层的输入/输出 tensor 尺寸操作
2020/06/10 Python
python如何设置静态变量
2020/09/07 Python
css3实例教程 一款纯css3实现的发光屏幕旋转特效
2014/12/07 HTML / CSS
尤妮佳moony海外旗舰店:日本殿堂级纸尿裤品牌
2018/02/23 全球购物
财务管理个人自荐书范文
2013/11/24 职场文书
中学教师培训制度
2014/01/31 职场文书
政府领导干部个人对照检查材料思想汇报
2014/09/24 职场文书
2015双创工作总结
2015/07/24 职场文书
公司员工宿舍管理制度
2015/08/07 职场文书
思品教学工作总结
2015/08/10 职场文书
介绍信应该怎么开?
2019/04/03 职场文书
Mysql - 常用函数 每天积极向上
2021/04/05 MySQL
Netty分布式客户端处理接入事件handle源码解析
2022/03/25 Java/Android
python神经网络学习 使用Keras进行简单分类
2022/05/04 Python
MySql统计函数COUNT的具体使用详解
2022/08/14 MySQL