环境准备:
本文所用到环境如下:
软件:Eclipse(2018)
服务器:Tomcat 9
在index.jsp添加相关的代码
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
Insert title here
用户注册
效果为:
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
<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_ceshidisplay-name>
<welcome-file-list>
<welcome-file>index.htmlwelcome-file>
<welcome-file>index.htmwelcome-file>
<welcome-file>index.jspwelcome-file>
<welcome-file>default.htmlwelcome-file>
<welcome-file>default.htmwelcome-file>
<welcome-file>default.jspwelcome-file>
welcome-file-list>
<servlet>
<servlet-name>Login_Servletservlet-name>
<servlet-class>onlyxiu_ceshi.comservlet-class>
servlet>
<servlet-mapping>
<servlet-name>Login_Servletservlet-name>
<url-pattern>/onlyxiu_ceshi/Login_Servleturl-pattern>// /项目名/类名
servlet-mapping>
web-app>
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("用户注册信息
");
out.print("用户名:\n"+username+"
");
out.print("密码:"+password+"
");
out.print("电子邮箱:"+youxaing+"
");
out.print("性别:"+gender+"
");
out.print("头像:"+bfile+"
");
out.print("爱好:"+hobby+"
");
out.print("喜欢的城市:"+selectList+"
");
// 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("用户注册信息
");
out.print("用户名:\n"+username+"
");
out.print("密码:"+password+"
");
out.print("电子邮箱:"+youxaing+"
");
out.print("性别:"+gender+"
");
out.print("头像:"+bfile+"
");
out.print("爱好:"+hobby+"
");
out.print("喜欢的城市:"+selectList+"
");
// 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);
}
}