8. 编写RegisterServlet代码
package com.java.servlet; import com.java.dao.UserDao; import com.java.dao.impl.UserDaoImpl; import com.java.domain.User; 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 java.io.IOException; /** * @Author shaonaiyi@163.com * @Date 2022/3/6 16:38 * @Description 注册servlet */ @WebServlet(name = "/register") public class RegisterServlet extends HttpServlet { @Override protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { doPut(req, resp); } @Override protected void doPut(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { req.setCharacterEncoding("UTF-8"); resp.setContentType("text/html;charset=UTF-8"); // id不需要传进来,角色默认注册是普通用户,无法注册管理员 String username = req.getParameter("username"); String password = req.getParameter("password"); User user = new User(); user.setUsername(username); user.setPassword(password); UserDao userDao = new UserDaoImpl(); if (userDao.register(user)) { System.out.println("注册成功"); // 注册成功,跳转登录页面 req.getRequestDispatcher("register-success.jsp").forward(req,resp); } else { System.out.println("注册失败"); req.setAttribute("message", "注册失败"); req.getRequestDispatcher("register.jsp").forward(req,resp); } } }
至此,后端代码已经编写好了,还需要完成前端页面代码。
0x04 编写前端代码
1. 编写login.jsp代码
<%-- Created by IntelliJ IDEA. User: shaonaiyi Date: 2022/3/6 Time: 14:47 To change this template use File | Settings | File Templates. --%> <%@ page contentType="text/html;charset=UTF-8" language="java" %> <html> <head> <title>登录界面</title> <style type="text/css"> body{ background-position: center; background-repeat: no-repeat; } </style> </head> <body> <div style="text-align:center;margin-top:120px"> <h2>登录页面</h2> <form action="/login" method="post"> <table style="margin-left:40%"> <tr> <td>用户名:</td> <td><input type="text" size="21" name="username"/></td> </tr> <tr> <td>密码:</td> <td><input type="text" size="21" name="password"/></td> </tr> <tr> <td>角色:</td> <td> <input type="radio" name="role" value="0" checked="checked">管理员 <input type="radio" name="role" value="1">普通用户 </td> </tr> </table> <input type="submit" value="登录"/> <input type="reset" value="重置"/> </form> <a href="register.jsp">跳转注册</a> </div> </body> </html>
2. 编写success.jsp代码
<%@ page import="com.java.domain.User" %><%-- Created by IntelliJ IDEA. User: shaonaiyi Date: 2022/3/6 Time: 14:59 To change this template use File | Settings | File Templates. --%> <%@ page contentType="text/html;charset=UTF-8" language="java" %> <html> <head> <title>登录成功</title> </head> <body> <h1>登录成功!</h1> <br> <font color="red"> <% User user = (User)request.getAttribute("user"); String username = user.getUsername(); %> 欢迎您: <% out.print(username); %> </font> </body> </html>
3. 编写defeat.jsp代码
<%-- Created by IntelliJ IDEA. User: shaonaiyi Date: 2022/3/6 Time: 14:59 To change this template use File | Settings | File Templates. --%> <%@ page contentType="text/html;charset=UTF-8" language="java" %> <html> <head> <title>登录失败</title> </head> <body> <h1>登录失败!</h1> <font color="red"> <% if(request.getAttribute("message")!= null){ out.print(request.getAttribute("message")); } %> </font> <br> <a href="login.jsp">重新登录</a> </body> </html>
4. 编写register.jsp代码
<%-- Created by IntelliJ IDEA. User: shaonaiyi Date: 2022/3/6 Time: 16:37 To change this template use File | Settings | File Templates. --%> <%@ page contentType="text/html;charset=UTF-8" language="java" %> <html> <head> <title>注册界面</title> <style type="text/css"> body{ background-repeat: no-repeat; background-position: center; } </style> </head> <body> <div style="text-align:center;margin-top: 120px"> <form action="/register" method="post"> <table style="margin-left:40%"> <h2>用户注册</h2> <tr> <td>用户名:</td> <td><input name="username" type="text" size="21"></td> </tr> <tr> <td>密码:</td> <td><input name="password" type="password" size="21"></td> </tr> </table> <input type="submit" value="注册"> <input type="reset" value="重置"> </form> <a href="login.jsp">跳转登录</a> </form> </div> </body> </html>
5. 编写register-success.jsp代码
<%@ page import="com.java.domain.User" %><%-- Created by IntelliJ IDEA. User: shaonaiyi Date: 2022/3/6 Time: 14:59 To change this template use File | Settings | File Templates. --%> <%@ page contentType="text/html;charset=UTF-8" language="java" %> <html> <head> <title>注册成功</title> </head> <body> <h1>注册成功!</h1> <br> <a href="login.jsp">跳转登录</a> <a href="register.jsp">继续注册</a> </body> </html>
0xFF 总结
本篇教程内容比较长,适合初学者学习,但是也需要有一点基础,比如环境的配置,但如果你稍微有点基础,操作一下这篇文章是非常不错的,特别是代码。
关于JavaWeb其他文章参考:
JavaWeb项目如何配置Servlet
使用Eclipse打开IDEA写的JavaWeb项目【全网唯一教程】