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 相关文章推荐
深入理解以DEBUG方式线程的底层运行原理
Jun 21 Java/Android
Java并发编程之Executor接口的使用
Jun 21 Java/Android
Java日常练习题,每天进步一点点(38)
Jul 26 Java/Android
Java设计模式之享元模式示例详解
Mar 03 Java/Android
你知道Java Spring的两种事务吗
Mar 16 Java/Android
Android超详细讲解组件ScrollView的使用
Mar 31 Java/Android
Java 多态分析
Apr 26 Java/Android
mybatis 获取更新记录的id
May 20 Java/Android
springboot读取nacos配置文件
May 20 Java/Android
Java实现添加条码或二维码到Word文档
Jun 01 Java/Android
openGauss数据库JDBC环境连接配置的详细过程(Eclipse)
Jun 01 Java/Android
Java实现贪吃蛇游戏的示例代码
Sep 23 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实现图片等比例放大和缩小的方法详解
2013/06/06 PHP
基于empty函数的输出详解
2013/06/17 PHP
CL vs ForZe BO5 第五场 2.13
2021/03/10 DOTA
Nigma vs Alliance BO5 第二场2.14
2021/03/10 DOTA
javascript之卸载鼠标事件的代码
2007/05/14 Javascript
javascript实现的在当前窗口中漂浮框的代码
2010/03/15 Javascript
JS中typeof与instanceof之间的区别总结
2013/11/14 Javascript
js对象继承之原型链继承实例
2015/01/10 Javascript
Javascript核心读书有感之表达式和运算符
2015/02/11 Javascript
js实现div拖动动画运行轨迹效果代码分享
2015/08/27 Javascript
把多个JavaScript函数绑定到onload事件处理函数上的方法
2016/09/04 Javascript
xcode中获取js文件的路径方法(推荐)
2016/11/05 Javascript
angular中使用Socket.io实例代码
2017/06/03 Javascript
Vue使用高德地图搭建实时公交应用功能(地图 + 附近站点+线路详情 + 输入提示+换乘详情)
2018/05/16 Javascript
vue指令只能输入正数并且只能输入一个小数点的方法
2018/06/08 Javascript
详解使用VueJS开发项目中的兼容问题
2018/08/02 Javascript
详解vue指令与$nextTick 操作DOM的不同之处
2018/08/02 Javascript
angular2组件中定时刷新并清除定时器的实例讲解
2018/08/31 Javascript
基于mpvue小程序使用echarts画折线图的方法示例
2019/04/24 Javascript
javaScript 实现重复输出给定的字符串的常用方法小结
2020/02/20 Javascript
JavaScript 引用类型实例详解【数组、对象、严格模式等】
2020/05/13 Javascript
vue实现移动端触屏拖拽功能
2020/08/21 Javascript
JS算法教程之字符串去重与字符串反转
2020/12/15 Javascript
[03:20]次级联赛厮杀超职业 现超级兵对拆世纪大战
2014/10/30 DOTA
[50:11]2018DOTA2亚洲邀请赛 4.7总决赛 LGD vs Mineski 第三场
2018/04/09 DOTA
Django代码性能优化与Pycharm Profile使用详解
2018/08/26 Python
Django model update的多种用法介绍
2020/03/28 Python
canvas实现俄罗斯方块的方法示例
2018/12/13 HTML / CSS
html5录音功能实战示例
2019/03/25 HTML / CSS
canvas实现手机的手势解锁的步骤详细
2020/03/16 HTML / CSS
得到Class的三个过程是什么
2012/08/10 面试题
PPP协议组成及简述协议协商的基本过程
2015/05/28 面试题
商务会议邀请函
2014/01/09 职场文书
党性教育心得体会
2014/09/03 职场文书
团组织推优材料
2014/12/29 职场文书
Win11任务栏太宽了怎么办?一招解决Win11任务栏太宽问题
2021/11/21 数码科技