<%-- Created by IntelliJ IDEA. User: xinfeng Date: 2020/5/22 Time: 10:00 To change this template use File | Settings | File Templates. --%> <%@ page contentType="text/html;charset=UTF-8" language="java" %> <html> <head> <title>$Title$</title> </head> <body> <form action="dologin.jsp" method="post"> 用户名:<input type="text" name="uname"/><br> 密码 :<input type="password" name="upwd"/><br> 验证码 :<input type="text" name="validationCode" size="6"/> <% int intmethod1 = (int) (Math.random() * 10); int intmethod2 = (int) (Math.random() * 10); int intmethod3 = (int) (Math.random() * 10); int intmethod4 = (int) (Math.random() * 10); String intsum = intmethod1 + "" + intmethod2 + intmethod3 + intmethod4; %> <input type="hidden" name="validationCode1" value="<%=intsum%>"> <img src="image/<%=intmethod1%>.png"> <img src="image/<%=intmethod2%>.png"> <img src="image/<%=intmethod3%>.png"> <img src="image/<%=intmethod4%>.png"><br><br> <input type="submit" name="submit" value="登录"> <input type="reset" value="重置"><br><br> <a href="register.jsp">注册新用户请单击此处</a> </form> </body> </html>
<%-- Created by IntelliJ IDEA. User: xinfeng Date: 2020/5/22 Time: 10:00 To change this template use File | Settings | File Templates. --%> <%@ page contentType="text/html;charset=UTF-8" language="java" %> <html> <head> <title>Title</title> </head> <body> <form action="RegisterServlet" method="post"> 用户名:<input name="username"><br><br> 密 码:<input name="userpassword"><br><br> 邮 箱:<input name="Email"><br><br> <input type="submit" name="submit" value="注册"> <input type="reset" value="重置"><br> </form> </body> </html>
package Servlet; import JDBC.JDBC; 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.swing.*; import java.io.IOException; import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.SQLException; @WebServlet(name = "LoginServlet") public class RegisterServlet extends HttpServlet { public RegisterServlet() { super(); } @Override public void destroy() { super.destroy(); } public void wrong1() { String msg = "注册成功!"; int type = JOptionPane.YES_NO_OPTION; String title = "信息提示"; JOptionPane.showMessageDialog(null, msg, title, type); } protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { JDBC jdbc = new JDBC(); Connection con = null; PreparedStatement ps = null; String username=new String(request.getParameter("username").getBytes("ISO-8859-1"),"UTF-8"); String PASSWORD=new String(request.getParameter("userpassword").getBytes("ISO-8859-1"),"UTF-8"); String email=new String(request.getParameter("Email").getBytes("ISO-8859-1"),"UTF-8"); try { con = jdbc.getConnection(); String sql = "insert into users(username,PASSWORD,email) values (?,?,?)"; ps = con.prepareStatement(sql); ps.setString(1, username); ps.setString(2,PASSWORD); ps.setString(3, email); int row = ps.executeUpdate(); wrong1(); } catch (SQLException e) { e.printStackTrace(); }finally { jdbc.closeAll(con,ps,null); } } protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doPost(request, response); } }
<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd" version="4.0"> <servlet-mapping> <servlet-name>RegisterServlet</servlet-name> <url-pattern>/RegisterServlet</url-pattern> </servlet-mapping> <servlet> <servlet-name>RegisterServlet</servlet-name> <servlet-class>Servlet.RegisterServlet</servlet-class> </servlet> </web-app>