1
首先在sql 中创建一个数据库 ASD 然后创建一个表 存放 用户名,密码,性别,爱好,出生日期,个性签名,注册时间。
2
,写一个java 类 User,连接数据库,
<span style="font-size:14px;">package cn.com.servlet;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
public class User {
String username = null;
Connection conn = null;
PreparedStatement pstmt = null;
ResultSet rs = null;
//1.获取数据库连接
public Connection getConnection(){
try {
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
String url = "jdbc:sqlserver://localhost:1433;databasename=ASD";
String user = "sa";
String password = "SQL2014";
conn = DriverManager.getConnection(url, user, password);
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return conn;
}
//2.执行更新语句
public int executeUpdate(String sql,Object...objects){
int row = 0;
conn = this.getConnection();
try {
pstmt = conn.prepareStatement(sql);
for (int i = 0; i < objects.length; i++) {
pstmt.setObject(i+1, objects[i]);
}
row = pstmt.executeUpdate();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return row;
}
//3.执行查询语句
public ResultSet executeQuery(String sql,Object...objects){
conn = this.getConnection();
try {
pstmt = conn.prepareStatement(sql);
for (int i = 0; i < objects.length; i++) {
pstmt.setObject(i+1, objects[i]);
}
rs = pstmt.executeQuery();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return rs;
}
//4.关闭资源
public void closeAll(){
if(rs != null){
try {
rs.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
if(pstmt != null){
try {
pstmt.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
if(conn != null){
try {
conn.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
}</span>
3.
写一个servlet 将注册信息存到数据库中 Usertext
<span style="font-size:14px;">package cn.com.servlet;
import java.io.IOException;
import java.io.PrintWriter;
import java.text.DateFormat;
import java.util.Date;
import java.util.Locale;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class Usertest extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html");
PrintWriter out = response.getWriter();
out.println("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\">");
out.println("<HTML>");
out.println(" <HEAD><TITLE>A Servlet</TITLE></HEAD>");
out.println(" <BODY>");
out.print(" This is ");
out.print(this.getClass());
out.println(", using the GET method");
out.println(" </BODY>");
out.println("</HTML>");
out.flush();
out.close();
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
//脚本乱麻
response.setCharacterEncoding("utf-8");
request.setCharacterEncoding("utf-8");
//获取表单数据
String username = request.getParameter("username");
String password = request.getParameter("password");
String sex = request.getParameter("sex");
String[] hobbys = request.getParameterValues("hobby");
String hobby = "";
for (String string : hobbys) {
hobby = hobby + string+" ";
}
String year = request.getParameter("year");
String month = request.getParameter("month");
String day = request.getParameter("day");
//获取系统当前时间
String bir = year+"-"+month+"-"+day;
java.sql.Date time = java.sql.Date.valueOf(bir);
DateFormat df = null; //中文显示时间
df = DateFormat.getDateTimeInstance(DateFormat.YEAR_FIELD,DateFormat.ERA_FIELD,new Locale("zh","CN"));
String ztime = df.format(new Date());
String sign = request.getParameter("sign");
//3.插入数据库
User conn = new User();
String sql = "insert into u_tabe1 values(?,?,?,?,?,?,?)";
int row = conn.executeUpdate(sql, username,password,sex,hobby,time,sign,ztime);
response.setContentType("text/html");
PrintWriter out = response.getWriter();
out.println("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\">");
out.println("<HTML>");
out.println(" <HEAD><TITLE>A Servlet</TITLE></HEAD>");
out.println(" <BODY>");
if(row != 0){
out.println("注册成功!");
//跳转到登录页面
RequestDispatcher rd = request.getRequestDispatcher("MyHtml.html");
rd.forward(request, response);
}else {
out.println("注册失败!");
response.sendRedirect("zhuceshi.html");
}
out.println(" </BODY>");
out.println("</HTML>");
out.flush();
out.close();
}
}</span>
4.
写一个登录验证的servlet text
<span style="font-size:14px;">package cn.com.servlet;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.ResultSet;
import java.sql.SQLException;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class test extends HttpServlet {
@Override
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html");
PrintWriter out = response.getWriter();
out.println("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\">");
out.println("<HTML>");
out.println(" <HEAD><TITLE>A Servlet</TITLE></HEAD>");
out.println(" <BODY>");
out.print(" This is ");
out.print(this.getClass());
out.println(", using the GET method");
out.println(" </BODY>");
out.println("</HTML>");
out.flush();
out.close();
}
@Override
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
//修改编码
request.setCharacterEncoding("utf-8");
response.setCharacterEncoding("utf-8");
//设置响应的内容类型 :html网页
response.setContentType("text/html");
PrintWriter out = response.getWriter();
//获取表单的数据
String username = request.getParameter("username");
String password = request.getParameter("password");
//连接数据库 判断能否登录
User dbUtil = new User();
String sql = "select * from u_tabe1 where username = ? and password = ?";
ResultSet rs = dbUtil.executeQuery(sql, username,password);
//给浏览器的响应内容(html源代码)
out.println("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\">");
out.println("<HTML>");
out.println(" <HEAD><TITLE>A Servlet</TITLE></HEAD>");
out.println(" <BODY>");
try {
if(rs.next()){
</span><pre name="code" class="html"><span style="font-size:14px;"><span style="white-space:pre"> </span>out.println("登录成功!");</span>
}else{out.println("登录失败!");
<span style="font-size:14px;"><span style="white-space:pre"> </span>request.getRequestDispatcher("MyHtml.html").forward(request, response);</span>
<span style="font-size:14px;"><span style="white-space:pre"> </span>}</span>
<span style="font-size:14px;"><span style="white-space:pre"> </span>} catch (SQLException e) {// TODO Auto-generated catch blocke.printStackTrace();} finally{dbUtil.closeAll();}out.println(" </BODY>");out.println("</HTML>");out.flush();out.close();}}</span>
5.
写一个注册的页面 ,zhuce.html
<html>
<head>
<title>zhuce.html</title>
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="this is my page">
<meta http-equiv="content-type" content="text/html; charset=gb2312">
<!--<link rel="stylesheet" type="text/css" href="./styles.css">-->
</head>
<body>
<form action="test_1" method="post">
<div align="center">
<table>
<tr>
<td colspan="2" align="center"><h1>注册页面</h1></td>
</tr>
<tr>
<td width="100">用户名:</td>
<td><input name="username" type="text" /></td>
</tr>
<tr>
<td>密码:</td>
<td><input name="password" type="password"></td>
</tr>
<tr>
<td>确认密码:</td>
<td><input name="password" type="password"></td>
</tr>
<tr>
<td>性别:</td>
<td>
<input type="radio" name="sex" value="男" checked />男
<input type="radio" name="sex" value="女" />女
</td>
</tr>
<tr>
<td>爱好:</td>
<td>
<input type="checkbox" name="hobby" value="影视" />影视
<input type="checkbox" name="hobby" value="文学" />文学
<input type="checkbox" name="hobby" value="游戏" />游戏
<input type="checkbox" name="hobby" value="体育" />体育
</td>
</tr>
<tr>
<td>出生日期:</td>
<td>
<select name="year">
<option>-请选择-</option>
<option>1995</option>
<option>1996</option>
<option>1997</option>
<option>1998</option>
<option>1999</option>
<option>2000</option>
<option>2001</option>
<option>2002</option>
<option>2003</option>
<option>2004</option>
<option>2005</option>
</select>
年
<select name="month">
<option>-请选择-</option>
<option>1</option>
<option>2</option>
<option>3</option>
<option>4</option>
<option>5</option>
<option>6</option>
<option>7</option>
<option>8</option>
<option>9</option>
<option>10</option>
<option>11</option>
<option>12</option>
</select>
月
<select name="day">
<option>-请选择-</option>
<option>1</option><option>2</option><option>3</option>
<option>4</option><option>5</option><option>6</option>
<option>7</option><option>8</option><option>9</option>
<option>10</option><option>11</option><option>12</option>
<option>13</option><option>14</option><option>15</option>
<option>16</option><option>17</option><option>18</option>
<option>19</option><option>20</option><option>21</option>
<option>22</option><option>23</option><option>24</option>
<option>25</option><option>26</option><option>27</option>
<option>28</option><option>29</option><option>30</option>
<option>31</option>
</select>
日</td>
</tr>
<tr>
<td>个性签名:</td>
<td>
<textarea rows="3" cols="40" name="sign"></textarea></td>
</tr>
<tr>
<td colspan="2" align="center">
<input type="submit" value="注册" />
<input type="reset" value="重置" />
</td>
</tr>
</table>
</div>
</form>
</body>
</html>
6.
写一个登录的页面
<!DOCTYPE html>
<html>
<head>
<title>MyHtml.html</title>
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="this is my page">
<meta http-equiv="content-type" content="text/html; charset=gb2312">
<!--<link rel="stylesheet" type="text/css" href="./styles.css">-->
</head>
<body>
<form action="test" method="post">
<table align="center" border="1" cellspacing="0" cellpadding="10">
<tr>
<td>用户名:</td>
<td><input type="text" name="username" /></td>
</tr>
<tr>
<td>密码:</td>
<td><input type="password" name="password" /></td>
</tr>
<tr>
<td colspan="2" align="center">
<input type="submit" value="登录" />
<input type="reset" value="重置" />
<a href="zhuce.html">注册</a>
</td>
</tr>
</form>
</body>
</html>
7
然后配置web.xml
在WebRoot 文件夹下的 WEB-INF 下
<servlet>
<servlet-name>Login</servlet-name>
<servlet-class>cn.com.servlet.Usertest</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>Login</servlet-name>
<url-pattern>/test_1</url-pattern>
</servlet-mapping>
注意:
<servlet-name>的值是自己随便定义的 必须和
<servlet-mapping>
<servlet-name>的值一样
<servlet-class> 包名+‘.’+类名
<url-pattern> 自己定义的