jsp第九次作业
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%> <% String path = request.getContextPath(); String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; %> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <base href="<%=basePath%>"> <title>register</title> <meta http-equiv="pragma" content="no-cache"> <meta http-equiv="cache-control" content="no-cache"> <meta http-equiv="expires" content="0"> <meta http-equiv="keywords" content="keyword1,keyword2,keyword3"> <meta http-equiv="description" content="This is my page"> <!-- <link rel="stylesheet" type="text/css" href="styles.css"> --> <style type="text/css"> body{font-size:20px;} </style> </head> <body> <form action="doregister.jsp" name="form1" method="post"> 用户名:<input type="text" name="userName" size="20"><br> 密 码: <input type="password" name="password" size="20"><br> 验证码:<input type="text" name="validationCode" onkeyDown="if(event.keyCode==13){form1.submit.focus();}" size=6> <% int intmethod1 = (int)(((Math.random())*11)-1); int intmethod2 = (int)(((Math.random())*11)-1); int intmethod3 = (int)(((Math.random())*11)-1); int intmethod4 = (int)(((Math.random())*11)-1); //将的到的随机数进行连接 String intsum = ""+intmethod1+intmethod2+intmethod3+intmethod4; %> <!--设置隐藏域验证比较时使用 --> <input type="hidden" name="validationCode1" value="<%=intsum%>"> <input type="text" value="<%=intsum%>" size=5> <br> <input type="submit" name="submit1" value="注册" onClick="mycheck()"> <input type="reset" value="重置"> </form> <br> </body> </html>
<%@ page language="java" contentType="text/html; charset=utf-8" pageEncoding="utf-8"%> <%@page import="java.sql.SQLException"%> <%@page import="com.gd.dao.BaseDao"%> <%@page import="com.gd.entity.Msg"%> <%@page import="java.sql.DriverManager"%> <%@page import="java.sql.PreparedStatement"%> <%@page import="java.sql.ResultSet"%> <%@page import="java.util.Random"%> <%@page import="java.sql.Connection"%> <!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>doregister</title> </head> <body> <% request.setCharacterEncoding("utf-8"); String username = request.getParameter("userName"); String password = request.getParameter("password"); Random r = new Random(); String email = r.nextInt(100)+ r.nextInt(100)+"@qq.com"; try { // 1.加载驱动 Class.forName("com.mysql.jdbc.Driver"); // 2.建立连接 Connection con = DriverManager.getConnection( "jdbc:mysql://localhost:3306/sxz", "root", "sxz990715"); // 3.编写SQL语句 String sql = "insert into users(username,password,email) values(?,?,?)"; PreparedStatement ps = con.prepareStatement(sql);// PS是用来执行SQL语句和给问号赋值的 ps.setString(1, username); ps.setString(2, password); ps.setString(3, email); // 4.执行SQL语句 ps.executeUpdate(); // 执行查询 // 5.处理查询结果(查询的时候需要) // 6.关闭连接 ps.close(); con.close(); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } %> 注册成功 </br> <a href="index.jsp">【返回】 </body> </html>