文章详情

短信预约-IT技能 免费直播动态提醒

请输入下面的图形验证码

提交验证

短信预约提醒成功

JavaWeb之Servlet注册页面的实现示例

2024-04-02 19:55

关注

Servlet-注册页面

环境准备:

本文所用到环境如下:

软件:Eclipse(2018)

服务器:Tomcat 9

image-20220410201745892

image-20220410202055002

image-20220410202849114

image-20220410202352747

image-20220410203937149

image-20220410203345395

image-20220410203358312

image-20220410204121620

image-20220410204152464

image-20220410204213630

在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
@WebServlet("/Login_Servlet")
public class Login_Servlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    
    public Login_Servlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	
	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");//重定向到登陆界面
	        }
	    }
	

	
	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>

image-20220410205637467

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;


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

	
	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();
	}

	
	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;


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

	
	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();
	}

	
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
//		doGet(request, response);
	}

}

image-20220410210256565

image-20220410210243740

 到此这篇关于JavaWeb之Servlet注册页面的实现示例的文章就介绍到这了,更多相关Servlet注册页面内容请搜索编程网以前的文章或继续浏览下面的相关文章希望大家以后多多支持编程网!

阅读原文内容投诉

免责声明:

① 本站未注明“稿件来源”的信息均来自网络整理。其文字、图片和音视频稿件的所属权归原作者所有。本站收集整理出于非商业性的教育和科研之目的,并不意味着本站赞同其观点或证实其内容的真实性。仅作为临时的测试数据,供内部测试之用。本站并未授权任何人以任何方式主动获取本站任何信息。

② 本站未注明“稿件来源”的临时测试数据将在测试完成后最终做删除处理。有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341

软考中级精品资料免费领

  • 历年真题答案解析
  • 备考技巧名师总结
  • 高频考点精准押题
  • 2024年上半年信息系统项目管理师第二批次真题及答案解析(完整版)

    难度     813人已做
    查看
  • 【考后总结】2024年5月26日信息系统项目管理师第2批次考情分析

    难度     354人已做
    查看
  • 【考后总结】2024年5月25日信息系统项目管理师第1批次考情分析

    难度     318人已做
    查看
  • 2024年上半年软考高项第一、二批次真题考点汇总(完整版)

    难度     435人已做
    查看
  • 2024年上半年系统架构设计师考试综合知识真题

    难度     224人已做
    查看

相关文章

发现更多好内容

猜你喜欢

AI推送时光机
位置:首页-资讯-后端开发
咦!没有更多了?去看看其它编程学习网 内容吧
首页课程
资料下载
问答资讯